{"id":16247259,"url":"https://github.com/dotnetcarpenter/decorators","last_synced_at":"2025-04-08T11:49:43.287Z","repository":{"id":10743245,"uuid":"66808565","full_name":"dotnetCarpenter/decorators","owner":"dotnetCarpenter","description":"small stab into decorators","archived":false,"fork":false,"pushed_at":"2023-01-08T13:47:50.000Z","size":498,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-14T12:16:21.951Z","etag":null,"topics":["decorators"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dotnetCarpenter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-29T03:35:02.000Z","updated_at":"2022-03-29T09:50:02.000Z","dependencies_parsed_at":"2023-01-11T17:56:26.686Z","dependency_job_id":null,"html_url":"https://github.com/dotnetCarpenter/decorators","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnetCarpenter%2Fdecorators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnetCarpenter%2Fdecorators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnetCarpenter%2Fdecorators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dotnetCarpenter%2Fdecorators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dotnetCarpenter","download_url":"https://codeload.github.com/dotnetCarpenter/decorators/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247838415,"owners_count":21004576,"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":["decorators"],"created_at":"2024-10-10T14:36:36.453Z","updated_at":"2025-04-08T11:49:43.265Z","avatar_url":"https://github.com/dotnetCarpenter.png","language":"JavaScript","readme":"[![Test with code coverage](https://github.com/dotnetCarpenter/decorators/actions/workflows/unit-test-with-code-coverage.yml/badge.svg)](https://github.com/dotnetCarpenter/decorators/actions/workflows/unit-test-with-code-coverage.yml)\n[![codecov](https://codecov.io/gh/dotnetCarpenter/decorators/branch/master/graph/badge.svg)](https://codecov.io/gh/dotnetCarpenter/decorators)\n\n\nhttp://javascript.info/tutorial/decorators\n\n\n## API\nThis is just small snippets, but perhaps they\ncan inspire you to write your own decorators.\n\nUse either esm import or CommonJS.\n\nE.g.\n\n```js\nimport logger from \"src/logger.mjs\"\n\n// or\n\nconst logger = require \"src/logger.cjs\"\n```\n\n\n### Logging\nWhere `work` is your function, that you want to log calls to.\n\n```js\nimport logger from \"src/logger.mjs\"\n\nwork = logger(work)\nwork(1,2)\nwork(5,6)\nwork.outputLog() // will use console.log to output calls made to work\n```\n\nOr if you want to do something with the logs, you can provide your own\nlog handler function.\n\n```js\nimport logger from \"src/logger.mjs\"\n\nconst logs = []\nwork = logger(work, log =\u003e logs.push(log)) // add a function to be called for each log\nwork(1,2)\nwork(5,6)\nwork.outputLog() // will fill the logs array with logs\n```\n\n\n### Caching\nWhere `slow` is your function, that you want to speed up.\n\n```js\nimport memoize from \"src/cache.mjs\"\n\nconst speedy = memoize(slow)\nlet a = speedy(22) // call slow with 22 and return the result\nlet b = speedy(22) // returns the previous result\n```\n\n`memoize` can also have a context. Which is useful when you use\nprototypal inheritance or objects with methods.\n\n```js\nimport memoize from \"src/cache.mjs\"\n\nclass MyClass {\n\tconstructor() {\n\t\tthis.slow = slow\n\t}\n\texecute(values) {\n\t\treturn this.slow(values)\n\t}\n}\n\nconst myClass = new MyClass\nconst speedy = memoize(myClass.execute, myClass)\nlet a = speedy(22) // call slow with 22 and return the result\nlet b = speedy(22) // returns the previous result\n```\n\n```js\nconst myObject = {\n\tslow,\n\texecute(values) {\n\t\treturn this.slow(values)\n\t}\n}\n\nconst speedy = memoize(myObject.execute, myObject)\nlet a = speedy(22) // call slow with 22 and return the result\nlet b = speedy(22) // returns the previous result\n```\n\n\n### Guarding\nA *guard* decorator is useful when you want to guard against\nparticular arguments to a function. This is known as defensive\ncoding and if implemented inside your function, can quickly\nmake a mess of otherwise clean functions.\n\nBut with a decorator your function will still be clean and\nyour can *turn on/off* guards as needed. I suggest your use\nguards while developing and construct your guards to throw\nerrors. This way, you can write your application and quickly\nspot malfunctioning caller functions. When the application\nis well tested, your can remove the guards and gain better\nperformance.\n\nWhere `work` will return nonsense if the second argument is zero.\n\n```js\nimport {guard, guards} from \"src/guard.mjs\"\n\nwork = guard(work, guards.zero(1)) // default error for guards.zero is TypeError\nwork(32,0) // will throw a TypeError\n```\n\nOr with a custom error:\n\n```js\nimport {guard, guards} from \"src/guard.mjs\"\n\nwork = guard(work, guards.zero(1, new RangeError(\"Second argument to work MUST be between 1-100\")))\nwork(32,0) // will throw a RangeError with the message: \"Second argument to work MUST be between 1-100\"\n```\n\n**Custom guard**\nWhere `work` takes an object as first argument and we want to throw if a property is *falsy*.\n\n```js\nimport {guard} from \"src/guard.mjs\"\n\nwork = guard(work, (...args) =\u003e {\n\tif( !args[0].requiredProperty ) throw new TypeError(\"options.requiredProperty MUST be set\")\n\treturn true\n})\nwork({ requiredProperty: undefined }) // will throw\n```\n\n## How to test\n`npm test`\n\nRemember to install the npm modules before running\nthe test suite. Use `npm i` to install.\n\n\n## How to build\n`make`\n\nYou can build faster by specifying building in parallel.\n`make -j 2` for building two targets at the time.\nYou should set the `-j` parameter to as many cpu cores,\nas you have.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdotnetcarpenter%2Fdecorators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdotnetcarpenter%2Fdecorators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdotnetcarpenter%2Fdecorators/lists"}