{"id":19332491,"url":"https://github.com/yuchi/decorators-toolbox","last_synced_at":"2025-07-08T12:07:59.176Z","repository":{"id":34794071,"uuid":"38778313","full_name":"yuchi/decorators-toolbox","owner":"yuchi","description":"A Toolbox for writing ES7 decorators more easily","archived":false,"fork":false,"pushed_at":"2015-07-10T10:16:18.000Z","size":144,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-24T07:17:57.059Z","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":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yuchi.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":"2015-07-08T20:31:18.000Z","updated_at":"2018-10-31T08:52:24.000Z","dependencies_parsed_at":"2022-07-17T05:30:33.413Z","dependency_job_id":null,"html_url":"https://github.com/yuchi/decorators-toolbox","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yuchi/decorators-toolbox","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuchi%2Fdecorators-toolbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuchi%2Fdecorators-toolbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuchi%2Fdecorators-toolbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuchi%2Fdecorators-toolbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuchi","download_url":"https://codeload.github.com/yuchi/decorators-toolbox/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuchi%2Fdecorators-toolbox/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264267069,"owners_count":23581929,"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-10T02:46:01.203Z","updated_at":"2025-07-08T12:07:59.151Z","avatar_url":"https://github.com/yuchi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Decorators Toolbox\n==================\n\nA suite of helper for writing ES2016 (aka ES7) decorators.\n\n## Tools in the box\n\n- [**Value Transformer**](#value-transformer)\n- [**Ensure Accessors**](#ensure-accessors)\n\n- - -\n\nEvery helper has the same ‘shape’, you use them to build a decorator by\nproviding the required implementation details.\n\nEvery helper is tested against:\n- object properties,\n- object properties accessors,\n- object function property shorthand,\n- class initializers,\n- class static initializers,\n- class accessors,\n- class static accessors.\n\n**Very important disclaimer**: all built decorators must be used with parens,\nthat is in the form `@decorator(...args)`. You can anyway invoke it just after\ncreation to get a parens-free decorator. My personal advice: choose the parens.\n\n### Value Transformer\n\n```js\nimport { valueTransformer } from 'decorators-toolbox';\nimport valueTransformer from 'decorators-toolbox/value-transformer';\n```\n\nCreates a decorator that modifies the value of the property you attach it to.\n\n#### Examples\n\n```js\nconst multiplier = valueTransformer(m =\u003e n =\u003e m * n);\n//                                  ┬    ┬    ─┬───\n//                                  │    │     └─ result\n//                                  │    └─ value\n//                                  └─ parameter\n\nconst target = {\n  @multiplier(7) // the parameter\n  value: 5       // the value\n};\n\nconsole.assert(target.value === 35); // the result, 3 × 5\n```\n\n```js\nconst trimmer = valueTransformer(() =\u003e s =\u003e s.trim());\nconst upperCase = valueTransformer(() =\u003e s =\u003e s.toUpperCase());\nconst replacer = valueTransformer((from, to) =\u003e s =\u003e s.split(from).join(to));\n\nconst target = {\n  @trimmer()\n  @upperCase()\n  @replacer(',', '--')\n  get value() {\n    return this.storage;\n  },\n  set value(v) {\n    this.storage = v;\n  }\n};\n\n// If you use accessors it works with new values too.\ntarget.value = '   so space, very blanks    ';\n\nconsole.assert(target.value === 'SO SPACE-- VERY BLANKS');\n```\n\n## Ensure Accessors\n\n```js\nimport { ensureAccessors } from 'decorators-toolbox';\nimport ensureAccessors from 'decorators-toolbox/ensure-accessors';\n```\n\nWraps a decorator in such a way that the descriptor is guaranteed to be of a\ncomputed property.\n\nWhen it has to convert a normal property to a computed one it uses a *Symbol* to\nstore the value in the object.\n\n:warning: This helper creates decorators that make properties’ initialization\nlazy! :warning:\n\n### Examples\n\n```js\nconst emitOnChange = ensureAccessors(() =\u003e (target, name, descriptor) =\u003e {\n  const originalSet = descriptor.set;\n\n  // You don’t have to worry about `value` or `initializer`\n\n  return {\n    ...descriptor,\n    set(value) {\n      this.emit(`change:${ name }`, { value });\n\n      if (originalSet) {\n        this::originalSet(value);\n      }\n    }\n  };\n});\n\nclass Person extends EventEmitter {\n  @emitOnChange()\n  firstName = \"\";\n}\n\nconst author = new Person();\n\nauthor.firstName = 'Pier Paolo'; // fires 'change:firstName'\n```\n\n## License\n\nThis library, *Decorators Toolbox*, is free software (\"Licensed Software\"); you\ncan redistribute it and/or modify it under the terms of the [GNU Lesser General\nPublic License](http://www.gnu.org/licenses/lgpl-2.1.html) as published by the\nFree Software Foundation; either version 2.1 of the License, or (at your\noption) any later version.\n\nThis library is distributed in the hope that it will be useful, but WITHOUT ANY\nWARRANTY; including but not limited to, the implied warranty of MERCHANTABILITY,\nNONINFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General\nPublic License for more details.\n\nYou should have received a copy of the [GNU Lesser General Public\nLicense](http://www.gnu.org/licenses/lgpl-2.1.html) along with this library; if\nnot, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth\nFloor, Boston, MA 02110-1301 USA\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuchi%2Fdecorators-toolbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuchi%2Fdecorators-toolbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuchi%2Fdecorators-toolbox/lists"}