{"id":21270426,"url":"https://github.com/dan503/obj-append-strings","last_synced_at":"2025-03-15T11:46:52.190Z","repository":{"id":65467332,"uuid":"80498086","full_name":"Dan503/obj-append-strings","owner":"Dan503","description":"A simple js function for appending strings to the end of values in an object","archived":false,"fork":false,"pushed_at":"2017-02-07T10:24:26.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-22T02:28:52.839Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Dan503.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-31T06:50:19.000Z","updated_at":"2017-01-31T09:37:56.000Z","dependencies_parsed_at":"2023-01-25T08:45:53.445Z","dependency_job_id":null,"html_url":"https://github.com/Dan503/obj-append-strings","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dan503%2Fobj-append-strings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dan503%2Fobj-append-strings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dan503%2Fobj-append-strings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dan503%2Fobj-append-strings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Dan503","download_url":"https://codeload.github.com/Dan503/obj-append-strings/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243725563,"owners_count":20337667,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-21T08:17:25.587Z","updated_at":"2025-03-15T11:46:52.150Z","avatar_url":"https://github.com/Dan503.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# obj-append-strings\n\n``````\nnpm install obj-append-strings --save\n``````\n\nThis is a tiny function for bulk appending strings to other strings through the use of objects.\n\nSo basically instead of doing this:\n\n````js\nfunction functionName(settings){\n\n  //This is ugly\n  settings = settings || {};\n  settings.setting_1 = settings.setting_1 + ' c';\n  settings.setting_2 = settings.setting_2 || {};\n  settings.setting_2.alpha = settings.setting_2.alpha  + ' d';\n\n  return settings;\n}\n\nvar variable = functionName({\n  setting_1: 'a',\n  setting_2: {\n    alpha: 'b'\n  },\n});\n\n//variable = { setting_1 : 'a c', setting_2 : { alpha: 'b d' } }\n````\n\nYou can avoid the ugly repetition by doing this instead:\n\n````js\nvar appendStrings = require('obj-append-strings');\n\nfunction functionName(settings){\n\n  //This is pretty\n  settings = appendStrings(settings, {\n    setting_1: ' c',\n    setting_2: {\n      alpha: ' d'\n    },\n  });\n\n  return settings;\n}\n\nvar variable = functionName({\n  setting_1: 'a',\n  setting_2: {\n    alpha: 'b'\n  },\n});\n\n//variable = { setting_1 : 'a c', setting_2 : { alpha: 'b d' } }\n````\n\nOr if you want to prep-end the strings instead:\n\n````js\nvar appendStrings = require('obj-append-strings');\n\nfunction functionName(settings){\n\n  //This adds the strings before the original value\n  settings = appendStrings(settings, {\n    setting_1: 'c ',\n    setting_2: {\n      alpha: 'd ',\n    },\n  }, 'before');\n\n  return settings;\n}\n\nvar variable = functionName({\n  setting_1: 'a',\n  setting_2: {\n    alpha: 'b'\n  },\n});\n\n\n//variable = { setting_1 : 'c a', setting_2 : alpha: { 'd b' } }\n````\n\nIf you can 100% guarantee that the object having the strings appended to it is an already defined object (not \"undefined\") then you can leave off the `settings = ` bit:\n\n`````````js\n  //leave off the \"settings =\" bit if you can 100% guarantee that \"settings\" is already defined\n  appendStrings(settings, {\n    setting_1: 'c ',\n    setting_2: {\n      alpha: 'd ',\n    },\n  });\n\n`````````\n\nIf there are values that are undefined, it will just use the values that are available\n\n````js\nvar appendStrings = require('obj-append-strings');\n\nfunction functionName(settings){\n\n  //This adds the strings before the original value\n  settings = appendStrings(settings, {\n    setting_1: 'c ',\n    setting_2: {\n      alpha: 'd ',\n    },\n  });\n\n  return settings;\n}\n\nvar variable_1 = functionName({\n  setting_2: {\n    alpha: 'b'\n  },\n});\n\n\nvar variable_2 = functionName({\n  setting_1: 'a',\n});\n\n\n//variable_1 = { setting_1 : 'c ', setting_2 : alpha: { 'd b' } }\n//variable_2 = { setting_1 : 'a c', setting_2 : alpha: { 'd ' } }\n````\n\nIt also works the other way around (yeah the code in this example is a bit silly)\n\n````js\nvar appendStrings = require('obj-append-strings');\n\nfunction functionName(settings){\n\n  //This adds the strings before the original value\n  settings = appendStrings(settings, {\n  });\n\n  return settings;\n}\n\nvar variable = functionName({\n  setting_1: 'a',\n  setting_2: {\n    alpha: 'b'\n  },\n});\n\n//variable = { setting_1 : 'a', setting_2 : alpha: { 'b' } }\n````\n\n## Pug usage\n\nI primarily built this function for use in [Pug](https://pugjs.org/api/getting-started.html) templates as a way of assigning permanent classes to sub modules.\n\nTo use the function in pug you will need to parse the `require` function from node.js into the Pug `locals` object.\n\nIf you're using Gulp, then this is a simplified version of the setup you would use to compile Pug templates that have support for the `require` node.js function:\n\n```````````js\ngulp.src('**/*.pug')\n  .pipe(plugins.pug({\n    locals: {\n      //this bit gives access to the \"require\" function from inside pug templates\n      require: require,\n    }\n  }))\n```````````\n\nOnce you have the `require` function available inside your pug templates, you can use the function in pug mixins like this to assign permanent classes to sub modules. The below example uses [default-to](https://www.npmjs.com/package/default-to) to make the syntax simpler.\n\n```````````pug\ninclude path/to/subModule1\ninclude path/to/subModule2\n\n- var defaultTo = require('default-to').default;\n- var appendStrings = require('obj-append-strings');\n\nmixin example(spec)\n  -\n    //Any classes in here will be lost if the user defines their own classes\n    defaultTo(spec, {\n      classes : '',\n      subModule1 : {\n        classes : 'overridable-classes'\n      },\n      subModule2 : {\n        classes : 'overridable-classes'\n      }\n    });\n\n    //classes that are placed here are impossible for the user to override remotely\n    spec = appendStrings(spec, {\n      subModule1: {\n        classes: ' permanent-classes'\n      },\n      subModule2: {\n        classes: ' permanent-classes'\n      }\n    })\n\n  .example(class=spec.classes)\u0026attributes(attributes)\n    +subModule1(spec.subModule1)\n    +subModule2(spec.subModule2)\n\n```````````\n\nThis is how that would look when calling the Pug mixin\n\n`````````pug\ninclude path/to/example\n\n+example({\n  classes: 'module-classes',\n  subModule1: {\n    classes: 'subModule1-overide-classes',\n  },\n  subModule2: {\n    classes: 'subModule2-overide-classes',\n  }\n})\n`````````","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdan503%2Fobj-append-strings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdan503%2Fobj-append-strings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdan503%2Fobj-append-strings/lists"}