├── .editorconfig ├── .esdoc.json ├── .gitignore ├── .npmignore ├── .travis.yml ├── .vscode ├── launch.json ├── settings.json └── tasks.json ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── docs ├── ast │ ├── source │ │ ├── after.js.json │ │ ├── afterAll.js.json │ │ ├── applicators │ │ │ ├── Applicator.js.json │ │ │ ├── BindApplicator.js.json │ │ │ ├── ComposeApplicator.js.json │ │ │ ├── InvokeApplicator.js.json │ │ │ ├── MemoizeApplicator.js.json │ │ │ ├── PartialApplicator.js.json │ │ │ ├── PartialedApplicator.js.json │ │ │ ├── PostValueApplicator.js.json │ │ │ ├── PreValueApplicator.js.json │ │ │ ├── WrapApplicator.js.json │ │ │ └── index.js.json │ │ ├── ary.js.json │ │ ├── attempt.js.json │ │ ├── before.js.json │ │ ├── beforeAll.js.json │ │ ├── bind.js.json │ │ ├── bindAll.js.json │ │ ├── curry.js.json │ │ ├── curryAll.js.json │ │ ├── curryRight.js.json │ │ ├── curryRightAll.js.json │ │ ├── debounce.js.json │ │ ├── debounceAll.js.json │ │ ├── defer.js.json │ │ ├── delay.js.json │ │ ├── factory │ │ │ ├── DecoratorConfig.js.json │ │ │ ├── DecoratorFactory.js.json │ │ │ ├── common.js.json │ │ │ └── index.js.json │ │ ├── flip.js.json │ │ ├── flow.js.json │ │ ├── flowRight.js.json │ │ ├── index.js.json │ │ ├── memoize.js.json │ │ ├── memoizeAll.js.json │ │ ├── mixin.js.json │ │ ├── negate.js.json │ │ ├── once.js.json │ │ ├── onceAll.js.json │ │ ├── overArgs.js.json │ │ ├── partial.js.json │ │ ├── partialRight.js.json │ │ ├── rearg.js.json │ │ ├── rest.js.json │ │ ├── shared.js.json │ │ ├── spread.js.json │ │ ├── tap.js.json │ │ ├── throttle.js.json │ │ ├── throttleAll.js.json │ │ ├── unary.js.json │ │ ├── utils │ │ │ ├── CompositeKeyWeakMap.js.json │ │ │ ├── bind.js.json │ │ │ ├── copyMetaData.js.json │ │ │ ├── index.js.json │ │ │ ├── log.js.json │ │ │ ├── resolveFunction.js.json │ │ │ └── returnAtIndex.js.json │ │ └── wrap.js.json │ └── test │ │ ├── after.spec.js.json │ │ ├── afterAll.spec.js.json │ │ ├── ary.spec.js.json │ │ ├── attempt.spec.js.json │ │ ├── before.spec.js.json │ │ ├── beforeAll.spec.js.json │ │ ├── bind.spec.js.json │ │ ├── bindAll.spec.js.json │ │ ├── combinationTest.spec.js.json │ │ ├── curry.spec.js.json │ │ ├── curryAll.spec.js.json │ │ ├── curryRight.spec.js.json │ │ ├── curryRightAll.spec.js.json │ │ ├── debounce.spec.js.json │ │ ├── debounceAll.spec.js.json │ │ ├── defer.spec.js.json │ │ ├── delay.spec.js.json │ │ ├── flip.spec.js.json │ │ ├── flow.spec.js.json │ │ ├── flowRight.spec.js.json │ │ ├── memoize.spec.js.json │ │ ├── memoizeAll.spec.js.json │ │ ├── mixin.spec.js.json │ │ ├── negate.spec.js.json │ │ ├── once.spec.js.json │ │ ├── overArgs.spec.js.json │ │ ├── partial.spec.js.json │ │ ├── partialRight.spec.js.json │ │ ├── rearg.spec.js.json │ │ ├── rest.spec.js.json │ │ ├── spread.spec.js.json │ │ ├── tap.spec.js.json │ │ ├── throttle.spec.js.json │ │ ├── unary.spec.js.json │ │ └── wrap.spec.js.json ├── badge.svg ├── class │ └── buildDocs │ │ ├── applicators │ │ ├── Applicator.js~Applicator.html │ │ ├── BindApplicator.js~BindApplicator.html │ │ ├── ComposeApplicator.js~ComposeApplicator.html │ │ ├── InvokeApplicator.js~InvokeApplicator.html │ │ ├── MemoizeApplicator.js~MemoizeApplicator.html │ │ ├── PartialApplicator.js~PartialApplicator.html │ │ ├── PartialedApplicator.js~PartialedApplicator.html │ │ ├── PostValueApplicator.js~PostValueApplicator.html │ │ ├── PreValueApplicator.js~PreValueApplicator.html │ │ └── WrapApplicator.js~WrapApplicator.html │ │ └── factory │ │ ├── DecoratorConfig.js~DecoratorConfig.html │ │ └── DecoratorFactory.js~InternalDecoratorFactory.html ├── coverage.json ├── css │ ├── prettify-tomorrow.css │ └── style.css ├── dump.json ├── file │ └── buildDocs │ │ ├── after.js.html │ │ ├── afterAll.js.html │ │ ├── applicators │ │ ├── Applicator.js.html │ │ ├── BindApplicator.js.html │ │ ├── ComposeApplicator.js.html │ │ ├── InvokeApplicator.js.html │ │ ├── MemoizeApplicator.js.html │ │ ├── PartialApplicator.js.html │ │ ├── PartialedApplicator.js.html │ │ ├── PostValueApplicator.js.html │ │ ├── PreValueApplicator.js.html │ │ ├── WrapApplicator.js.html │ │ └── index.js.html │ │ ├── ary.js.html │ │ ├── attempt.js.html │ │ ├── before.js.html │ │ ├── beforeAll.js.html │ │ ├── bind.js.html │ │ ├── bindAll.js.html │ │ ├── curry.js.html │ │ ├── curryAll.js.html │ │ ├── curryRight.js.html │ │ ├── curryRightAll.js.html │ │ ├── debounce.js.html │ │ ├── debounceAll.js.html │ │ ├── defer.js.html │ │ ├── delay.js.html │ │ ├── factory │ │ ├── DecoratorConfig.js.html │ │ ├── DecoratorFactory.js.html │ │ ├── common.js.html │ │ └── index.js.html │ │ ├── flip.js.html │ │ ├── flow.js.html │ │ ├── flowRight.js.html │ │ ├── index.js.html │ │ ├── memoize.js.html │ │ ├── memoizeAll.js.html │ │ ├── mixin.js.html │ │ ├── negate.js.html │ │ ├── once.js.html │ │ ├── onceAll.js.html │ │ ├── overArgs.js.html │ │ ├── partial.js.html │ │ ├── partialRight.js.html │ │ ├── rearg.js.html │ │ ├── rest.js.html │ │ ├── shared.js.html │ │ ├── spread.js.html │ │ ├── tap.js.html │ │ ├── throttle.js.html │ │ ├── throttleAll.js.html │ │ ├── unary.js.html │ │ ├── utils │ │ ├── CompositeKeyWeakMap.js.html │ │ ├── bind.js.html │ │ ├── copyMetaData.js.html │ │ ├── index.js.html │ │ ├── log.js.html │ │ ├── resolveFunction.js.html │ │ └── returnAtIndex.js.html │ │ └── wrap.js.html ├── function │ └── index.html ├── identifiers.html ├── image │ ├── badge.svg │ ├── esdoc-logo-mini-black.png │ ├── esdoc-logo-mini.png │ ├── github.png │ ├── manual-badge.svg │ └── search.png ├── index.html ├── package.json ├── script │ ├── inherited-summary.js │ ├── inner-link.js │ ├── manual.js │ ├── patch-for-local.js │ ├── prettify │ │ ├── Apache-License-2.0.txt │ │ └── prettify.js │ ├── pretty-print.js │ ├── search.js │ ├── search_index.js │ └── test-summary.js ├── source.html ├── test-file │ └── buildDocs │ │ ├── after.spec.js.html │ │ ├── afterAll.spec.js.html │ │ ├── ary.spec.js.html │ │ ├── attempt.spec.js.html │ │ ├── before.spec.js.html │ │ ├── beforeAll.spec.js.html │ │ ├── bind.spec.js.html │ │ ├── bindAll.spec.js.html │ │ ├── combinationTest.spec.js.html │ │ ├── curry.spec.js.html │ │ ├── curryAll.spec.js.html │ │ ├── curryRight.spec.js.html │ │ ├── curryRightAll.spec.js.html │ │ ├── debounce.spec.js.html │ │ ├── debounceAll.spec.js.html │ │ ├── defer.spec.js.html │ │ ├── delay.spec.js.html │ │ ├── flip.spec.js.html │ │ ├── flow.spec.js.html │ │ ├── flowRight.spec.js.html │ │ ├── memoize.spec.js.html │ │ ├── memoizeAll.spec.js.html │ │ ├── mixin.spec.js.html │ │ ├── negate.spec.js.html │ │ ├── once.spec.js.html │ │ ├── overArgs.spec.js.html │ │ ├── partial.spec.js.html │ │ ├── partialRight.spec.js.html │ │ ├── rearg.spec.js.html │ │ ├── rest.spec.js.html │ │ ├── spread.spec.js.html │ │ ├── tap.spec.js.html │ │ ├── throttle.spec.js.html │ │ ├── unary.spec.js.html │ │ └── wrap.spec.js.html ├── test.html └── variable │ └── index.html ├── mocha.opts ├── package-lock.json ├── package.json ├── src ├── after.spec.ts ├── after.ts ├── afterAll.spec.ts ├── afterAll.ts ├── applicators.ts ├── applicators │ ├── Applicator.ts │ ├── BindApplicator.ts │ ├── ComposeApplicator.ts │ ├── InvokeApplicator.ts │ ├── MemoizeApplicator.ts │ ├── PartialApplicator.ts │ ├── PartialValueApplicator.ts │ ├── PartialedApplicator.ts │ ├── PostValueApplicator.ts │ ├── PreValueApplicator.ts │ └── WrapApplicator.ts ├── ary.spec.ts ├── ary.ts ├── attempt.spec.ts ├── attempt.ts ├── before.spec.ts ├── before.ts ├── beforeAll.spec.ts ├── beforeAll.ts ├── bind.spec.ts ├── bind.ts ├── bindAll.spec.ts ├── bindAll.ts ├── combinationTest.spec.ts ├── curry.spec.ts ├── curry.ts ├── curryAll.spec.ts ├── curryAll.ts ├── curryRight.spec.ts ├── curryRight.ts ├── curryRightAll.spec.ts ├── curryRightAll.ts ├── debounce.spec.ts ├── debounce.ts ├── debounceAll.spec.ts ├── debounceAll.ts ├── defer.spec.ts ├── defer.ts ├── delay.spec.ts ├── delay.ts ├── factory.ts ├── factory │ ├── DecoratorConfig.ts │ ├── DecoratorFactory.ts │ └── common.ts ├── flip.spec.ts ├── flip.ts ├── flow.spec.ts ├── flow.ts ├── flowRight.spec.ts ├── flowRight.ts ├── index.ts ├── memoize.spec.ts ├── memoize.ts ├── memoizeAll.spec.ts ├── memoizeAll.ts ├── mixin.spec.ts ├── mixin.ts ├── negate.spec.ts ├── negate.ts ├── once.spec.ts ├── once.ts ├── onceAll.ts ├── overArgs.spec.ts ├── overArgs.ts ├── partial.spec.ts ├── partial.ts ├── partialRight.spec.ts ├── partialRight.ts ├── rearg.spec.ts ├── rearg.ts ├── rest.spec.ts ├── rest.ts ├── shared.ts ├── spread.spec.ts ├── spread.ts ├── tap.spec.ts ├── tap.ts ├── throttle.spec.ts ├── throttle.ts ├── throttleAll.ts ├── unary.spec.ts ├── unary.ts ├── utils.ts ├── utils │ ├── CompositeKeyWeakMap.ts │ ├── assignAll.ts │ ├── bind.ts │ ├── copyMetaData.ts │ ├── isDecoratorArgs.spec.ts │ ├── isDecoratorArgs.ts │ ├── isPrototypeAccess.ts │ ├── log.ts │ ├── resolveFunction.ts │ ├── returnAtIndex.ts │ ├── wrapConstructor.spec.ts │ └── wrapConstructor.ts ├── wrap.spec.ts └── wrap.ts ├── tsconfig.build.json ├── tsconfig.docs.json ├── tsconfig.json ├── tsconfig.test.json └── tslint.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.esdoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "source": "./buildDocs", 3 | "destination": "./docs", 4 | "title": "Lodash Decorators Documentation", 5 | "access": [ "public" ], 6 | "excludes": [ ".*\\.spec\\.js" ], 7 | "test": { 8 | "type": "mocha", 9 | "source": "./buildDocs", 10 | "includes": [ ".*\\.spec\\.js" ] 11 | } 12 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | 4 | /*.js 5 | /*.js.map 6 | /*.d.ts 7 | /applicators 8 | /factory 9 | /utils 10 | /test 11 | /buildDocs 12 | src/**/*.js 13 | src/**/*.map 14 | .history 15 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | test 3 | docs 4 | buildDocs 5 | .travis.yml 6 | .gitignore 7 | tsconfig* 8 | mocha.opts 9 | .vscode 10 | tslint.json 11 | .editorconfig 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '8' 4 | script: 5 | - npm test 6 | - npm run build 7 | deploy: 8 | provider: npm 9 | email: steelsojka@gmail.com 10 | skip_cleanup: true 11 | api_key: 12 | secure: InuEwBDb6jFvEz5K2qhyVa1X+24fY/UYoTLONjJ8dzpy/OON7dxJFV4lux6i3N2dB+3og16Ae7pT3O7bX0cYxM2mV8XNYt4ipqve1gKnQFWGFyGM+M6uLBmlNnVe7x8z6kQWx1CS+v47PEQTbqkRq9IyHvzjFi8S5d2YO4kwSwBRKf9Y/ynWh75daaVkMyh2KznA5IyuaFQGzLSDP5JryNRN68pfVzyV4suu7CtNrhSsseTgbecLXXWuj16tP2Lt2KHSgZmzqFNzE20pJPNE8IRTmXV42xuEqiINXXrzMPH0eXQXhRn/6mwDOmqz/1SDPM8HxmW8vVdEfFxXXUsuFIe0EB+CrnVQsv5kEXQV6U3s4boHPUEXxGuXYYaQUh31qmdwEeRndg42z658UoLvdcJF563Ci/mXBqo1CxGAL86dZUB5gLEwuYuXC9RhvsT4W4EzLH1XreABHkgu3zZnQbhfrB860K0NAuA+db6jvg6NFK3ZUB48dEhg4gsYtD99jqpcniwVKWzVYTkUw/uA1vNuOfZhlrzal7/qdlYg+nQSmcD3knqvBNtxmvkYt5EfFH2I6/rwkMUbK1DGAJTVQ6nkh5KeQMsyUfprHS5t6H1rDP3UX41TI4o1gwv6HRPAOHCxKaJs/gKJsbJzo34iuOQ3z7md2zTwmT5dUXGFHSQ= 13 | on: 14 | tags: true 15 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible Node.js debug attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${workspaceRoot}/index.js", 12 | "outFiles": [] 13 | }, 14 | { 15 | "type": "node", 16 | "request": "launch", 17 | "name": "Mocha", 18 | "program": "${workspaceRoot}/node_modules/.bin/_mocha", 19 | "args": ["--opts", "mocha.opts", "--no-timeouts", "--colors"], 20 | "runtimeExecutable": null, 21 | "sourceMaps": true, 22 | "preLaunchTask": "build:test", 23 | "outFiles": [ 24 | "${workspaceRoot}/test/**/*.js" 25 | ] 26 | } 27 | ] 28 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsdk": "node_modules/typescript/lib", 3 | "cSpell.words": [ 4 | "applicate" 5 | ] 6 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "0.1.0", 5 | "command": "npm", 6 | "isShellCommand": true, 7 | "showOutput": "always", 8 | "suppressTaskName": true, 9 | "tasks": [ 10 | { 11 | "taskName": "install", 12 | "args": ["install"] 13 | }, 14 | { 15 | "taskName": "update", 16 | "args": ["update"] 17 | }, 18 | { 19 | "taskName": "test", 20 | "args": ["run", "test"] 21 | }, 22 | { 23 | "taskName": "build:test", 24 | "args": ["run", "build:test"] 25 | }, 26 | { 27 | "taskName": "clean", 28 | "args": ["run", "clean"] 29 | } 30 | ] 31 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | 6 | ## [6.0.1](https://github.com/steelsojka/lodash-decorators/compare/v6.0.0...v6.0.1) (2018-12-22) 7 | 8 | 9 | 10 | 11 | # [6.0.0](https://github.com/steelsojka/lodash-decorators/compare/v5.0.1...v6.0.0) (2018-06-17) 12 | 13 | 14 | ### Bug Fixes 15 | 16 | * **bindAll:** fix constructor not being called with new ([4e72d0c](https://github.com/steelsojka/lodash-decorators/commit/4e72d0c)) 17 | * **decorators:** don't apply instance decorators when accessing from a ([17caeb6](https://github.com/steelsojka/lodash-decorators/commit/17caeb6)) 18 | 19 | 20 | ### BREAKING CHANGES 21 | 22 | * **bindAll:** Properties will use the bind decorator to apply getter/setters 23 | on the prototype instead of the instance. This could cause 24 | issues with consumers currently using the implementain that 25 | assigns the bound properties to the instance through the constructor 26 | rather than on the prototype. The value on the prototype will now be 27 | a getter instead of the original function value. 28 | 29 | 30 | 31 | 32 | ## [5.0.1](https://github.com/steelsojka/lodash-decorators/compare/v5.0.0...v5.0.1) (2018-06-02) 33 | 34 | 35 | 36 | 37 | # [5.0.0](https://github.com/steelsojka/lodash-decorators/compare/v4.5.0...v5.0.0) (2018-03-17) 38 | 39 | 40 | ### Features 41 | 42 | * **all:** decorators with optional arguments do not require invocation ([59a71d7](https://github.com/steelsojka/lodash-decorators/commit/59a71d7)) 43 | * **all:** initial work for paramless decorators ([5300a2e](https://github.com/steelsojka/lodash-decorators/commit/5300a2e)) 44 | 45 | 46 | ### BREAKING CHANGES 47 | 48 | * **all:** This may cause issue with tools that rely on static analysis of the 49 | decorators. Since the deocorators are typed with intersections they must 50 | be removed from a static function wrapper. 51 | 52 | 53 | 54 | 55 | # [4.5.0](https://github.com/steelsojka/lodash-decorators/compare/v4.4.0...v4.5.0) (2017-11-03) 56 | 57 | 58 | ### Bug Fixes 59 | 60 | * **BindAll:** Copy original function name to wrapper ([73b8537](https://github.com/steelsojka/lodash-decorators/commit/73b8537)) 61 | * **factory:** use barrel files instead of node module resolution ([fda1c73](https://github.com/steelsojka/lodash-decorators/commit/fda1c73)) 62 | 63 | 64 | ### Features 65 | 66 | * **packages:** update tslib dependency to `^1.7.1` as this is the minimum version required for `typescript` `2.3.3` + ([c8985fb](https://github.com/steelsojka/lodash-decorators/commit/c8985fb)) 67 | 68 | 69 | 70 | 71 | ## [4.4.1](https://github.com/steelsojka/lodash-decorators/compare/v4.4.0...v4.4.1) (2017-08-12) 72 | 73 | 74 | ### Bug Fixes 75 | 76 | * **factory:** use barrel files instead of node module resolution ([fda1c73](https://github.com/steelsojka/lodash-decorators/commit/fda1c73)) 77 | 78 | 79 | 80 | 81 | # [4.4.0](https://github.com/steelsojka/lodash-decorators/compare/v4.3.5...v4.4.0) (2017-08-06) 82 | 83 | 84 | ### Features 85 | 86 | * add support for Angular 2+ in AOT mode ([d0a602d](https://github.com/steelsojka/lodash-decorators/commit/d0a602d)) 87 | * inline source code in sourceMaps rather than shipping it ([8e75951](https://github.com/steelsojka/lodash-decorators/commit/8e75951)) 88 | 89 | 90 | 91 | 92 | ## [4.3.5](https://github.com/steelsojka/lodash-decorators/compare/v4.3.4...v4.3.5) (2017-06-15) 93 | 94 | 95 | ### Bug Fixes 96 | 97 | * **BindAll:** copy over static properties from base constructor ([489aaa4](https://github.com/steelsojka/lodash-decorators/commit/489aaa4)) 98 | 99 | 100 | 101 | 102 | ## [4.3.4](https://github.com/steelsojka/lodash-decorators/compare/v4.3.3...v4.3.4) (2017-05-22) 103 | 104 | 105 | ### Bug Fixes 106 | 107 | * **sourceMaps:** include src folder in build to not break source maps ([5f2d875](https://github.com/steelsojka/lodash-decorators/commit/5f2d875)) 108 | 109 | 110 | 111 | 112 | ## [4.3.3](https://github.com/steelsojka/lodash-decorators/compare/v4.3.2...v4.3.3) (2017-05-19) 113 | 114 | 115 | ### Bug Fixes 116 | 117 | * **bindAll:** fix bind all should only apply to methods ([4b86629](https://github.com/steelsojka/lodash-decorators/commit/4b86629)) 118 | 119 | 120 | 121 | 122 | ## [4.3.2](https://github.com/steelsojka/lodash-decorators/compare/v4.3.1...v4.3.2) (2017-05-19) 123 | 124 | 125 | ### Bug Fixes 126 | 127 | * **bindAll:** guard against getters that aren't functions ([08fa50b](https://github.com/steelsojka/lodash-decorators/commit/08fa50b)) 128 | 129 | 130 | 131 | 132 | ## [4.3.1](https://github.com/steelsojka/lodash-decorators/compare/v4.3.0...v4.3.1) (2017-05-06) 133 | 134 | 135 | ### Bug Fixes 136 | 137 | * **rearg:** fix typescript build error ([65f8757](https://github.com/steelsojka/lodash-decorators/commit/65f8757)) 138 | 139 | 140 | 141 | 142 | # [4.3.0](https://github.com/steelsojka/lodash-decorators/compare/v4.2.1...v4.3.0) (2017-05-06) 143 | 144 | 145 | ### Features 146 | 147 | * **decorators:** negate, flip, rearg can now be used on properties ([6f951dc](https://github.com/steelsojka/lodash-decorators/commit/6f951dc)) 148 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing 2 | ============ 3 | 4 | Coming soon...? 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Steven Sojka 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /docs/ast/source/applicators/Applicator.js.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "File", 3 | "start": 0, 4 | "end": 28, 5 | "loc": { 6 | "start": { 7 | "line": 1, 8 | "column": 0 9 | }, 10 | "end": { 11 | "line": 3, 12 | "column": 0 13 | } 14 | }, 15 | "program": { 16 | "type": "Program", 17 | "start": 0, 18 | "end": 28, 19 | "loc": { 20 | "start": { 21 | "line": 1, 22 | "column": 0 23 | }, 24 | "end": { 25 | "line": 3, 26 | "column": 0 27 | } 28 | }, 29 | "sourceType": "module", 30 | "body": [ 31 | { 32 | "type": "ExportNamedDeclaration", 33 | "start": 0, 34 | "end": 27, 35 | "loc": { 36 | "start": { 37 | "line": 1, 38 | "column": 0 39 | }, 40 | "end": { 41 | "line": 2, 42 | "column": 1 43 | } 44 | }, 45 | "specifiers": [], 46 | "source": null, 47 | "declaration": { 48 | "type": "ClassDeclaration", 49 | "start": 7, 50 | "end": 27, 51 | "loc": { 52 | "start": { 53 | "line": 1, 54 | "column": 7 55 | }, 56 | "end": { 57 | "line": 2, 58 | "column": 1 59 | } 60 | }, 61 | "id": { 62 | "type": "Identifier", 63 | "start": 13, 64 | "end": 23, 65 | "loc": { 66 | "start": { 67 | "line": 1, 68 | "column": 13 69 | }, 70 | "end": { 71 | "line": 1, 72 | "column": 23 73 | }, 74 | "identifierName": "Applicator" 75 | }, 76 | "name": "Applicator" 77 | }, 78 | "superClass": null, 79 | "body": { 80 | "type": "ClassBody", 81 | "start": 24, 82 | "end": 27, 83 | "loc": { 84 | "start": { 85 | "line": 1, 86 | "column": 24 87 | }, 88 | "end": { 89 | "line": 2, 90 | "column": 1 91 | } 92 | }, 93 | "body": [] 94 | }, 95 | "leadingComments": [], 96 | "trailingComments": [] 97 | } 98 | } 99 | ], 100 | "directives": [] 101 | }, 102 | "comments": [], 103 | "tokens": [ 104 | { 105 | "type": { 106 | "label": "export", 107 | "keyword": "export", 108 | "beforeExpr": false, 109 | "startsExpr": false, 110 | "rightAssociative": false, 111 | "isLoop": false, 112 | "isAssign": false, 113 | "prefix": false, 114 | "postfix": false, 115 | "binop": null, 116 | "updateContext": null 117 | }, 118 | "value": "export", 119 | "start": 0, 120 | "end": 6, 121 | "loc": { 122 | "start": { 123 | "line": 1, 124 | "column": 0 125 | }, 126 | "end": { 127 | "line": 1, 128 | "column": 6 129 | } 130 | } 131 | }, 132 | { 133 | "type": { 134 | "label": "class", 135 | "keyword": "class", 136 | "beforeExpr": false, 137 | "startsExpr": false, 138 | "rightAssociative": false, 139 | "isLoop": false, 140 | "isAssign": false, 141 | "prefix": false, 142 | "postfix": false, 143 | "binop": null, 144 | "updateContext": null 145 | }, 146 | "value": "class", 147 | "start": 7, 148 | "end": 12, 149 | "loc": { 150 | "start": { 151 | "line": 1, 152 | "column": 7 153 | }, 154 | "end": { 155 | "line": 1, 156 | "column": 12 157 | } 158 | } 159 | }, 160 | { 161 | "type": { 162 | "label": "name", 163 | "beforeExpr": false, 164 | "startsExpr": true, 165 | "rightAssociative": false, 166 | "isLoop": false, 167 | "isAssign": false, 168 | "prefix": false, 169 | "postfix": false, 170 | "binop": null 171 | }, 172 | "value": "Applicator", 173 | "start": 13, 174 | "end": 23, 175 | "loc": { 176 | "start": { 177 | "line": 1, 178 | "column": 13 179 | }, 180 | "end": { 181 | "line": 1, 182 | "column": 23 183 | } 184 | } 185 | }, 186 | { 187 | "type": { 188 | "label": "{", 189 | "beforeExpr": true, 190 | "startsExpr": true, 191 | "rightAssociative": false, 192 | "isLoop": false, 193 | "isAssign": false, 194 | "prefix": false, 195 | "postfix": false, 196 | "binop": null 197 | }, 198 | "start": 24, 199 | "end": 25, 200 | "loc": { 201 | "start": { 202 | "line": 1, 203 | "column": 24 204 | }, 205 | "end": { 206 | "line": 1, 207 | "column": 25 208 | } 209 | } 210 | }, 211 | { 212 | "type": { 213 | "label": "}", 214 | "beforeExpr": false, 215 | "startsExpr": false, 216 | "rightAssociative": false, 217 | "isLoop": false, 218 | "isAssign": false, 219 | "prefix": false, 220 | "postfix": false, 221 | "binop": null 222 | }, 223 | "start": 26, 224 | "end": 27, 225 | "loc": { 226 | "start": { 227 | "line": 2, 228 | "column": 0 229 | }, 230 | "end": { 231 | "line": 2, 232 | "column": 1 233 | } 234 | } 235 | }, 236 | { 237 | "type": { 238 | "label": "eof", 239 | "beforeExpr": false, 240 | "startsExpr": false, 241 | "rightAssociative": false, 242 | "isLoop": false, 243 | "isAssign": false, 244 | "prefix": false, 245 | "postfix": false, 246 | "binop": null, 247 | "updateContext": null 248 | }, 249 | "start": 28, 250 | "end": 28, 251 | "loc": { 252 | "start": { 253 | "line": 3, 254 | "column": 0 255 | }, 256 | "end": { 257 | "line": 3, 258 | "column": 0 259 | } 260 | } 261 | } 262 | ] 263 | } -------------------------------------------------------------------------------- /docs/ast/source/shared.js.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "File", 3 | "start": 0, 4 | "end": 0, 5 | "loc": { 6 | "start": { 7 | "line": 1, 8 | "column": 0 9 | }, 10 | "end": { 11 | "line": 1, 12 | "column": 0 13 | } 14 | }, 15 | "program": { 16 | "type": "Program", 17 | "start": 0, 18 | "end": 0, 19 | "loc": { 20 | "start": { 21 | "line": 1, 22 | "column": 0 23 | }, 24 | "end": { 25 | "line": 1, 26 | "column": 0 27 | } 28 | }, 29 | "sourceType": "module", 30 | "body": [], 31 | "directives": [] 32 | }, 33 | "comments": [], 34 | "tokens": [ 35 | { 36 | "type": { 37 | "label": "eof", 38 | "beforeExpr": false, 39 | "startsExpr": false, 40 | "rightAssociative": false, 41 | "isLoop": false, 42 | "isAssign": false, 43 | "prefix": false, 44 | "postfix": false, 45 | "binop": null, 46 | "updateContext": null 47 | }, 48 | "start": 0, 49 | "end": 0, 50 | "loc": { 51 | "start": { 52 | "line": 1, 53 | "column": 0 54 | }, 55 | "end": { 56 | "line": 1, 57 | "column": 0 58 | } 59 | } 60 | } 61 | ] 62 | } -------------------------------------------------------------------------------- /docs/badge.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | document 13 | document 14 | 28% 15 | 28% 16 | 17 | 18 | -------------------------------------------------------------------------------- /docs/coverage.json: -------------------------------------------------------------------------------- 1 | { 2 | "coverage": "28%", 3 | "expectCount": 75, 4 | "actualCount": 21, 5 | "files": { 6 | "buildDocs/after.js": { 7 | "expectCount": 1, 8 | "actualCount": 1, 9 | "undocumentLines": [] 10 | }, 11 | "buildDocs/afterAll.js": { 12 | "expectCount": 1, 13 | "actualCount": 1, 14 | "undocumentLines": [] 15 | }, 16 | "buildDocs/applicators/Applicator.js": { 17 | "expectCount": 1, 18 | "actualCount": 0, 19 | "undocumentLines": [ 20 | 1 21 | ] 22 | }, 23 | "buildDocs/ary.js": { 24 | "expectCount": 1, 25 | "actualCount": 1, 26 | "undocumentLines": [] 27 | }, 28 | "buildDocs/attempt.js": { 29 | "expectCount": 1, 30 | "actualCount": 1, 31 | "undocumentLines": [] 32 | }, 33 | "buildDocs/before.js": { 34 | "expectCount": 1, 35 | "actualCount": 1, 36 | "undocumentLines": [] 37 | }, 38 | "buildDocs/beforeAll.js": { 39 | "expectCount": 1, 40 | "actualCount": 1, 41 | "undocumentLines": [] 42 | }, 43 | "buildDocs/bind.js": { 44 | "expectCount": 1, 45 | "actualCount": 1, 46 | "undocumentLines": [] 47 | }, 48 | "buildDocs/bindAll.js": { 49 | "expectCount": 1, 50 | "actualCount": 1, 51 | "undocumentLines": [] 52 | }, 53 | "buildDocs/applicators/BindApplicator.js": { 54 | "expectCount": 2, 55 | "actualCount": 0, 56 | "undocumentLines": [ 57 | 2, 58 | 3 59 | ] 60 | }, 61 | "buildDocs/applicators/ComposeApplicator.js": { 62 | "expectCount": 4, 63 | "actualCount": 0, 64 | "undocumentLines": [ 65 | 4, 66 | 12, 67 | 5, 68 | 9 69 | ] 70 | }, 71 | "buildDocs/curry.js": { 72 | "expectCount": 1, 73 | "actualCount": 1, 74 | "undocumentLines": [] 75 | }, 76 | "buildDocs/curryAll.js": { 77 | "expectCount": 1, 78 | "actualCount": 1, 79 | "undocumentLines": [] 80 | }, 81 | "buildDocs/curryRight.js": { 82 | "expectCount": 1, 83 | "actualCount": 1, 84 | "undocumentLines": [] 85 | }, 86 | "buildDocs/curryRightAll.js": { 87 | "expectCount": 1, 88 | "actualCount": 1, 89 | "undocumentLines": [] 90 | }, 91 | "buildDocs/debounce.js": { 92 | "expectCount": 1, 93 | "actualCount": 1, 94 | "undocumentLines": [] 95 | }, 96 | "buildDocs/debounceAll.js": { 97 | "expectCount": 1, 98 | "actualCount": 1, 99 | "undocumentLines": [] 100 | }, 101 | "buildDocs/factory/DecoratorConfig.js": { 102 | "expectCount": 10, 103 | "actualCount": 0, 104 | "undocumentLines": [ 105 | 1, 106 | 4, 107 | 7, 108 | 2, 109 | 3, 110 | 13, 111 | 19, 112 | 5, 113 | 16, 114 | 10 115 | ] 116 | }, 117 | "buildDocs/factory/DecoratorFactory.js": { 118 | "expectCount": 5, 119 | "actualCount": 0, 120 | "undocumentLines": [ 121 | 125, 122 | 4, 123 | 105, 124 | 5, 125 | 28 126 | ] 127 | }, 128 | "buildDocs/defer.js": { 129 | "expectCount": 1, 130 | "actualCount": 1, 131 | "undocumentLines": [] 132 | }, 133 | "buildDocs/delay.js": { 134 | "expectCount": 1, 135 | "actualCount": 1, 136 | "undocumentLines": [] 137 | }, 138 | "buildDocs/flip.js": { 139 | "expectCount": 1, 140 | "actualCount": 1, 141 | "undocumentLines": [] 142 | }, 143 | "buildDocs/flow.js": { 144 | "expectCount": 1, 145 | "actualCount": 1, 146 | "undocumentLines": [] 147 | }, 148 | "buildDocs/flowRight.js": { 149 | "expectCount": 1, 150 | "actualCount": 1, 151 | "undocumentLines": [] 152 | }, 153 | "buildDocs/factory/common.js": { 154 | "expectCount": 1, 155 | "actualCount": 0, 156 | "undocumentLines": [ 157 | 2 158 | ] 159 | }, 160 | "buildDocs/applicators/InvokeApplicator.js": { 161 | "expectCount": 2, 162 | "actualCount": 0, 163 | "undocumentLines": [ 164 | 2, 165 | 3 166 | ] 167 | }, 168 | "buildDocs/memoize.js": { 169 | "expectCount": 1, 170 | "actualCount": 0, 171 | "undocumentLines": [ 172 | 4 173 | ] 174 | }, 175 | "buildDocs/memoizeAll.js": { 176 | "expectCount": 1, 177 | "actualCount": 1, 178 | "undocumentLines": [] 179 | }, 180 | "buildDocs/applicators/MemoizeApplicator.js": { 181 | "expectCount": 2, 182 | "actualCount": 0, 183 | "undocumentLines": [ 184 | 4, 185 | 5 186 | ] 187 | }, 188 | "buildDocs/mixin.js": { 189 | "expectCount": 1, 190 | "actualCount": 0, 191 | "undocumentLines": [ 192 | 2 193 | ] 194 | }, 195 | "buildDocs/negate.js": { 196 | "expectCount": 1, 197 | "actualCount": 0, 198 | "undocumentLines": [ 199 | 4 200 | ] 201 | }, 202 | "buildDocs/once.js": { 203 | "expectCount": 1, 204 | "actualCount": 0, 205 | "undocumentLines": [ 206 | 4 207 | ] 208 | }, 209 | "buildDocs/onceAll.js": { 210 | "expectCount": 1, 211 | "actualCount": 0, 212 | "undocumentLines": [ 213 | 4 214 | ] 215 | }, 216 | "buildDocs/overArgs.js": { 217 | "expectCount": 1, 218 | "actualCount": 0, 219 | "undocumentLines": [ 220 | 4 221 | ] 222 | }, 223 | "buildDocs/partial.js": { 224 | "expectCount": 1, 225 | "actualCount": 0, 226 | "undocumentLines": [ 227 | 4 228 | ] 229 | }, 230 | "buildDocs/applicators/PartialApplicator.js": { 231 | "expectCount": 2, 232 | "actualCount": 0, 233 | "undocumentLines": [ 234 | 3, 235 | 4 236 | ] 237 | }, 238 | "buildDocs/partialRight.js": { 239 | "expectCount": 1, 240 | "actualCount": 0, 241 | "undocumentLines": [ 242 | 4 243 | ] 244 | }, 245 | "buildDocs/applicators/PartialedApplicator.js": { 246 | "expectCount": 2, 247 | "actualCount": 0, 248 | "undocumentLines": [ 249 | 3, 250 | 4 251 | ] 252 | }, 253 | "buildDocs/applicators/PostValueApplicator.js": { 254 | "expectCount": 2, 255 | "actualCount": 0, 256 | "undocumentLines": [ 257 | 2, 258 | 3 259 | ] 260 | }, 261 | "buildDocs/applicators/PreValueApplicator.js": { 262 | "expectCount": 2, 263 | "actualCount": 0, 264 | "undocumentLines": [ 265 | 2, 266 | 3 267 | ] 268 | }, 269 | "buildDocs/rearg.js": { 270 | "expectCount": 1, 271 | "actualCount": 0, 272 | "undocumentLines": [ 273 | 4 274 | ] 275 | }, 276 | "buildDocs/rest.js": { 277 | "expectCount": 1, 278 | "actualCount": 0, 279 | "undocumentLines": [ 280 | 4 281 | ] 282 | }, 283 | "buildDocs/spread.js": { 284 | "expectCount": 1, 285 | "actualCount": 0, 286 | "undocumentLines": [ 287 | 4 288 | ] 289 | }, 290 | "buildDocs/tap.js": { 291 | "expectCount": 1, 292 | "actualCount": 1, 293 | "undocumentLines": [] 294 | }, 295 | "buildDocs/throttle.js": { 296 | "expectCount": 3, 297 | "actualCount": 0, 298 | "undocumentLines": [ 299 | 4, 300 | 5, 301 | 6 302 | ] 303 | }, 304 | "buildDocs/throttleAll.js": { 305 | "expectCount": 1, 306 | "actualCount": 0, 307 | "undocumentLines": [ 308 | 4 309 | ] 310 | }, 311 | "buildDocs/unary.js": { 312 | "expectCount": 1, 313 | "actualCount": 0, 314 | "undocumentLines": [ 315 | 4 316 | ] 317 | }, 318 | "buildDocs/wrap.js": { 319 | "expectCount": 1, 320 | "actualCount": 0, 321 | "undocumentLines": [ 322 | 4 323 | ] 324 | }, 325 | "buildDocs/applicators/WrapApplicator.js": { 326 | "expectCount": 2, 327 | "actualCount": 0, 328 | "undocumentLines": [ 329 | 3, 330 | 4 331 | ] 332 | } 333 | } 334 | } -------------------------------------------------------------------------------- /docs/css/prettify-tomorrow.css: -------------------------------------------------------------------------------- 1 | /* Tomorrow Theme */ 2 | /* Original theme - https://github.com/chriskempson/tomorrow-theme */ 3 | /* Pretty printing styles. Used with prettify.js. */ 4 | /* SPAN elements with the classes below are added by prettyprint. */ 5 | /* plain text */ 6 | .pln { 7 | color: #4d4d4c; } 8 | 9 | @media screen { 10 | /* string content */ 11 | .str { 12 | color: #718c00; } 13 | 14 | /* a keyword */ 15 | .kwd { 16 | color: #8959a8; } 17 | 18 | /* a comment */ 19 | .com { 20 | color: #8e908c; } 21 | 22 | /* a type name */ 23 | .typ { 24 | color: #4271ae; } 25 | 26 | /* a literal value */ 27 | .lit { 28 | color: #f5871f; } 29 | 30 | /* punctuation */ 31 | .pun { 32 | color: #4d4d4c; } 33 | 34 | /* lisp open bracket */ 35 | .opn { 36 | color: #4d4d4c; } 37 | 38 | /* lisp close bracket */ 39 | .clo { 40 | color: #4d4d4c; } 41 | 42 | /* a markup tag name */ 43 | .tag { 44 | color: #c82829; } 45 | 46 | /* a markup attribute name */ 47 | .atn { 48 | color: #f5871f; } 49 | 50 | /* a markup attribute value */ 51 | .atv { 52 | color: #3e999f; } 53 | 54 | /* a declaration */ 55 | .dec { 56 | color: #f5871f; } 57 | 58 | /* a variable name */ 59 | .var { 60 | color: #c82829; } 61 | 62 | /* a function name */ 63 | .fun { 64 | color: #4271ae; } } 65 | /* Use higher contrast and text-weight for printable form. */ 66 | @media print, projection { 67 | .str { 68 | color: #060; } 69 | 70 | .kwd { 71 | color: #006; 72 | font-weight: bold; } 73 | 74 | .com { 75 | color: #600; 76 | font-style: italic; } 77 | 78 | .typ { 79 | color: #404; 80 | font-weight: bold; } 81 | 82 | .lit { 83 | color: #044; } 84 | 85 | .pun, .opn, .clo { 86 | color: #440; } 87 | 88 | .tag { 89 | color: #006; 90 | font-weight: bold; } 91 | 92 | .atn { 93 | color: #404; } 94 | 95 | .atv { 96 | color: #060; } } 97 | /* Style */ 98 | /* 99 | pre.prettyprint { 100 | background: white; 101 | font-family: Consolas, Monaco, 'Andale Mono', monospace; 102 | font-size: 12px; 103 | line-height: 1.5; 104 | border: 1px solid #ccc; 105 | padding: 10px; } 106 | */ 107 | 108 | /* Specify class=linenums on a pre to get line numbering */ 109 | ol.linenums { 110 | margin-top: 0; 111 | margin-bottom: 0; } 112 | 113 | /* IE indents via margin-left */ 114 | li.L0, 115 | li.L1, 116 | li.L2, 117 | li.L3, 118 | li.L4, 119 | li.L5, 120 | li.L6, 121 | li.L7, 122 | li.L8, 123 | li.L9 { 124 | /* */ } 125 | 126 | /* Alternate shading for lines */ 127 | li.L1, 128 | li.L3, 129 | li.L5, 130 | li.L7, 131 | li.L9 { 132 | /* */ } 133 | -------------------------------------------------------------------------------- /docs/file/buildDocs/applicators/Applicator.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | buildDocs/applicators/Applicator.js | Lodash Decorators Documentation API Document 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | Home 18 | 19 | Reference 20 | Source 21 | Test 22 | Repository 23 | 30 |
31 | 32 | 90 | 91 |

buildDocs/applicators/Applicator.js

92 |
export class Applicator {
 93 | }
 94 | 
95 | 96 |
97 | 98 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /docs/file/buildDocs/factory/common.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | buildDocs/factory/common.js | Lodash Decorators Documentation API Document 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | Home 18 | 19 | Reference 20 | Source 21 | Test 22 | Repository 23 | 30 |
31 | 32 | 90 | 91 |

buildDocs/factory/common.js

92 |
import { CompositeKeyWeakMap } from '../utils';
 93 | export const InstanceChainMap = new CompositeKeyWeakMap();
 94 | 
95 | 96 |
97 | 98 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /docs/file/buildDocs/shared.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | buildDocs/shared.js | Lodash Decorators Documentation API Document 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | Home 18 | 19 | Reference 20 | Source 21 | Test 22 | Repository 23 | 30 |
31 | 32 | 90 | 91 |

buildDocs/shared.js

92 |

 93 | 

Sorry, this documentation does not provide source code.

94 |
95 | 96 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /docs/image/badge.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | document 13 | document 14 | @ratio@ 15 | @ratio@ 16 | 17 | 18 | -------------------------------------------------------------------------------- /docs/image/esdoc-logo-mini-black.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steelsojka/lodash-decorators/a30cbd16ea42d8f48c4c4a28eb40ec5230ac65bb/docs/image/esdoc-logo-mini-black.png -------------------------------------------------------------------------------- /docs/image/esdoc-logo-mini.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steelsojka/lodash-decorators/a30cbd16ea42d8f48c4c4a28eb40ec5230ac65bb/docs/image/esdoc-logo-mini.png -------------------------------------------------------------------------------- /docs/image/github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steelsojka/lodash-decorators/a30cbd16ea42d8f48c4c4a28eb40ec5230ac65bb/docs/image/github.png -------------------------------------------------------------------------------- /docs/image/manual-badge.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | manual 13 | manual 14 | @value@ 15 | @value@ 16 | 17 | 18 | -------------------------------------------------------------------------------- /docs/image/search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steelsojka/lodash-decorators/a30cbd16ea42d8f48c4c4a28eb40ec5230ac65bb/docs/image/search.png -------------------------------------------------------------------------------- /docs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lodash-decorators", 3 | "author": "Steven Sojka ", 4 | "description": "A collection of decorators using lodash at it's core.", 5 | "version": "4.1.0", 6 | "engines": { 7 | "node": ">=0.12.0" 8 | }, 9 | "scripts": { 10 | "clean": "npm run build:clean; npm run build:docs:clean; npm run test:clean", 11 | "pretest": "npm run build:test", 12 | "test": "mocha --opts mocha.opts", 13 | "test:clean": "del test", 14 | "posttest": "npm run test:clean", 15 | "prebuild": "npm run build:clean", 16 | "prebuild:test": "npm run test:clean", 17 | "prebuild:docs": "npm run build:docs:clean", 18 | "build:clean": "del '*.js' '*.js.map' '*.d.ts' applicators factory utils", 19 | "build:docs:clean": "del buildDocs", 20 | "build": "tsc -p tsconfig.build.json", 21 | "build:test": "tsc -p tsconfig.test.json", 22 | "build:docs": "tsc -p tsconfig.docs.json", 23 | "predocs": "npm run build:docs", 24 | "postdocs": "npm run build:docs:clean", 25 | "docs": "del docs && esdoc", 26 | "docs:serve": "http-server ./docs", 27 | "docs:publish": "git subtree push --prefix docs origin gh-pages", 28 | "readme": "doctoc ./README.md", 29 | "release": "npm run build && npm run docs && npm run readme" 30 | }, 31 | "main": "index.js", 32 | "keywords": [ 33 | "lodash", 34 | "decorators", 35 | "ES7", 36 | "ES2016", 37 | "utility" 38 | ], 39 | "license": "MIT", 40 | "bugs": { 41 | "url": "http://github.com/steelsojka/lodash-decorators/issues" 42 | }, 43 | "repository": { 44 | "type": "git", 45 | "url": "http://github.com/steelsojka/lodash-decorators" 46 | }, 47 | "devDependencies": { 48 | "@types/chai": "^3.5.1", 49 | "@types/lodash": "^4.14.63", 50 | "@types/mocha": "^2.2.41", 51 | "@types/node": "^7.0.13", 52 | "@types/sinon": "^2.1.3", 53 | "chai": "~2.2.0", 54 | "del-cli": "^0.2.1", 55 | "doctoc": "^1.3.0", 56 | "esdoc": "^0.5.2", 57 | "http-server": "^0.9.0", 58 | "mocha": "^3.3.0", 59 | "sinon": "~1.14.1", 60 | "ts-node": "^3.0.2", 61 | "typescript": "^2.2.2" 62 | }, 63 | "dependencies": { 64 | "lodash": "^4.0.0", 65 | "tslib": "^1.6.1" 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /docs/script/inherited-summary.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | function toggle(ev) { 3 | var button = ev.target; 4 | var parent = ev.target.parentElement; 5 | while(parent) { 6 | if (parent.tagName === 'TABLE' && parent.classList.contains('summary')) break; 7 | parent = parent.parentElement; 8 | } 9 | 10 | if (!parent) return; 11 | 12 | var tbody = parent.querySelector('tbody'); 13 | if (button.classList.contains('opened')) { 14 | button.classList.remove('opened'); 15 | button.classList.add('closed'); 16 | tbody.style.display = 'none'; 17 | } else { 18 | button.classList.remove('closed'); 19 | button.classList.add('opened'); 20 | tbody.style.display = 'block'; 21 | } 22 | } 23 | 24 | var buttons = document.querySelectorAll('.inherited-summary thead .toggle'); 25 | for (var i = 0; i < buttons.length; i++) { 26 | buttons[i].addEventListener('click', toggle); 27 | } 28 | })(); 29 | -------------------------------------------------------------------------------- /docs/script/inner-link.js: -------------------------------------------------------------------------------- 1 | // inner link(#foo) can not correctly scroll, because page has fixed header, 2 | // so, I manually scroll. 3 | (function(){ 4 | var matched = location.hash.match(/errorLines=([\d,]+)/); 5 | if (matched) return; 6 | 7 | function adjust() { 8 | window.scrollBy(0, -55); 9 | var el = document.querySelector('.inner-link-active'); 10 | if (el) el.classList.remove('inner-link-active'); 11 | 12 | // ``[ ] . ' " @`` are not valid in DOM id. so must escape these. 13 | var id = location.hash.replace(/([\[\].'"@$])/g, '\\$1'); 14 | var el = document.querySelector(id); 15 | if (el) el.classList.add('inner-link-active'); 16 | } 17 | 18 | window.addEventListener('hashchange', adjust); 19 | 20 | if (location.hash) { 21 | setTimeout(adjust, 0); 22 | } 23 | })(); 24 | 25 | (function(){ 26 | var els = document.querySelectorAll('[href^="#"]'); 27 | for (var i = 0; i < els.length; i++) { 28 | var el = els[i]; 29 | el.href = location.href + el.getAttribute('href'); // because el.href is absolute path 30 | } 31 | })(); 32 | -------------------------------------------------------------------------------- /docs/script/manual.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | var matched = location.pathname.match(/\/(manual\/.*?\/.*\.html)$/); 3 | if (!matched) return; 4 | 5 | var currentName = matched[1]; 6 | var cssClass = '.navigation .manual-toc li[data-link="' + currentName + '"]'; 7 | var styleText = cssClass + '{ display: block; }\n'; 8 | var style = document.createElement('style'); 9 | style.textContent = styleText; 10 | document.querySelector('head').appendChild(style); 11 | })(); 12 | -------------------------------------------------------------------------------- /docs/script/patch-for-local.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | if (location.protocol === 'file:') { 3 | var elms = document.querySelectorAll('a[href="./"]'); 4 | for (var i = 0; i < elms.length; i++) { 5 | elms[i].href = './index.html'; 6 | } 7 | } 8 | })(); 9 | -------------------------------------------------------------------------------- /docs/script/prettify/Apache-License-2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /docs/script/pretty-print.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | prettyPrint(); 3 | var lines = document.querySelectorAll('.prettyprint.linenums li[class^="L"]'); 4 | for (var i = 0; i < lines.length; i++) { 5 | lines[i].id = 'lineNumber' + (i + 1); 6 | } 7 | 8 | var matched = location.hash.match(/errorLines=([\d,]+)/); 9 | if (matched) { 10 | var lines = matched[1].split(','); 11 | for (var i = 0; i < lines.length; i++) { 12 | var id = '#lineNumber' + lines[i]; 13 | var el = document.querySelector(id); 14 | el.classList.add('error-line'); 15 | } 16 | return; 17 | } 18 | 19 | if (location.hash) { 20 | // ``[ ] . ' " @`` are not valid in DOM id. so must escape these. 21 | var id = location.hash.replace(/([\[\].'"@$])/g, '\\$1'); 22 | var line = document.querySelector(id); 23 | if (line) line.classList.add('active'); 24 | } 25 | })(); 26 | -------------------------------------------------------------------------------- /docs/script/search.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | var searchIndex = window.esdocSearchIndex; 3 | var searchBox = document.querySelector('.search-box'); 4 | var input = document.querySelector('.search-input'); 5 | var result = document.querySelector('.search-result'); 6 | var selectedIndex = -1; 7 | var prevText; 8 | 9 | // active search box and focus when mouse enter on search box. 10 | searchBox.addEventListener('mouseenter', function(){ 11 | searchBox.classList.add('active'); 12 | input.focus(); 13 | }); 14 | 15 | // search with text when key is upped. 16 | input.addEventListener('keyup', function(ev){ 17 | var text = ev.target.value.toLowerCase(); 18 | if (!text) { 19 | result.style.display = 'none'; 20 | result.innerHTML = ''; 21 | return; 22 | } 23 | 24 | if (text === prevText) return; 25 | prevText = text; 26 | 27 | var html = {class: [], method: [], member: [], function: [], variable: [], typedef: [], external: [], file: [], test: [], testFile: []}; 28 | var len = searchIndex.length; 29 | var kind; 30 | for (var i = 0; i < len; i++) { 31 | var pair = searchIndex[i]; 32 | if (pair[0].indexOf(text) !== -1) { 33 | kind = pair[3]; 34 | html[kind].push('
  • ' + pair[2] + '
  • '); 35 | } 36 | } 37 | 38 | var innerHTML = ''; 39 | for (kind in html) { 40 | var list = html[kind]; 41 | if (!list.length) continue; 42 | innerHTML += '
  • ' + kind + '
  • \n' + list.join('\n'); 43 | } 44 | result.innerHTML = innerHTML; 45 | if (innerHTML) result.style.display = 'block'; 46 | selectedIndex = -1; 47 | }); 48 | 49 | // down, up and enter key are pressed, select search result. 50 | input.addEventListener('keydown', function(ev){ 51 | if (ev.keyCode === 40) { 52 | // arrow down 53 | var current = result.children[selectedIndex]; 54 | var selected = result.children[selectedIndex + 1]; 55 | if (selected && selected.classList.contains('search-separator')) { 56 | var selected = result.children[selectedIndex + 2]; 57 | selectedIndex++; 58 | } 59 | 60 | if (selected) { 61 | if (current) current.classList.remove('selected'); 62 | selectedIndex++; 63 | selected.classList.add('selected'); 64 | } 65 | } else if (ev.keyCode === 38) { 66 | // arrow up 67 | var current = result.children[selectedIndex]; 68 | var selected = result.children[selectedIndex - 1]; 69 | if (selected && selected.classList.contains('search-separator')) { 70 | var selected = result.children[selectedIndex - 2]; 71 | selectedIndex--; 72 | } 73 | 74 | if (selected) { 75 | if (current) current.classList.remove('selected'); 76 | selectedIndex--; 77 | selected.classList.add('selected'); 78 | } 79 | } else if (ev.keyCode === 13) { 80 | // enter 81 | var current = result.children[selectedIndex]; 82 | if (current) { 83 | var link = current.querySelector('a'); 84 | if (link) location.href = link.href; 85 | } 86 | } else { 87 | return; 88 | } 89 | 90 | ev.preventDefault(); 91 | }); 92 | 93 | // select search result when search result is mouse over. 94 | result.addEventListener('mousemove', function(ev){ 95 | var current = result.children[selectedIndex]; 96 | if (current) current.classList.remove('selected'); 97 | 98 | var li = ev.target; 99 | while (li) { 100 | if (li.nodeName === 'LI') break; 101 | li = li.parentElement; 102 | } 103 | 104 | if (li) { 105 | selectedIndex = Array.prototype.indexOf.call(result.children, li); 106 | li.classList.add('selected'); 107 | } 108 | }); 109 | 110 | // clear search result when body is clicked. 111 | document.body.addEventListener('click', function(ev){ 112 | selectedIndex = -1; 113 | result.style.display = 'none'; 114 | result.innerHTML = ''; 115 | }); 116 | 117 | })(); 118 | -------------------------------------------------------------------------------- /docs/script/test-summary.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | function toggle(ev) { 3 | var button = ev.target; 4 | var parent = ev.target.parentElement; 5 | while(parent) { 6 | if (parent.tagName === 'TR' && parent.classList.contains('test-describe')) break; 7 | parent = parent.parentElement; 8 | } 9 | 10 | if (!parent) return; 11 | 12 | var direction; 13 | if (button.classList.contains('opened')) { 14 | button.classList.remove('opened'); 15 | button.classList.add('closed'); 16 | direction = 'closed'; 17 | } else { 18 | button.classList.remove('closed'); 19 | button.classList.add('opened'); 20 | direction = 'opened'; 21 | } 22 | 23 | var targetDepth = parseInt(parent.dataset.testDepth, 10) + 1; 24 | var nextElement = parent.nextElementSibling; 25 | while (nextElement) { 26 | var depth = parseInt(nextElement.dataset.testDepth, 10); 27 | if (depth >= targetDepth) { 28 | if (direction === 'opened') { 29 | if (depth === targetDepth) nextElement.style.display = ''; 30 | } else if (direction === 'closed') { 31 | nextElement.style.display = 'none'; 32 | var innerButton = nextElement.querySelector('.toggle'); 33 | if (innerButton && innerButton.classList.contains('opened')) { 34 | innerButton.classList.remove('opened'); 35 | innerButton.classList.add('closed'); 36 | } 37 | } 38 | } else { 39 | break; 40 | } 41 | nextElement = nextElement.nextElementSibling; 42 | } 43 | } 44 | 45 | var buttons = document.querySelectorAll('.test-summary tr.test-describe .toggle'); 46 | for (var i = 0; i < buttons.length; i++) { 47 | buttons[i].addEventListener('click', toggle); 48 | } 49 | 50 | var topDescribes = document.querySelectorAll('.test-summary tr[data-test-depth="0"]'); 51 | for (var i = 0; i < topDescribes.length; i++) { 52 | topDescribes[i].style.display = ''; 53 | } 54 | })(); 55 | -------------------------------------------------------------------------------- /mocha.opts: -------------------------------------------------------------------------------- 1 | --reporter spec 2 | --watch-extensions ts 3 | test/**/*.spec.js -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lodash-decorators", 3 | "author": "Steven Sojka ", 4 | "description": "A collection of decorators using lodash at it's core.", 5 | "version": "6.0.1", 6 | "engines": { 7 | "node": ">=0.12.0" 8 | }, 9 | "scripts": { 10 | "clean": "npm run build:clean; npm run build:docs:clean; npm run test:clean", 11 | "pretest": "npm run build:test", 12 | "test": "mocha --opts mocha.opts", 13 | "test:clean": "del test", 14 | "posttest": "npm run test:clean", 15 | "prebuild": "npm run lint && npm run build:clean", 16 | "prebuild:test": "npm run test:clean", 17 | "prebuild:docs": "npm run build:docs:clean", 18 | "build:clean": "del '*.js' '*.js.map' '*.d.ts' applicators factory utils", 19 | "build:docs:clean": "del buildDocs", 20 | "build": "tsc -p tsconfig.build.json", 21 | "build:test": "tsc -p tsconfig.test.json", 22 | "build:docs": "tsc -p tsconfig.docs.json", 23 | "predocs": "npm run build:docs", 24 | "postdocs": "npm run build:docs:clean", 25 | "docs": "del docs && esdoc", 26 | "docs:serve": "http-server ./docs", 27 | "docs:publish": "git subtree push --prefix docs origin gh-pages", 28 | "readme": "doctoc ./README.md", 29 | "release": "npm run build && npm run docs && npm run readme", 30 | "lint": "tslint src/**", 31 | "lint:fix": "tslint src/** --fix", 32 | "version": "standard-version" 33 | }, 34 | "main": "index.js", 35 | "typings": "index.d.ts", 36 | "keywords": [ 37 | "lodash", 38 | "decorators", 39 | "ES7", 40 | "ES2016", 41 | "utility" 42 | ], 43 | "license": "MIT", 44 | "bugs": { 45 | "url": "http://github.com/steelsojka/lodash-decorators/issues" 46 | }, 47 | "repository": { 48 | "type": "git", 49 | "url": "http://github.com/steelsojka/lodash-decorators" 50 | }, 51 | "devDependencies": { 52 | "@types/chai": "^3.5.1", 53 | "@types/lodash": "^4.14.109", 54 | "@types/mocha": "^2.2.48", 55 | "@types/node": "^7.0.65", 56 | "@types/sinon": "^2.3.7", 57 | "chai": "~2.2.0", 58 | "del-cli": "^0.2.1", 59 | "doctoc": "^1.3.1", 60 | "esdoc": "^1.1.0", 61 | "http-server": "^0.11.1", 62 | "lodash": "^4.17.10", 63 | "mocha": "^5.2.0", 64 | "sinon": "~1.14.1", 65 | "standard-version": "^8.0.1", 66 | "ts-node": "^3.0.2", 67 | "tslint": "^5.10.0", 68 | "tslint-language-service": "^0.9.9", 69 | "typescript": "~2.3.4" 70 | }, 71 | "peerDependencies": { 72 | "lodash": "4.x" 73 | }, 74 | "dependencies": { 75 | "tslib": "^1.9.2" 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/after.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { After } from './after'; 4 | 5 | describe('after', () => { 6 | it('should invoke the method after 3 times', () => { 7 | let calls = 0; 8 | 9 | class MyClass { 10 | props: number[] = []; 11 | 12 | @After(2) 13 | set prop(val: number) { 14 | this.props.push(val); 15 | } 16 | 17 | @After(3) 18 | fn() { 19 | calls++; 20 | } 21 | } 22 | 23 | const myClass = new MyClass(); 24 | const myClass2 = new MyClass(); 25 | 26 | myClass.fn(); 27 | myClass.fn(); 28 | myClass.fn(); 29 | myClass.fn(); 30 | 31 | expect(calls, 'single class').to.equal(2); 32 | 33 | myClass2.fn(); 34 | myClass2.fn(); 35 | myClass2.fn(); 36 | myClass2.fn(); 37 | 38 | expect(calls, 'multiple class').to.equal(4); 39 | 40 | myClass.prop = 50; 41 | myClass.prop = 100; 42 | 43 | expect(myClass.props.length, 'setter length').to.equal(1); 44 | expect(myClass.props[0], 'setter value').to.equal(100); 45 | }); 46 | }); -------------------------------------------------------------------------------- /src/after.ts: -------------------------------------------------------------------------------- 1 | import after = require('lodash/after'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashDecorator } from './factory'; 4 | import { PostValueApplicator } from './applicators'; 5 | 6 | const decorator = DecoratorFactory.createInstanceDecorator( 7 | new DecoratorConfig(after, new PostValueApplicator(), { setter: true }) 8 | ); 9 | 10 | /** 11 | * The opposite of Before. This method creates a function that invokes once it's called n or more times. 12 | * @param {number} n The number of calls before the function is invoked. 13 | * @example 14 | * 15 | * class MyClass { 16 | * @After(2) 17 | * fn() { 18 | * return 10; 19 | * } 20 | * } 21 | * 22 | * const myClass = new MyClass(); 23 | * 24 | * myClass.fn(); // => undefined 25 | * myClass.fn(); // => 10 26 | */ 27 | export function After(n: number): LodashDecorator { 28 | return decorator(n); 29 | } 30 | export { After as after }; 31 | export default decorator; 32 | -------------------------------------------------------------------------------- /src/afterAll.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { AfterAll } from './afterAll'; 4 | 5 | describe('afterAll', () => { 6 | it('should invoke the method after 3 times', () => { 7 | let calls = 0; 8 | 9 | class MyClass { 10 | @AfterAll(3) 11 | fn() { 12 | calls++; 13 | } 14 | } 15 | 16 | const myClass = new MyClass(); 17 | const myClass2 = new MyClass(); 18 | 19 | myClass.fn(); 20 | myClass.fn(); 21 | myClass.fn(); 22 | myClass.fn(); 23 | 24 | expect(calls, 'single class').to.equal(2); 25 | 26 | myClass2.fn(); 27 | myClass2.fn(); 28 | myClass2.fn(); 29 | myClass2.fn(); 30 | 31 | expect(calls, 'multiple class').to.equal(6); 32 | }); 33 | }); -------------------------------------------------------------------------------- /src/afterAll.ts: -------------------------------------------------------------------------------- 1 | import after = require('lodash/after'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashDecorator } from './factory'; 4 | import { PostValueApplicator } from './applicators'; 5 | 6 | const decorator = DecoratorFactory.createDecorator( 7 | new DecoratorConfig(after, new PostValueApplicator(), { setter: true }) 8 | ); 9 | 10 | /** 11 | * The opposite of Before. This method creates a function that invokes once it's called n or more times. 12 | * This spans across all instances of the class instead of the instance. 13 | * @param {number} n The number of calls before the function is invoked. 14 | * @example 15 | * 16 | * class MyClass { 17 | * @AfterAll(2) 18 | * fn() { 19 | * return 10; 20 | * } 21 | * } 22 | * 23 | * const myClass = new MyClass(); 24 | * const myClass2 = new MyClass(); 25 | * 26 | * myClass.fn(); // => undefined 27 | * myClass.fn(); // => 10 28 | * 29 | * myClass2.fn(); // => 10 30 | * myClass2.fn(); // => 10 31 | */ 32 | export function AfterAll(n: number): LodashDecorator { 33 | return decorator(n); 34 | } 35 | export { AfterAll as afterAll }; 36 | export default decorator; 37 | -------------------------------------------------------------------------------- /src/applicators.ts: -------------------------------------------------------------------------------- 1 | export * from './applicators/Applicator'; 2 | export * from './applicators/ComposeApplicator'; 3 | export * from './applicators/PartialApplicator'; 4 | export * from './applicators/PartialedApplicator'; 5 | export * from './applicators/PartialValueApplicator'; 6 | export * from './applicators/PostValueApplicator'; 7 | export * from './applicators/PreValueApplicator'; 8 | export * from './applicators/WrapApplicator'; 9 | export * from './applicators/BindApplicator'; 10 | export * from './applicators/InvokeApplicator'; 11 | export * from './applicators/MemoizeApplicator'; 12 | -------------------------------------------------------------------------------- /src/applicators/Applicator.ts: -------------------------------------------------------------------------------- 1 | import { ApplicateOptions } from '../shared'; 2 | 3 | export { ApplicateOptions } from '../shared'; 4 | 5 | export abstract class Applicator { 6 | abstract apply(options: ApplicateOptions): any; 7 | } -------------------------------------------------------------------------------- /src/applicators/BindApplicator.ts: -------------------------------------------------------------------------------- 1 | import { Applicator, ApplicateOptions } from './Applicator'; 2 | 3 | export class BindApplicator extends Applicator { 4 | apply({ value, config: { execute } , args, instance, target }: ApplicateOptions): any { 5 | if (!instance) { 6 | return value; 7 | } 8 | 9 | return execute(value, instance, ...args); 10 | } 11 | } -------------------------------------------------------------------------------- /src/applicators/ComposeApplicator.ts: -------------------------------------------------------------------------------- 1 | import identity = require('lodash/identity'); 2 | 3 | import { Applicator, ApplicateOptions } from './Applicator'; 4 | import { resolveFunction } from '../utils'; 5 | 6 | export class ComposeApplicator extends Applicator { 7 | constructor(private _config: { post?: boolean } = {}) { 8 | super(); 9 | } 10 | 11 | get post(): boolean { 12 | return this._config.post === true; 13 | } 14 | 15 | apply({ config: { execute }, value = identity, args, target }: ApplicateOptions): any { 16 | const applicator = this; 17 | 18 | return function(...invokeArgs: any[]): any { 19 | const _args = [ 20 | ...args.map(method => resolveFunction(method, this, target)) 21 | ]; 22 | 23 | if (applicator.post) { 24 | _args.push(value); 25 | } else { 26 | _args.unshift(value); 27 | } 28 | 29 | return execute(..._args).apply(this, invokeArgs); 30 | }; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/applicators/InvokeApplicator.ts: -------------------------------------------------------------------------------- 1 | import { Applicator, ApplicateOptions } from './Applicator'; 2 | 3 | export class InvokeApplicator extends Applicator { 4 | apply({ args, target, config: { execute }, value }: ApplicateOptions): any { 5 | return function(...invokeArgs: any[]): any { 6 | return execute(value.bind(this), ...invokeArgs, ...args); 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/applicators/MemoizeApplicator.ts: -------------------------------------------------------------------------------- 1 | import isFunction = require('lodash/isFunction'); 2 | import isObject = require('lodash/isObject'); 3 | 4 | import { Applicator, ApplicateOptions } from './Applicator'; 5 | import { resolveFunction } from '../utils'; 6 | 7 | export class MemoizeApplicator extends Applicator { 8 | apply({ value, instance, config: { execute }, args, target }: ApplicateOptions): any { 9 | let resolver = resolveFunction( 10 | isFunction(args[0]) ? args[0] : isObject(args[0]) ? args[0].resolver : args[0], 11 | instance, 12 | target, 13 | false 14 | ); 15 | 16 | if (resolver && instance) { 17 | resolver = resolver.bind(instance); 18 | } 19 | 20 | const memoized = resolver ? execute(value, resolver) : execute(value); 21 | 22 | if (isObject(args[0])) { 23 | const { cache, type } = args[0]; 24 | 25 | if (cache) { 26 | memoized.cache = cache; 27 | } else if (isFunction(type)) { 28 | memoized.cache = new (type as any)(); 29 | } 30 | } 31 | 32 | return memoized; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/applicators/PartialApplicator.ts: -------------------------------------------------------------------------------- 1 | import { Applicator, ApplicateOptions } from './Applicator'; 2 | import { resolveFunction } from '../utils'; 3 | 4 | export class PartialApplicator extends Applicator { 5 | apply({ args, target, config: { execute } }: ApplicateOptions): any { 6 | return function(...invokeArgs: any[]): any { 7 | return execute(resolveFunction(args[0], this, target), ...args.slice(1)).apply(this, invokeArgs); 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /src/applicators/PartialValueApplicator.ts: -------------------------------------------------------------------------------- 1 | import isFunction = require('lodash/isFunction'); 2 | 3 | import { Applicator, ApplicateOptions } from './Applicator'; 4 | import { resolveFunction } from '../utils'; 5 | 6 | export class PartialValueApplicator extends Applicator { 7 | apply({ args, target, value, config: { execute } }: ApplicateOptions): any { 8 | return function(...invokeArgs: any[]): any { 9 | let fn = value; 10 | let argIndex = 0; 11 | 12 | if (!isFunction(fn)) { 13 | fn = resolveFunction(args[0], this, target); 14 | argIndex = 1; 15 | } 16 | 17 | return execute(fn, ...args.slice(argIndex)).apply(this, invokeArgs); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/applicators/PartialedApplicator.ts: -------------------------------------------------------------------------------- 1 | import partial = require('lodash/partial'); 2 | 3 | import { Applicator, ApplicateOptions } from './Applicator'; 4 | 5 | export class PartialedApplicator extends Applicator { 6 | apply({ config: { execute }, value, args }: ApplicateOptions): any { 7 | return partial(execute as (...args: any[]) => any, value, ...args); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/applicators/PostValueApplicator.ts: -------------------------------------------------------------------------------- 1 | import { Applicator, ApplicateOptions } from './Applicator'; 2 | 3 | export class PostValueApplicator extends Applicator { 4 | apply({ config: { execute, bound }, args, value }: ApplicateOptions): any { 5 | return execute(...args, value); 6 | } 7 | } -------------------------------------------------------------------------------- /src/applicators/PreValueApplicator.ts: -------------------------------------------------------------------------------- 1 | import { Applicator, ApplicateOptions } from './Applicator'; 2 | 3 | export class PreValueApplicator extends Applicator { 4 | apply({ value, config: { execute }, args }: ApplicateOptions): any { 5 | return execute(value, ...args); 6 | } 7 | } -------------------------------------------------------------------------------- /src/applicators/WrapApplicator.ts: -------------------------------------------------------------------------------- 1 | import { Applicator, ApplicateOptions } from './Applicator'; 2 | import { resolveFunction } from '../utils'; 3 | 4 | export class WrapApplicator extends Applicator { 5 | apply({ args, config: { execute }, target, value }: ApplicateOptions): any { 6 | return function(...invokeArgs: any[]): any { 7 | return execute(resolveFunction(args[0], this, target), value).apply(this, invokeArgs); 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /src/ary.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Ary } from './ary'; 4 | 5 | describe('ary', () => { 6 | it('should invoke the method with specified arguments', () => { 7 | class MyClass { 8 | @Ary(2) 9 | fn(...args: any[]) { 10 | expect(args.length).to.equal(2); 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | 16 | myClass.fn(1, 2, 3, 4, 5); 17 | }); 18 | }); -------------------------------------------------------------------------------- /src/ary.ts: -------------------------------------------------------------------------------- 1 | import ary = require('lodash/ary'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashMethodDecorator } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | const decorator = DecoratorFactory.createDecorator( 7 | new DecoratorConfig(ary, new PreValueApplicator()) 8 | ); 9 | 10 | /** 11 | * Creates a function that invokes func, with up to n arguments, ignoring any additional arguments. 12 | * @param {number} n The arity cap. 13 | * @example 14 | * 15 | * class MyClass { 16 | * @Ary(1) 17 | * fn(...args) { 18 | * return args; 19 | * } 20 | * } 21 | * 22 | * const myClass = new MyClass(); 23 | * 24 | * myClass.fn(1, 2, 3, 4); // => [ 1 ] 25 | */ 26 | export function Ary(n: number): LodashMethodDecorator { 27 | return decorator(n); 28 | } 29 | export { Ary as ary }; 30 | export default decorator; 31 | -------------------------------------------------------------------------------- /src/attempt.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Attempt } from './attempt'; 4 | 5 | describe('attempt', () => { 6 | it('should catch the error and return it', () => { 7 | class MyClass { 8 | @Attempt() 9 | fn() { 10 | throw new Error(); 11 | } 12 | 13 | @Attempt() 14 | fn2() { 15 | return 10; 16 | } 17 | } 18 | 19 | const myClass = new MyClass(); 20 | 21 | expect(myClass.fn()).to.be.an.instanceOf(Error); 22 | expect(myClass.fn2()).to.equal(10); 23 | }); 24 | 25 | it('should catch the error and return it (paramless)', () => { 26 | class MyClass { 27 | @Attempt 28 | fn() { 29 | throw new Error(); 30 | } 31 | 32 | @Attempt 33 | fn2() { 34 | return 10; 35 | } 36 | } 37 | 38 | const myClass = new MyClass(); 39 | 40 | expect(myClass.fn()).to.be.an.instanceOf(Error); 41 | expect(myClass.fn2()).to.equal(10); 42 | }); 43 | }); 44 | -------------------------------------------------------------------------------- /src/attempt.ts: -------------------------------------------------------------------------------- 1 | import attempt = require('lodash/attempt'); 2 | import partial = require('lodash/partial'); 3 | 4 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator } from './factory'; 5 | import { PreValueApplicator } from './applicators'; 6 | 7 | const attemptFn = (fn: () => void) => partial(attempt, fn); 8 | 9 | /** 10 | * Attempts to invoke func, returning either the result or the caught error object. Any additional arguments are provided to func when it's invoked. 11 | * @param {...*} [args] The arguments to invoke func with. 12 | * @example 13 | * 14 | * class MyClass { 15 | * @Attempt() 16 | * fn(value) { 17 | * if (typeof value === 'number') { 18 | * return value 19 | * } 20 | * 21 | * throw new Error(); 22 | * } 23 | * } 24 | * 25 | * const myClass = new MyClass(); 26 | * 27 | * myClass.fn(10); // => 10; 28 | * myClass.fn(null); // => Error 29 | */ 30 | export const Attempt = DecoratorFactory.createDecorator( 31 | new DecoratorConfig(attemptFn, new PreValueApplicator(), { 32 | optionalParams: true 33 | }) 34 | ) as BiTypedMethodDecorator; 35 | export { Attempt as attempt }; 36 | export default Attempt; 37 | -------------------------------------------------------------------------------- /src/before.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Before } from './before'; 4 | 5 | describe('before', () => { 6 | it('should invoke the method before 3 times', () => { 7 | let calls = 0; 8 | 9 | class MyClass { 10 | @Before(3) 11 | fn() { 12 | calls++; 13 | } 14 | } 15 | 16 | const myClass = new MyClass(); 17 | const myClass2 = new MyClass(); 18 | 19 | myClass.fn(); 20 | myClass.fn(); 21 | myClass.fn(); 22 | myClass.fn(); 23 | myClass.fn(); 24 | myClass.fn(); 25 | 26 | expect(calls, 'single class').to.equal(2); 27 | 28 | myClass2.fn(); 29 | myClass2.fn(); 30 | myClass2.fn(); 31 | myClass2.fn(); 32 | myClass2.fn(); 33 | myClass2.fn(); 34 | 35 | expect(calls, 'multiple class').to.equal(4); 36 | }); 37 | }); -------------------------------------------------------------------------------- /src/before.ts: -------------------------------------------------------------------------------- 1 | import before = require('lodash/before'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashDecorator } from './factory'; 4 | import { PostValueApplicator } from './applicators'; 5 | 6 | const decorator = DecoratorFactory.createInstanceDecorator( 7 | new DecoratorConfig(before, new PostValueApplicator(), { setter: true }) 8 | ); 9 | 10 | /** 11 | * Creates a function that invokes func, with the this binding and arguments of the created function, while it's called less than n times. 12 | * Subsequent calls to the created function return the result of the last func invocation. 13 | * @param {number} n The number of calls at whichc func is no longer invoked. 14 | * @example 15 | * 16 | * let calls = 0; 17 | * 18 | * class MyClass { 19 | * @Before(3) 20 | * fn() { 21 | * calls++; 22 | * } 23 | * } 24 | * 25 | * const myClass = new MyClass(); 26 | * 27 | * myClass.fn(); 28 | * myClass.fn(); 29 | * myClass.fn(); 30 | * myClass.fn(); 31 | * 32 | * calls === 2; // => true 33 | */ 34 | export function Before(n: number): LodashDecorator { 35 | return decorator(n); 36 | } 37 | export { Before as before }; 38 | export default decorator; 39 | -------------------------------------------------------------------------------- /src/beforeAll.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { BeforeAll } from './beforeAll'; 4 | 5 | describe('beforeAll', () => { 6 | it('should invoke the method before 3 times', () => { 7 | let calls = 0; 8 | 9 | class MyClass { 10 | @BeforeAll(3) 11 | fn() { 12 | calls++; 13 | } 14 | } 15 | 16 | const myClass = new MyClass(); 17 | const myClass2 = new MyClass(); 18 | 19 | myClass.fn(); 20 | myClass.fn(); 21 | myClass.fn(); 22 | myClass.fn(); 23 | myClass.fn(); 24 | myClass.fn(); 25 | 26 | expect(calls, 'single class').to.equal(2); 27 | 28 | myClass2.fn(); 29 | myClass2.fn(); 30 | myClass2.fn(); 31 | myClass2.fn(); 32 | myClass2.fn(); 33 | myClass2.fn(); 34 | 35 | expect(calls, 'multiple class').to.equal(2); 36 | }); 37 | }); -------------------------------------------------------------------------------- /src/beforeAll.ts: -------------------------------------------------------------------------------- 1 | import before = require('lodash/before'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashDecorator } from './factory'; 4 | import { PostValueApplicator } from './applicators'; 5 | 6 | const decorator = DecoratorFactory.createDecorator( 7 | new DecoratorConfig(before, new PostValueApplicator(), { setter: true }) 8 | ); 9 | 10 | /** 11 | * Creates a function that invokes func, with the this binding and arguments of the created function, while it's called less than n times. 12 | * Subsequent calls to the created function return the result of the last func invocation. 13 | * @param {number} n The number of calls at whichc func is no longer invoked. 14 | * @example 15 | * 16 | * let calls = 0; 17 | * 18 | * class MyClass { 19 | * @BeforeAll(3) 20 | * fn() { 21 | * calls++; 22 | * } 23 | * } 24 | * 25 | * const myClass = new MyClass(); 26 | * const myClass2 = new MyClass(); 27 | * 28 | * myClass.fn(); 29 | * myClass.fn(); 30 | * myClass.fn(); 31 | * myClass.fn(); 32 | * 33 | * myClass2.fn(); 34 | * 35 | * calls === 3; // => true 36 | */ 37 | export function BeforeAll(n: number): LodashDecorator { 38 | return decorator(n); 39 | } 40 | export { BeforeAll as beforeAll }; 41 | export default decorator; 42 | -------------------------------------------------------------------------------- /src/bind.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Bind } from './bind'; 4 | import { Once } from './once'; 5 | 6 | describe('bind', () => { 7 | it('should bind the context of the method', () => { 8 | let context; 9 | 10 | class MyClass { 11 | @Bind() 12 | fn() { 13 | context = this; 14 | } 15 | } 16 | 17 | const myClass = new MyClass(); 18 | const myClass2 = new MyClass(); 19 | 20 | myClass.fn.call(null); 21 | expect(context).to.equal(myClass); 22 | 23 | myClass2.fn.call(null); 24 | expect(context).to.equal(myClass2); 25 | }); 26 | 27 | it('should bind with other decorators', () => { 28 | let context; 29 | 30 | class MyClass { 31 | @Once() 32 | @Bind() 33 | fn() { 34 | context = this; 35 | } 36 | } 37 | 38 | const myClass = new MyClass(); 39 | 40 | myClass.fn.call(null); 41 | expect(context).to.equal(myClass); 42 | }); 43 | 44 | it('should bind without params', () => { 45 | let context; 46 | 47 | class MyClass { 48 | @Bind 49 | fn() { 50 | context = this; 51 | } 52 | } 53 | 54 | const myClass = new MyClass(); 55 | const myClass2 = new MyClass(); 56 | 57 | myClass.fn.call(null); 58 | expect(context).to.equal(myClass); 59 | 60 | myClass2.fn.call(null); 61 | expect(context).to.equal(myClass2); 62 | }); 63 | 64 | it('should not bind when accessed on the prototype', () => { 65 | let context; 66 | 67 | class MyClass { 68 | @Bind 69 | fn() { 70 | context = this; 71 | } 72 | } 73 | 74 | MyClass.prototype.fn(); 75 | 76 | const myClass = new MyClass(); 77 | 78 | myClass.fn.call(null); 79 | 80 | expect(context).to.equal(myClass); 81 | }); 82 | }); 83 | -------------------------------------------------------------------------------- /src/bind.ts: -------------------------------------------------------------------------------- 1 | import bind = require('lodash/bind'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator1 } from './factory'; 4 | import { BindApplicator } from './applicators'; 5 | 6 | /** 7 | * Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives. 8 | * 9 | * The _.bind.placeholder value, which defaults to _ in monolithic builds, may be used as a placeholder for partially applied arguments. 10 | * 11 | * Note: Unlike native Function#bind, this method doesn't set the "length" property of bound functions. 12 | * @param {...*} [partials] The argument to be partially applied. 13 | * @example 14 | * 15 | * class MyClass { 16 | * @Bind() 17 | * bound() { 18 | * return this; 19 | * } 20 | * 21 | * unbound() { 22 | * return this; 23 | * } 24 | * } 25 | * 26 | * const myClass = new MyClass(); 27 | * 28 | * myClass.bound.call(null); // => myClass {} 29 | * myClass.unbound.call(null); // => null 30 | */ 31 | export const Bind = DecoratorFactory.createInstanceDecorator( 32 | new DecoratorConfig(bind, new BindApplicator(), { 33 | optionalParams: true 34 | }) 35 | ) as BiTypedMethodDecorator1; 36 | export { Bind as bind, }; 37 | export default Bind; 38 | -------------------------------------------------------------------------------- /src/bindAll.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { BindAll } from './bindAll'; 4 | import { Debounce } from './debounce'; 5 | 6 | describe('bindAll', () => { 7 | it('should bind all method to the context', () => { 8 | let context; 9 | 10 | class Parent { 11 | fn() { 12 | context = this; 13 | } 14 | } 15 | 16 | @BindAll() 17 | class MyClass extends Parent { 18 | fn2() { 19 | context = this; 20 | } 21 | } 22 | 23 | const myClass = new MyClass(); 24 | 25 | myClass.fn.call(null); 26 | expect(context).to.equal(myClass); 27 | 28 | myClass.fn2.call(null); 29 | expect(context).to.equal(myClass); 30 | }); 31 | 32 | it('should bind only specified methods to the context', () => { 33 | let context; 34 | 35 | class Parent { 36 | fn() { 37 | context = this; 38 | } 39 | } 40 | 41 | @BindAll([ 'fn' ]) 42 | class MyClass extends Parent { 43 | fn2() { 44 | context = this; 45 | } 46 | } 47 | 48 | const myClass = new MyClass(); 49 | 50 | myClass.fn.call(null); 51 | expect(context).to.equal(myClass); 52 | 53 | myClass.fn2.call(null); 54 | expect(context).to.equal(null); 55 | }); 56 | 57 | it('should work with getters', () => { 58 | @BindAll() 59 | class MyClass { 60 | get prop(): string { 61 | return 'blorg'; 62 | } 63 | 64 | fn() { 65 | return this.prop; 66 | } 67 | } 68 | 69 | const myClass = new MyClass(); 70 | 71 | myClass.fn.call(null); 72 | expect(myClass.fn()).to.equal('blorg'); 73 | }); 74 | 75 | it('should not access getters that are not decorated', () => { 76 | let accessed = false; 77 | 78 | @BindAll() 79 | class MyClass { 80 | get prop(): string { 81 | accessed = true; 82 | 83 | return 'blorg'; 84 | } 85 | 86 | @Debounce() 87 | get prop2(): string { 88 | return 'test'; 89 | } 90 | 91 | @Debounce(1) 92 | fn() { 93 | return this.prop; 94 | } 95 | } 96 | 97 | const myClass = new MyClass(); 98 | 99 | expect(accessed).to.be.false; 100 | expect(myClass.hasOwnProperty('prop')).to.be.false; 101 | expect(myClass.hasOwnProperty('prop2')).to.be.false; 102 | }); 103 | 104 | it('should copy over any static properties on the constructor', () => { 105 | @BindAll() 106 | class MyClass { 107 | static $inject = []; 108 | } 109 | 110 | expect(MyClass.$inject).to.be.an('array'); 111 | }); 112 | 113 | it('should pass babels class call check', () => { 114 | function classCallCheck(instance: any, Constructor: any): void { 115 | if (!(instance instanceof Constructor)) { 116 | throw new TypeError('Cannot call a class as a function'); 117 | } 118 | } 119 | 120 | @BindAll() 121 | class MyClass { 122 | constructor() { 123 | classCallCheck(this, MyClass); 124 | } 125 | } 126 | 127 | expect(() => new MyClass()).not.to.throw(); 128 | }); 129 | }); 130 | -------------------------------------------------------------------------------- /src/bindAll.ts: -------------------------------------------------------------------------------- 1 | import isFunction = require('lodash/isFunction'); 2 | 3 | import { InstanceChainMap } from './factory'; 4 | import { Bind } from './bind'; 5 | 6 | /** 7 | * Binds methods of an object to the object itself, overwriting the existing method. 8 | * @export 9 | * @param {string[]} [methods=[]] 10 | * @returns {ClassDecorator} 11 | * @example 12 | * 13 | * @BindAll() 14 | * class MyClass { 15 | * bound() { 16 | * return this; 17 | * } 18 | * 19 | * unbound() { 20 | * return this; 21 | * } 22 | * } 23 | * 24 | * const myClass = new MyClass(); 25 | * 26 | * myClass.bound.call(null); // => MyClass {} 27 | * myClass.unbound.call(null); // => MyClass {} 28 | */ 29 | export function BindAll(methods: string[] = []): ClassDecorator { 30 | return (target: any) => { 31 | bindAllMethods(target, methods); 32 | }; 33 | } 34 | 35 | function bindAllMethods(target: Function, methods: string[] = []): void { 36 | const targetProto = target.prototype; 37 | let proto = target.prototype; 38 | const boundKeys: string[] = []; 39 | 40 | while (proto && proto !== Object.prototype) { 41 | for (const key of Object.getOwnPropertyNames(proto)) { 42 | const include = methods.length ? methods.indexOf(key) !== -1 : true; 43 | const descriptor = Object.getOwnPropertyDescriptor(proto, key); 44 | 45 | if (include && key !== 'constructor') { 46 | // If this property is a getter and it's NOT an instance decorated 47 | // method, ignore it. Instance decorators are getters until first accessed. 48 | if (descriptor.get) { 49 | const chainData = InstanceChainMap.get([ proto, key ]); 50 | 51 | if (!chainData || !chainData.isMethod) { 52 | continue; 53 | } 54 | } 55 | 56 | if (isFunction(proto[key]) && boundKeys.indexOf(key) === -1) { 57 | Object.defineProperty(targetProto, key, Bind(proto, key, descriptor)!); 58 | boundKeys.push(key); 59 | } 60 | } 61 | } 62 | 63 | proto = Object.getPrototypeOf(proto); 64 | } 65 | } 66 | 67 | export { BindAll as bindAll }; 68 | export default BindAll; 69 | -------------------------------------------------------------------------------- /src/combinationTest.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { spy } from 'sinon'; 3 | 4 | import { Bind } from './bind'; 5 | import { Memoize } from './memoize'; 6 | import { Curry } from './curry'; 7 | import { Delay } from './delay'; 8 | 9 | describe('multiple decorators', () => { 10 | describe('when using multiple instance decorators', () => { 11 | it('should apply both decorators', () => { 12 | const _spy = spy(); 13 | 14 | class MyClass { 15 | @Bind() 16 | @Memoize() 17 | fn(n: number) { 18 | expect(this).to.equal(myClass); 19 | _spy(); 20 | } 21 | } 22 | 23 | const myClass = new MyClass(); 24 | 25 | myClass.fn.call(null, 1); 26 | myClass.fn.call(null, 2); 27 | 28 | myClass.fn.call(null, 1); 29 | myClass.fn.call(null, 2); 30 | 31 | expect(_spy.callCount).to.equal(2); 32 | }); 33 | }); 34 | 35 | describe('when combining proto and instance decorators', () => { 36 | it('should apply both decorators', (done) => { 37 | const _spy = spy(); 38 | 39 | class MyClass { 40 | @Curry(2) 41 | @Delay(10) 42 | fn(n?: number, n2?: number): any { 43 | expect(this).to.equal(myClass); 44 | _spy(n, n2); 45 | } 46 | } 47 | 48 | const myClass = new MyClass(); 49 | const add10 = myClass.fn(10); 50 | 51 | expect(_spy.called).to.be.false; 52 | 53 | add10(5); 54 | 55 | expect(_spy.called).to.be.false; 56 | 57 | setTimeout(() => { 58 | expect(_spy.callCount).to.equal(1); 59 | expect(_spy.getCalls()[0].args).to.eql([ 10, 5 ]); 60 | done(); 61 | }, 20); 62 | }); 63 | 64 | describe('when proto is after instance', () => { 65 | it('should only apply the instance decorator', () => { 66 | const _spy = spy(); 67 | 68 | class MyClass { 69 | @Delay(10) 70 | @Curry(2) 71 | fn(n?: number, n2?: number): any { 72 | expect(this).to.equal(myClass); 73 | _spy(n, n2); 74 | } 75 | } 76 | 77 | const myClass = new MyClass(); 78 | const add10 = myClass.fn(10); 79 | 80 | expect(_spy.called).to.be.false; 81 | 82 | add10(5); 83 | 84 | expect(_spy.called).to.be.true; 85 | expect(_spy.getCalls()[0].args).to.eql([ 10, 5 ]); 86 | }); 87 | }); 88 | }); 89 | }); -------------------------------------------------------------------------------- /src/curry.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Curry } from './curry'; 4 | 5 | describe('curry', () => { 6 | it('should curry the method with default arity', () => { 7 | class MyClass { 8 | @Curry() 9 | add(a: any, b?: any) { 10 | return a + b; 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | const add5 = myClass.add(5); 16 | 17 | expect(add5).to.be.an.instanceOf(Function); 18 | expect(add5(10)).to.equal(15); 19 | }); 20 | 21 | it('should curry the method with default arity (paramless)', () => { 22 | class MyClass { 23 | @Curry 24 | add(a: any, b?: any) { 25 | return a + b; 26 | } 27 | } 28 | 29 | const myClass = new MyClass(); 30 | const add5 = myClass.add(5); 31 | 32 | expect(add5).to.be.an.instanceOf(Function); 33 | expect(add5(10)).to.equal(15); 34 | }); 35 | 36 | it('should curry the method with fixed arity', () => { 37 | class MyClass { 38 | @Curry(2) 39 | add(a: any, b?: any, c?: any) { 40 | return a + b * c; 41 | } 42 | } 43 | 44 | const myClass = new MyClass(); 45 | const add5 = myClass.add(5); 46 | 47 | expect(add5).to.be.an.instanceOf(Function); 48 | expect(add5(10, 2)).to.equal(25); 49 | }); 50 | 51 | it('should retain the class context', () => { 52 | class MyClass { 53 | value = 10; 54 | 55 | @Curry() 56 | add(a: any, b?: any): any { 57 | return (a + b) * this.value; 58 | } 59 | } 60 | 61 | const myClass = new MyClass(); 62 | const add5 = myClass.add(5); 63 | 64 | expect(add5(2)).to.equal(70); 65 | }); 66 | }); 67 | -------------------------------------------------------------------------------- /src/curry.ts: -------------------------------------------------------------------------------- 1 | import curry = require('lodash/curry'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator1 } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | /** 7 | * Creates a function that accepts arguments of func and either invokes func returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining func arguments, and so on. 8 | * The arity of func may be specified if func.length is not sufficient. 9 | * The original function is bound to the instance. If the method is needed to call in a different context use `CurryAll`. 10 | * 11 | * The _.curry.placeholder value, which defaults to _ in monolithic builds, may be used as a placeholder for provided arguments. 12 | * 13 | * Note: This method doesn't set the "length" property of curried functions. 14 | * @param {number} [arity] The arity of func. 15 | * @example 16 | * 17 | * class MyClass { 18 | * multiplier = 2; 19 | * 20 | * @Curry() 21 | * add(a, b) { 22 | * return (a + b) * this.multiplier; 23 | * } 24 | * } 25 | * 26 | * const myClass = new MyClass(); 27 | * 28 | * const add5 = myClass.add(5); 29 | * 30 | * add5AndMultiply(10); // => 30 31 | */ 32 | export const Curry = DecoratorFactory.createInstanceDecorator( 33 | new DecoratorConfig(curry, new PreValueApplicator(), { bound: true, optionalParams: true }) 34 | ) as BiTypedMethodDecorator1; 35 | export { Curry as curry }; 36 | export default Curry; 37 | -------------------------------------------------------------------------------- /src/curryAll.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { CurryAll } from './curryAll'; 4 | 5 | describe('curryAll', () => { 6 | it('should curry the method with default arity', () => { 7 | class MyClass { 8 | @CurryAll() 9 | add(a: any, b?: any) { 10 | return a + b; 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | const add5 = myClass.add(5); 16 | 17 | expect(add5).to.be.an.instanceOf(Function); 18 | expect(add5(10)).to.equal(15); 19 | }); 20 | 21 | it('should curry the method with default arity (paramless)', () => { 22 | class MyClass { 23 | @CurryAll 24 | add(a: any, b?: any) { 25 | return a + b; 26 | } 27 | } 28 | 29 | const myClass = new MyClass(); 30 | const add5 = myClass.add(5); 31 | 32 | expect(add5).to.be.an.instanceOf(Function); 33 | expect(add5(10)).to.equal(15); 34 | }); 35 | 36 | it('should curry the method with fixed arity', () => { 37 | class MyClass { 38 | @CurryAll(2) 39 | add(a: any, b?: any, c?: any) { 40 | return a + b * c; 41 | } 42 | } 43 | 44 | const myClass = new MyClass(); 45 | const add5 = myClass.add(5); 46 | 47 | expect(add5).to.be.an.instanceOf(Function); 48 | expect(add5(10, 2)).to.equal(25); 49 | }); 50 | 51 | it('should not retain the class context', () => { 52 | class MyClass { 53 | @CurryAll() 54 | add(a: any, b?: any): any { 55 | expect(this).to.equal(global); 56 | } 57 | } 58 | 59 | const myClass = new MyClass(); 60 | const add5 = myClass.add(5); 61 | 62 | add5(10); 63 | }); 64 | }); 65 | -------------------------------------------------------------------------------- /src/curryAll.ts: -------------------------------------------------------------------------------- 1 | import curry = require('lodash/curry'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedDecorator1 } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | /** 7 | * Creates a function that accepts arguments of func and either invokes func returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining func arguments, and so on. 8 | * The arity of func may be specified if func.length is not sufficient. 9 | * 10 | * The _.curry.placeholder value, which defaults to _ in monolithic builds, may be used as a placeholder for provided arguments. 11 | * 12 | * Note: This method doesn't set the "length" property of curried functions. 13 | * Note: The original function invoked will not be called in the context of the instance. Use `Curry` to for using it bound. 14 | * @param {number} [arity] The arity of func. 15 | * @example 16 | * 17 | * class MyClass { 18 | * @CurryAll() 19 | * add(a, b) { 20 | * return (a + b); 21 | * } 22 | * } 23 | * 24 | * const myClass = new MyClass(); 25 | * 26 | * const add5 = myClass.add(5); 27 | * 28 | * add5AndMultiply(10); // => 15 29 | */ 30 | export const CurryAll = DecoratorFactory.createDecorator( 31 | new DecoratorConfig(curry, new PreValueApplicator(), { optionalParams: true }) 32 | ) as BiTypedDecorator1; 33 | export { CurryAll as curryAll }; 34 | export default CurryAll; 35 | -------------------------------------------------------------------------------- /src/curryRight.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { CurryRight } from './curryRight'; 4 | 5 | describe('curryRight', () => { 6 | it('should curry the method with default arity', () => { 7 | class MyClass { 8 | @CurryRight() 9 | add(a: any, b?: any) { 10 | return [ a, b ]; 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | const set5 = myClass.add(5) as any; 16 | 17 | expect(set5(10)).to.eql([ 10, 5 ]); 18 | }); 19 | 20 | it('should curry the method with default arity (paramless)', () => { 21 | class MyClass { 22 | @CurryRight 23 | add(a: any, b?: any) { 24 | return [ a, b ]; 25 | } 26 | } 27 | 28 | const myClass = new MyClass(); 29 | const set5 = myClass.add(5) as any; 30 | 31 | expect(set5(10)).to.eql([ 10, 5 ]); 32 | }); 33 | 34 | it('should retain the class context', () => { 35 | class MyClass { 36 | value = 'blorg'; 37 | 38 | @CurryRight() 39 | add(a: any, b?: any) { 40 | return [ a, b, this.value ]; 41 | } 42 | } 43 | 44 | const myClass = new MyClass(); 45 | const set5 = myClass.add(5) as any; 46 | 47 | expect(set5(10)).to.eql([ 10, 5, 'blorg' ]); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /src/curryRight.ts: -------------------------------------------------------------------------------- 1 | import curryRight = require('lodash/curryRight'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator1 } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | /** 7 | * This method is like _.curry except that arguments are applied to func in the manner of _.partialRight instead of _.partial. 8 | * The arity of func may be specified if func.length is not sufficient. 9 | * The original function is bound to the instance. If the method is needed to call in a different context use `CurryAll`. 10 | * 11 | * The _.curryRight.placeholder value, which defaults to _ in monolithic builds, may be used as a placeholder for provided arguments. 12 | * 13 | * Note: This method doesn't set the "length" property of curried functions. 14 | * @param {number} [arity] The arity of func. 15 | * @example 16 | * 17 | * class MyClass { 18 | * multiplier = 2; 19 | * 20 | * @CurryRight() 21 | * add(a, b) { 22 | * return (a + b) * this.multiplier; 23 | * } 24 | * } 25 | * 26 | * const myClass = new MyClass(); 27 | * 28 | * const add5 = myClass.add(5); 29 | * 30 | * add5AndMultiply(10); // => 30 31 | */ 32 | export const CurryRight = DecoratorFactory.createInstanceDecorator( 33 | new DecoratorConfig(curryRight, new PreValueApplicator(), { bound: true, optionalParams: true }) 34 | ) as BiTypedMethodDecorator1; 35 | export { CurryRight as curryRight }; 36 | export default CurryRight; 37 | -------------------------------------------------------------------------------- /src/curryRightAll.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { CurryRightAll } from './curryRightAll'; 4 | 5 | describe('curryRightAll', () => { 6 | it('should curry the method with default arity', () => { 7 | class MyClass { 8 | @CurryRightAll() 9 | add(a: any, b?: any) { 10 | return [ a, b ]; 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | const set5 = myClass.add(5) as any; 16 | 17 | expect(set5(10)).to.eql([ 10, 5 ]); 18 | }); 19 | 20 | it('should curry the method with default arity (paramless)', () => { 21 | class MyClass { 22 | @CurryRightAll 23 | add(a: any, b?: any) { 24 | return [ a, b ]; 25 | } 26 | } 27 | 28 | const myClass = new MyClass(); 29 | const set5 = myClass.add(5) as any; 30 | 31 | expect(set5(10)).to.eql([ 10, 5 ]); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /src/curryRightAll.ts: -------------------------------------------------------------------------------- 1 | import curryRight = require('lodash/curryRight'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator1 } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | /** 7 | * This method is like _.curry except that arguments are applied to func in the manner of _.partialRight instead of _.partial. 8 | * The arity of func may be specified if func.length is not sufficient. 9 | * The original function is bound to the instance. If the method is needed to call in a different context use `CurryAll`. 10 | * 11 | * The _.curryRight.placeholder value, which defaults to _ in monolithic builds, may be used as a placeholder for provided arguments. 12 | * 13 | * Note: This method doesn't set the "length" property of curried functions. 14 | * @param {number} [arity] The arity of func. 15 | * @example 16 | * 17 | * class MyClass { 18 | * @CurryRightAll() 19 | * add(a, b) { 20 | * return (a + b); 21 | * } 22 | * } 23 | * 24 | * const myClass = new MyClass(); 25 | * 26 | * const add5 = myClass.add(5); 27 | * 28 | * add5AndMultiply(10); // => 15 29 | */ 30 | export const CurryRightAll = DecoratorFactory.createDecorator( 31 | new DecoratorConfig(curryRight, new PreValueApplicator(), { optionalParams: true }) 32 | ) as BiTypedMethodDecorator1; 33 | export { CurryRightAll as curryRightAll }; 34 | export default CurryRightAll; 35 | -------------------------------------------------------------------------------- /src/debounce.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Debounce } from './debounce'; 4 | 5 | describe('debounce', () => { 6 | it('should debounce the method', (done) => { 7 | let calls = 0; 8 | 9 | class MyClass { 10 | @Debounce(10) 11 | fn() { 12 | calls++; 13 | } 14 | } 15 | 16 | const myClass = new MyClass(); 17 | 18 | myClass.fn(); 19 | myClass.fn(); 20 | myClass.fn(); 21 | myClass.fn(); 22 | myClass.fn(); 23 | 24 | setTimeout(() => { 25 | expect(calls).to.equal(1); 26 | done(); 27 | }, 20); 28 | }); 29 | 30 | it('should debounce the property setter', (done) => { 31 | class MyClass { 32 | private _value: number = 100; 33 | 34 | @Debounce(10) 35 | set value(value: number) { 36 | this._value = value; 37 | } 38 | 39 | get value(): number { 40 | return this._value; 41 | } 42 | } 43 | 44 | const myClass = new MyClass(); 45 | 46 | myClass.value = 5; 47 | myClass.value = 15; 48 | 49 | expect(myClass.value).to.equal(100); 50 | 51 | setTimeout(() => { 52 | expect(myClass.value).to.equal(15); 53 | done(); 54 | }, 20); 55 | }); 56 | 57 | it('should contain the flush and cancel methods', () => { 58 | class MyClass { 59 | @Debounce(10) 60 | fn() {} 61 | } 62 | 63 | const myClass = new MyClass(); 64 | 65 | expect((myClass.fn as any).cancel).to.be.a('function'); 66 | expect((myClass.fn as any).flush).to.be.a('function'); 67 | }); 68 | }); 69 | -------------------------------------------------------------------------------- /src/debounce.ts: -------------------------------------------------------------------------------- 1 | import debounce = require('lodash/debounce'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashDecorator } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | import { DebounceOptions } from './shared'; 6 | 7 | const decorator = DecoratorFactory.createInstanceDecorator( 8 | new DecoratorConfig(debounce, new PreValueApplicator(), { setter: true }) 9 | ); 10 | 11 | /** 12 | * Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked. 13 | * The debounced function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them. 14 | * Provide options to indicate whether func should be invoked on the leading and/or trailing edge of the wait timeout. The func is invoked with the last arguments provided to the debounced function. 15 | * Subsequent calls to the debounced function return the result of the last func invocation. 16 | * 17 | * Note: If leading and trailing options are true, func is invoked on the trailing edge of the timeout only if the debounced function is invoked more than once during the wait timeout. 18 | * 19 | * If wait is 0 and leading is false, func invocation is deferred until to the next tick, similar to setTimeout with a timeout of 0. 20 | * 21 | * @param {number} [wait=0] The number in milliseconds to delay. 22 | * @param {DebounceOptions} [options] The options object. 23 | * @example 24 | * 25 | * class MyClass { 26 | * value = 100; 27 | * 28 | * @Debounce(10) 29 | * add(a) { 30 | * this.value += a; 31 | * } 32 | * } 33 | * 34 | * const myClass = new MyClass(); 35 | * 36 | * myClass.add(10); 37 | * myClass.add(50); 38 | * myClass.add(20); 39 | * 40 | * myClass.value; // => 100; 41 | * 42 | * setTimeout(() => { 43 | * myClass.value; // => 120; 44 | * }, 11); 45 | */ 46 | export function Debounce(wait?: number, options?: DebounceOptions): LodashDecorator { 47 | return decorator(wait, options); 48 | } 49 | export { Debounce as debounce }; 50 | export default decorator; 51 | -------------------------------------------------------------------------------- /src/debounceAll.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { DebounceAll } from './debounceAll'; 4 | 5 | describe('debounceAll', () => { 6 | it('should debounce the method across instances', (done) => { 7 | let calls = 0; 8 | 9 | class MyClass { 10 | @DebounceAll(10) 11 | fn() { 12 | calls++; 13 | } 14 | } 15 | 16 | const myClass = new MyClass(); 17 | const myClass2 = new MyClass(); 18 | 19 | myClass.fn(); 20 | myClass.fn(); 21 | myClass.fn(); 22 | myClass.fn(); 23 | myClass.fn(); 24 | 25 | myClass2.fn(); 26 | myClass2.fn(); 27 | myClass2.fn(); 28 | 29 | setTimeout(() => { 30 | expect(calls).to.equal(1); 31 | done(); 32 | }, 20); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /src/debounceAll.ts: -------------------------------------------------------------------------------- 1 | import debounce = require('lodash/debounce'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashMethodDecorator } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | import { DebounceOptions } from './shared'; 6 | 7 | const decorator = DecoratorFactory.createDecorator( 8 | new DecoratorConfig(debounce, new PreValueApplicator()) 9 | ); 10 | 11 | /** 12 | * Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked. 13 | * The debounced function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them. 14 | * Provide options to indicate whether func should be invoked on the leading and/or trailing edge of the wait timeout. The func is invoked with the last arguments provided to the debounced function. 15 | * Subsequent calls to the debounced function return the result of the last func invocation. 16 | * 17 | * The debounce state is shared across all instances of the class. 18 | * 19 | * Note: If leading and trailing options are true, func is invoked on the trailing edge of the timeout only if the debounced function is invoked more than once during the wait timeout. 20 | * 21 | * If wait is 0 and leading is false, func invocation is deferred until to the next tick, similar to setTimeout with a timeout of 0. 22 | * 23 | * @param {number} [wait=0] The number in milliseconds to delay. 24 | * @param {DebounceOptions} [options] The options object. 25 | * @example 26 | * 27 | * class MyClass { 28 | * value = 100; 29 | * 30 | * @DebounceAll(10) 31 | * add(a) { 32 | * this.value += a; 33 | * } 34 | * } 35 | * 36 | * const myClass = new MyClass(); 37 | * 38 | * myClass.add(10); 39 | * myClass.add(50); 40 | * myClass.add(20); 41 | * 42 | * myClass.value; // => 100; 43 | * 44 | * setTimeout(() => { 45 | * myClass.value; // => 120; 46 | * }, 11); 47 | */ 48 | export function DebounceAll(wait?: number, options?: DebounceOptions): LodashMethodDecorator { 49 | return decorator(wait, options); 50 | } 51 | export { DebounceAll as debounceAll }; 52 | export default decorator; 53 | -------------------------------------------------------------------------------- /src/defer.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { spy } from 'sinon'; 3 | 4 | import { Defer } from './defer'; 5 | 6 | describe('defer', () => { 7 | it('should defer the method', (done) => { 8 | const _spy = spy(); 9 | 10 | class MyClass { 11 | @Defer('test') 12 | fn(...args: any[]) { 13 | expect(this, 'context').to.equal(myClass); 14 | _spy(...args); 15 | } 16 | } 17 | 18 | const myClass = new MyClass(); 19 | 20 | myClass.fn(10); 21 | expect(_spy.called).to.be.false; 22 | 23 | setTimeout(() => { 24 | expect(_spy.callCount).to.equal(1); 25 | expect(_spy.getCalls()[0].args).to.eql([ 10, 'test' ]); 26 | done(); 27 | }, 0); 28 | }); 29 | 30 | it('should defer the method (paramless)', (done) => { 31 | const _spy = spy(); 32 | 33 | class MyClass { 34 | @Defer 35 | fn(...args: any[]) { 36 | expect(this, 'context').to.equal(myClass); 37 | _spy(...args); 38 | } 39 | } 40 | 41 | const myClass = new MyClass(); 42 | 43 | myClass.fn(10); 44 | expect(_spy.called).to.be.false; 45 | 46 | setTimeout(() => { 47 | expect(_spy.callCount).to.equal(1); 48 | expect(_spy.getCalls()[0].args).to.eql([ 10 ]); 49 | done(); 50 | }, 0); 51 | }); 52 | 53 | it('should debounce the property setter', (done) => { 54 | class MyClass { 55 | private _value: number = 100; 56 | 57 | @Defer() 58 | set value(value: number) { 59 | expect(this, 'context').to.equal(myClass); 60 | this._value = value; 61 | } 62 | 63 | get value(): number { 64 | return this._value; 65 | } 66 | } 67 | 68 | const myClass = new MyClass(); 69 | 70 | myClass.value = 5; 71 | myClass.value = 15; 72 | 73 | expect(myClass.value).to.equal(100); 74 | 75 | setTimeout(() => { 76 | expect(myClass.value).to.equal(15); 77 | done(); 78 | }, 0); 79 | }); 80 | 81 | it('should debounce the property setter (paramless)', (done) => { 82 | class MyClass { 83 | private _value: number = 100; 84 | 85 | @Defer 86 | set value(value: number) { 87 | expect(this, 'context').to.equal(myClass); 88 | this._value = value; 89 | } 90 | 91 | get value(): number { 92 | return this._value; 93 | } 94 | } 95 | 96 | const myClass = new MyClass(); 97 | 98 | myClass.value = 5; 99 | myClass.value = 15; 100 | 101 | expect(myClass.value).to.equal(100); 102 | 103 | setTimeout(() => { 104 | expect(myClass.value).to.equal(15); 105 | done(); 106 | }, 0); 107 | }); 108 | }); 109 | -------------------------------------------------------------------------------- /src/defer.ts: -------------------------------------------------------------------------------- 1 | import defer = require('lodash/defer'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedDecoratorN } from './factory'; 4 | import { InvokeApplicator } from './applicators'; 5 | 6 | /** 7 | * Defers invoking the func until the current call stack has cleared. Any additional arguments are provided to func when it's invoked. 8 | * 9 | * @param {...*} [args] Additional arguments to invoke the function with 10 | * @example 11 | * 12 | * class MyClass { 13 | * value = 100; 14 | * 15 | * @Defer() 16 | * add(a) { 17 | * this.value += a; 18 | * } 19 | * } 20 | * 21 | * const myClass = new MyClass(); 22 | * 23 | * myClass.add(10); 24 | * 25 | * myClass.value; // => 100; 26 | * 27 | * setTimeout(() => { 28 | * myClass.value; // => 110; 29 | * }, 0); 30 | */ 31 | export const Defer = DecoratorFactory.createDecorator( 32 | new DecoratorConfig(defer, new InvokeApplicator(), { setter: true, optionalParams: true }) 33 | ) as BiTypedDecoratorN; 34 | export { Defer as defer }; 35 | export default Defer; 36 | -------------------------------------------------------------------------------- /src/delay.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { spy } from 'sinon'; 3 | 4 | import { Delay } from './delay'; 5 | 6 | describe('delay', () => { 7 | it('should delay the method', (done) => { 8 | const _spy = spy(); 9 | 10 | class MyClass { 11 | @Delay(15, 'test') 12 | fn(...args: any[]) { 13 | expect(this, 'context').to.equal(myClass); 14 | _spy(...args); 15 | } 16 | } 17 | 18 | const myClass = new MyClass(); 19 | 20 | myClass.fn(10); 21 | expect(_spy.called).to.be.false; 22 | 23 | setTimeout(() => { 24 | expect(_spy.callCount).to.equal(1); 25 | expect(_spy.getCalls()[0].args).to.eql([ 10, 'test' ]); 26 | done(); 27 | }, 16); 28 | }); 29 | 30 | it('should debounce the property setter', (done) => { 31 | class MyClass { 32 | private _value: number = 100; 33 | 34 | @Delay(15) 35 | set value(value: number) { 36 | expect(this, 'context').to.equal(myClass); 37 | this._value = value; 38 | } 39 | 40 | get value(): number { 41 | return this._value; 42 | } 43 | } 44 | 45 | const myClass = new MyClass(); 46 | 47 | myClass.value = 5; 48 | myClass.value = 15; 49 | 50 | expect(myClass.value).to.equal(100); 51 | 52 | setTimeout(() => { 53 | expect(myClass.value).to.equal(15); 54 | done(); 55 | }, 15); 56 | }); 57 | }); 58 | -------------------------------------------------------------------------------- /src/delay.ts: -------------------------------------------------------------------------------- 1 | import delay = require('lodash/delay'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashMethodDecorator } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | const decorator = DecoratorFactory.createDecorator( 7 | new DecoratorConfig( 8 | function(value: Function, wait: number, ...args: any[]) { 9 | return function(...invokeArgs: any[]): any { 10 | return delay(value.bind(this), wait, ...invokeArgs, ...args); 11 | }; 12 | }, 13 | new PreValueApplicator(), 14 | { setter: true } 15 | ) 16 | ); 17 | 18 | /** 19 | * Invokes func after wait milliseconds. Any additional arguments are provided to func when it's invoked. 20 | * 21 | * @param {number} wait The number of milliseconds to delay invocation. 22 | * @param {...*} [args] Additional arguments to invoke the function with 23 | * @example 24 | * 25 | * class MyClass { 26 | * value = 100; 27 | * 28 | * @Delay(20) 29 | * add(a) { 30 | * this.value += a; 31 | * } 32 | * } 33 | * 34 | * const myClass = new MyClass(); 35 | * 36 | * myClass.add(10); 37 | * 38 | * myClass.value; // => 100; 39 | * 40 | * setTimeout(() => { 41 | * myClass.value; // => 110; 42 | * }, 30); 43 | */ 44 | export function Delay(wait: number, ...args: any[]): LodashMethodDecorator { 45 | return decorator(wait, ...args); 46 | } 47 | export { Delay as delay }; 48 | export default decorator; 49 | -------------------------------------------------------------------------------- /src/factory.ts: -------------------------------------------------------------------------------- 1 | export * from './factory/DecoratorConfig'; 2 | export * from './factory/DecoratorFactory'; 3 | export * from './factory/common'; 4 | -------------------------------------------------------------------------------- /src/factory/DecoratorConfig.ts: -------------------------------------------------------------------------------- 1 | import { Applicator } from '../applicators'; 2 | 3 | export interface DecoratorConfigOptions { 4 | bound?: boolean; 5 | setter?: boolean; 6 | getter?: boolean; 7 | property?: boolean; 8 | method?: boolean; 9 | optionalParams?: boolean; 10 | } 11 | 12 | export class DecoratorConfig { 13 | constructor( 14 | public readonly execute: Function, 15 | public readonly applicator: Applicator, 16 | public readonly options: DecoratorConfigOptions = {} 17 | ) {} 18 | 19 | get bound(): boolean { 20 | return this.options.bound != null ? this.options.bound : false; 21 | } 22 | 23 | get setter(): boolean { 24 | return this.options.setter != null ? this.options.setter : false; 25 | } 26 | 27 | get getter(): boolean { 28 | return this.options.getter != null ? this.options.getter : false; 29 | } 30 | 31 | get property(): boolean { 32 | return this.options.property != null ? this.options.property : false; 33 | } 34 | 35 | get method(): boolean { 36 | return this.options.method != null ? this.options.method : true; 37 | } 38 | 39 | get optionalParams(): boolean { 40 | return this.options.optionalParams != null ? this.options.optionalParams : false; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/factory/DecoratorFactory.ts: -------------------------------------------------------------------------------- 1 | import isFunction = require('lodash/isFunction'); 2 | 3 | import { 4 | InstanceChainMap, 5 | LodashDecorator, 6 | InstanceChainContext 7 | } from './common'; 8 | import { DecoratorConfig } from './DecoratorConfig'; 9 | import { 10 | copyMetadata, 11 | bind, 12 | isMethodOrPropertyDecoratorArgs, 13 | isPrototypeAccess 14 | } from '../utils'; 15 | 16 | export type GenericDecorator = (...args: any[]) => LodashDecorator; 17 | 18 | export class InternalDecoratorFactory { 19 | createDecorator(config: DecoratorConfig): GenericDecorator { 20 | const { applicator, optionalParams } = config; 21 | 22 | return (...args: any[]): LodashDecorator => { 23 | let params = args; 24 | 25 | const decorator = (target: Object, name: string, _descriptor?: PropertyDescriptor): PropertyDescriptor => { 26 | const descriptor = this._resolveDescriptor(target, name, _descriptor); 27 | const { value, get, set } = descriptor; 28 | 29 | // If this decorator is being applied after an instance decorator we simply ignore it 30 | // as we can't apply it correctly. 31 | if (!InstanceChainMap.has([ target, name ])) { 32 | if (isFunction(value)) { 33 | descriptor.value = copyMetadata(applicator.apply({ config, target, value, args: params }), value); 34 | } else if (isFunction(get) && config.getter) { 35 | descriptor.get = copyMetadata(applicator.apply({ config, target, value: get, args: params }), get); 36 | } else if (isFunction(set) && config.setter) { 37 | descriptor.set = copyMetadata(applicator.apply({ config, target, value: set, args: params }), set); 38 | } 39 | } 40 | 41 | return descriptor; 42 | }; 43 | 44 | if (optionalParams && isMethodOrPropertyDecoratorArgs(...args)) { 45 | params = []; 46 | 47 | return decorator(args[0], args[1], args[2]) as any; 48 | } 49 | 50 | return decorator; 51 | }; 52 | } 53 | 54 | createInstanceDecorator(config: DecoratorConfig): GenericDecorator { 55 | const { applicator, bound, optionalParams } = config; 56 | 57 | return (...args: any[]): LodashDecorator => { 58 | let params = args; 59 | const decorator = (target: Object, name: string, _descriptor?: PropertyDescriptor): PropertyDescriptor => { 60 | const descriptor = this._resolveDescriptor(target, name, _descriptor); 61 | const { value, writable, enumerable, configurable, get, set } = descriptor; 62 | const isFirstInstance = !InstanceChainMap.has([ target, name ]); 63 | const chainData = InstanceChainMap.get([ target, name ]) || { fns: [], properties: [] }; 64 | const isGetter = isFirstInstance && isFunction(get); 65 | const isSetter = isFirstInstance && isFunction(set); 66 | const isMethod = isFirstInstance && isFunction(value); 67 | const isProperty = isFirstInstance && !isGetter && !isSetter && !isMethod; 68 | const baseValue = isGetter ? get : isMethod ? value : undefined; 69 | 70 | chainData.properties.push(name); 71 | chainData.fns.push((fn: Function, instance: any, context: InstanceChainContext) => { 72 | if (!this._isApplicable(context, config)) { 73 | return fn; 74 | } 75 | 76 | if (bound) { 77 | fn = bind(fn, instance); 78 | } 79 | 80 | return copyMetadata( 81 | applicator.apply({ args: params, target, instance, value: fn, config }), 82 | fn 83 | ); 84 | }); 85 | 86 | InstanceChainMap.set([ target, name ], chainData); 87 | 88 | if (!isFirstInstance) { 89 | return descriptor; 90 | } 91 | 92 | chainData.isSetter = isSetter; 93 | chainData.isGetter = isGetter; 94 | chainData.isMethod = isMethod; 95 | chainData.isProperty = isProperty; 96 | 97 | const applyChain = (fn: any, context: InstanceChainContext, instance: any) => { 98 | return chainData.fns.reduce((result: Function, next: Function) => next(result, instance, context), fn); 99 | }; 100 | 101 | const applyDecorator = (instance: any) => { 102 | let getter = get || undefined; 103 | let setter = set || undefined; 104 | 105 | if (isGetter || isSetter) { 106 | // If we have a getter apply the decorators to the getter and assign it to the instance. 107 | if (isGetter) { 108 | getter = applyChain(get, { value: get, getter: true }, instance); 109 | } 110 | 111 | if (isSetter) { 112 | setter = applyChain(set, { value: set, setter: true }, instance); 113 | } 114 | 115 | Object.defineProperty(instance, name, { 116 | enumerable, 117 | configurable, 118 | get: getter, 119 | set: setter 120 | }); 121 | } else if (isMethod || isProperty) { 122 | const newFn = isMethod 123 | ? applyChain(value, { value, method: true }, instance) 124 | : applyChain(value, { value, property: true }, instance); 125 | 126 | Object.defineProperty(instance, name, { 127 | writable, 128 | enumerable, 129 | configurable, 130 | value: newFn 131 | }); 132 | } 133 | } 134 | 135 | if (isMethod || isProperty) { 136 | delete descriptor.value; 137 | delete descriptor.writable; 138 | } 139 | 140 | descriptor.get = function() { 141 | // Check for direct access on the prototype. 142 | // MyClass.prototype.fn <-- This should not apply the decorator. 143 | if (isPrototypeAccess(this, target)) { 144 | return baseValue; 145 | } 146 | 147 | applyDecorator(this); 148 | 149 | const descriptor = Object.getOwnPropertyDescriptor(this, name)!; 150 | 151 | if (descriptor.get) { 152 | return descriptor.get.call(this); 153 | } 154 | 155 | return descriptor.value; 156 | }; 157 | 158 | descriptor.set = function(value) { 159 | applyDecorator(this); 160 | 161 | const descriptor = Object.getOwnPropertyDescriptor(this, name)!; 162 | 163 | if (descriptor.set) { 164 | descriptor.set.call(this, value); 165 | } else if (isProperty || isMethod) { 166 | this[name] = value; 167 | } 168 | }; 169 | 170 | return descriptor; 171 | }; 172 | 173 | if (optionalParams && isMethodOrPropertyDecoratorArgs(...args)) { 174 | params = []; 175 | 176 | return decorator(args[0], args[1], args[2]) as any; 177 | } 178 | 179 | return decorator; 180 | }; 181 | } 182 | 183 | private _isApplicable(context: InstanceChainContext, config: DecoratorConfig): boolean { 184 | return !Boolean( 185 | context.getter && !config.getter 186 | || context.setter && !config.setter 187 | || context.method && !config.method 188 | || context.property && !config.property 189 | ); 190 | } 191 | 192 | private _resolveDescriptor(target: Object, name: string, descriptor?: PropertyDescriptor): PropertyDescriptor { 193 | if (descriptor) { 194 | return descriptor; 195 | } 196 | 197 | return Object.getOwnPropertyDescriptor(target, name) || {}; 198 | } 199 | } 200 | 201 | export const DecoratorFactory = new InternalDecoratorFactory(); 202 | -------------------------------------------------------------------------------- /src/factory/common.ts: -------------------------------------------------------------------------------- 1 | import { Applicator } from '../applicators'; 2 | import { CompositeKeyWeakMap } from '../utils'; 3 | 4 | export type ApplicatorToken = { new(): Applicator }; 5 | export type LodashMethodDecorator = MethodDecorator; 6 | export type LodashDecorator = MethodDecorator & PropertyDecorator; 7 | export type ResolvableFunction = string|Function; 8 | 9 | export type BiTypedMethodDecorator = (() => LodashMethodDecorator) & LodashMethodDecorator; 10 | export type BiTypedMethodDecorator1 = ((arg?: T) => LodashMethodDecorator) & LodashMethodDecorator; 11 | export type BiTypedMethodDecorator2 = ((arg1?: T, arg2?: T2) => LodashMethodDecorator) & LodashMethodDecorator; 12 | export type BiTypedMethodDecorator3 = ((arg1?: T, arg2?: T2, arg3?: T3) => LodashMethodDecorator) & LodashMethodDecorator; 13 | export type BiTypedMethodDecoratorN = ((...args: any[]) => LodashMethodDecorator) & LodashMethodDecorator; 14 | export type BiTypedDecorator = (() => LodashDecorator) & LodashDecorator; 15 | export type BiTypedDecorator1 = ((arg?: T) => LodashDecorator) & LodashDecorator; 16 | export type BiTypedDecorator2 = ((arg1?: T, arg2?: T2) => LodashDecorator) & LodashDecorator; 17 | export type BiTypedDecorator3 = ((arg1?: T, arg2?: T2, arg3?: T3) => LodashDecorator) & LodashDecorator; 18 | export type BiTypedDecoratorN = ((...args: any[]) => LodashDecorator) & LodashDecorator; 19 | 20 | export interface InstanceChainContext { 21 | getter?: boolean; 22 | setter?: boolean; 23 | method?: boolean; 24 | property?: boolean; 25 | value: any; 26 | } 27 | 28 | export interface InstanceChainData { 29 | properties: string[]; 30 | fns: Function[]; 31 | isGetter: boolean; 32 | isSetter: boolean; 33 | isMethod: boolean; 34 | isProperty: boolean; 35 | } 36 | 37 | export const InstanceChainMap = new CompositeKeyWeakMap(); 38 | -------------------------------------------------------------------------------- /src/flip.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Flip } from './flip'; 4 | 5 | describe('flip', () => { 6 | it('should flip the arguments of the function', () => { 7 | class MyClass { 8 | @Flip() 9 | fn(a: number, b: number) { 10 | return [ a, b ]; 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | 16 | expect(myClass.fn(10, 20)).to.eql([ 20, 10 ]); 17 | }); 18 | 19 | it('should flip the arguments of the resolved function', () => { 20 | class MyClass { 21 | @Flip('fn') 22 | fn2: (b: number, a: number) => [ number, number ]; 23 | 24 | fn(a: number, b: number) { 25 | return [ a, b ]; 26 | } 27 | } 28 | 29 | const myClass = new MyClass(); 30 | 31 | expect(myClass.fn2(10, 20)).to.eql([ 20, 10 ]); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /src/flip.ts: -------------------------------------------------------------------------------- 1 | import flip = require('lodash/flip'); 2 | 3 | import { 4 | DecoratorConfig, 5 | DecoratorFactory, 6 | LodashDecorator, 7 | ResolvableFunction 8 | } from './factory'; 9 | import { PartialValueApplicator } from './applicators'; 10 | 11 | const decorator = DecoratorFactory.createInstanceDecorator( 12 | new DecoratorConfig(flip, new PartialValueApplicator(), { property: true }) 13 | ); 14 | 15 | /** 16 | * Creates a function that invokes func with arguments reversed. Honestly, there is probably not much 17 | * use for this decorator but maybe you will find one? 18 | * 19 | * @example 20 | * 21 | * class MyClass { 22 | * value = 100; 23 | * 24 | * @Flip('fn') 25 | * fn2: (b: number, a: string) => [ number, string ]; 26 | * 27 | * fn(a: string, b: number): [ string, number ] { 28 | * return [ a, b ]; 29 | * } 30 | * } 31 | * 32 | * const myClass = new MyClass(); 33 | * 34 | * myClass.fn2(10, '20'); // => [ '20', 10 ] 35 | */ 36 | export function Flip(fn?: ResolvableFunction): LodashDecorator { 37 | return decorator(fn); 38 | } 39 | export { Flip as flip }; 40 | export default decorator; 41 | -------------------------------------------------------------------------------- /src/flow.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Flow } from './flow'; 4 | 5 | describe('flow', () => { 6 | it('should compose the functions', () => { 7 | class MyClass { 8 | name = 'Avry'; 9 | 10 | @Flow('getName', (v: string) => v.toUpperCase()) 11 | fn(...args: any[]) { 12 | return args[0]; 13 | } 14 | 15 | getName(): string { 16 | expect(this, 'context').to.equal(myClass); 17 | 18 | return this.name; 19 | } 20 | } 21 | 22 | const myClass = new MyClass(); 23 | 24 | expect(myClass.fn()).to.equal('AVRY'); 25 | }); 26 | 27 | it('should compose and assign to the property', () => { 28 | class MyClass { 29 | name = 'Avry'; 30 | 31 | @Flow('getName', (v: string) => v.toUpperCase()) 32 | fn: () => string; 33 | 34 | getName(): string { 35 | expect(this, 'context').to.equal(myClass); 36 | 37 | return this.name; 38 | } 39 | } 40 | 41 | const myClass = new MyClass(); 42 | 43 | expect(myClass.fn()).to.equal('AVRY'); 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /src/flow.ts: -------------------------------------------------------------------------------- 1 | import flow = require('lodash/flow'); 2 | 3 | import { 4 | DecoratorConfig, 5 | DecoratorFactory, 6 | ResolvableFunction, 7 | LodashDecorator 8 | } from './factory'; 9 | import { ComposeApplicator } from './applicators'; 10 | 11 | const decorator = DecoratorFactory.createInstanceDecorator( 12 | new DecoratorConfig(flow, new ComposeApplicator({ post: true }), { property: true }) 13 | ); 14 | 15 | /** 16 | * Creates a function that returns the result of invoking the given functions with the this binding of the created function, 17 | * where each successive invocation is supplied the return value of the previous. 18 | * 19 | * @example 20 | * 21 | * class MyClass { 22 | * name = 'Ted'; 23 | * 24 | * @Flow('getName', toUpperCase) 25 | * getUpperCaseName: () => string; 26 | * 27 | * getName() { 28 | * return this.name; 29 | * } 30 | * } 31 | * 32 | * const myClass = new MyClass(); 33 | * 34 | * myClass.getUpperCaseName(); // => 'TED' 35 | */ 36 | export function Flow(...fns: ResolvableFunction[]): LodashDecorator { 37 | return decorator(...fns); 38 | } 39 | export { Flow as flow }; 40 | export default decorator; 41 | -------------------------------------------------------------------------------- /src/flowRight.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { FlowRight } from './flowRight'; 4 | 5 | describe('flowRight', () => { 6 | it('should compose the functions', () => { 7 | class MyClass { 8 | name = 'Avry'; 9 | 10 | @FlowRight((v: string) => v.toUpperCase(), 'getName') 11 | fn(...args: any[]) { 12 | return args[0]; 13 | } 14 | 15 | getName(): string { 16 | expect(this, 'context').to.equal(myClass); 17 | 18 | return this.name; 19 | } 20 | } 21 | 22 | const myClass = new MyClass(); 23 | 24 | expect(myClass.fn()).to.equal('AVRY'); 25 | }); 26 | 27 | it('should compose the property', () => { 28 | class MyClass { 29 | name = 'Avry'; 30 | 31 | @FlowRight((v: string) => v.toUpperCase(), 'getName') 32 | fn: () => string; 33 | 34 | getName(): string { 35 | expect(this, 'context').to.equal(myClass); 36 | 37 | return this.name; 38 | } 39 | } 40 | 41 | const myClass = new MyClass(); 42 | 43 | expect(myClass.fn()).to.equal('AVRY'); 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /src/flowRight.ts: -------------------------------------------------------------------------------- 1 | import flowRight = require('lodash/flowRight'); 2 | 3 | import { 4 | DecoratorConfig, 5 | DecoratorFactory, 6 | LodashDecorator, 7 | ResolvableFunction 8 | } from './factory'; 9 | import { ComposeApplicator } from './applicators'; 10 | 11 | const decorator = DecoratorFactory.createInstanceDecorator( 12 | new DecoratorConfig(flowRight, new ComposeApplicator({ post: false }), { property: true }) 13 | ); 14 | 15 | /** 16 | * Creates a function that returns the result of invoking the given functions with the this binding of the created function, 17 | * where each successive invocation is supplied the return value of the previous. 18 | * 19 | * @example 20 | * 21 | * class MyClass { 22 | * name = 'Ted'; 23 | * 24 | * @FlowRight(toUpperCase, 'getName') 25 | * getUpperCaseName: () => string; 26 | * 27 | * getName() { 28 | * return this.name; 29 | * } 30 | * } 31 | * 32 | * const myClass = new MyClass(); 33 | * 34 | * myClass.getUpperCaseName(); // => 'TED' 35 | */ 36 | export function FlowRight(...fns: ResolvableFunction[]): LodashDecorator { 37 | return decorator(...fns); 38 | } 39 | export { FlowRight as flowRight }; 40 | export default decorator; 41 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './factory'; 2 | export * from './after'; 3 | export * from './afterAll'; 4 | export * from './ary'; 5 | export * from './attempt'; 6 | export * from './before'; 7 | export * from './beforeAll'; 8 | export * from './bind'; 9 | export * from './bindAll'; 10 | export * from './curry'; 11 | export * from './curryAll'; 12 | export * from './curryRight'; 13 | export * from './curryRightAll'; 14 | export * from './debounce'; 15 | export * from './debounceAll'; 16 | export * from './defer'; 17 | export * from './delay'; 18 | export * from './flip'; 19 | export * from './flow'; 20 | export * from './flowRight'; 21 | export * from './memoize'; 22 | export * from './memoizeAll'; 23 | export * from './mixin'; 24 | export * from './negate'; 25 | export * from './once'; 26 | export * from './overArgs'; 27 | export * from './partial'; 28 | export * from './partialRight'; 29 | export * from './rearg'; 30 | export * from './rest'; 31 | export * from './spread'; 32 | export * from './tap'; 33 | export * from './throttle'; 34 | export * from './throttleAll'; 35 | export * from './unary'; 36 | export * from './wrap'; 37 | -------------------------------------------------------------------------------- /src/memoize.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { spy } from 'sinon'; 3 | 4 | import { Memoize } from './memoize'; 5 | 6 | describe('memoize', () => { 7 | it('should memoize the function', () => { 8 | const _spy = spy(); 9 | 10 | class MyClass { 11 | @Memoize() 12 | fn(n: number): number { 13 | _spy(n); 14 | expect(this, 'context').to.equal(myClass); 15 | 16 | return n; 17 | } 18 | } 19 | 20 | const myClass = new MyClass(); 21 | 22 | myClass.fn(1); 23 | myClass.fn(1); 24 | myClass.fn(1); 25 | myClass.fn(1); 26 | myClass.fn(1); 27 | 28 | expect(myClass.fn(1)).to.equal(1); 29 | expect(_spy.callCount).to.equal(1); 30 | 31 | myClass.fn(2); 32 | myClass.fn(2); 33 | 34 | expect(myClass.fn(2)).to.equal(2); 35 | expect(_spy.callCount).to.equal(2); 36 | }); 37 | 38 | it('should memoize the function (paramless)', () => { 39 | const _spy = spy(); 40 | 41 | class MyClass { 42 | @Memoize 43 | fn(n: number): number { 44 | _spy(n); 45 | expect(this, 'context').to.equal(myClass); 46 | 47 | return n; 48 | } 49 | } 50 | 51 | const myClass = new MyClass(); 52 | 53 | myClass.fn(1); 54 | myClass.fn(1); 55 | myClass.fn(1); 56 | myClass.fn(1); 57 | myClass.fn(1); 58 | 59 | expect(myClass.fn(1)).to.equal(1); 60 | expect(_spy.callCount).to.equal(1); 61 | 62 | myClass.fn(2); 63 | myClass.fn(2); 64 | 65 | expect(myClass.fn(2)).to.equal(2); 66 | expect(_spy.callCount).to.equal(2); 67 | }); 68 | 69 | describe('with function resolver', () => { 70 | it('should resolve the key', () => { 71 | const _spy = spy(); 72 | 73 | class MyClass { 74 | @Memoize((i: any) => i.key) 75 | fn(item: any): any { 76 | _spy(); 77 | expect(this, 'context').to.equal(myClass); 78 | 79 | return item.value; 80 | } 81 | } 82 | 83 | const myClass = new MyClass(); 84 | 85 | myClass.fn({ key: 'test', value: 10 }); 86 | myClass.fn({ key: 'test', value: 20 }); 87 | myClass.fn({ key: 'test', value: 30 }); 88 | 89 | expect(myClass.fn({ key: 'test', value: 50 })).to.equal(10); 90 | expect(_spy.callCount).to.equal(1); 91 | 92 | myClass.fn({ key: 'test2', value: 30 }); 93 | 94 | expect(myClass.fn({ key: 'test2', value: 50 })).to.equal(30); 95 | expect(_spy.callCount).to.equal(2); 96 | }); 97 | }); 98 | 99 | describe('with string resolver', () => { 100 | it('should resolve the key', () => { 101 | const _spy = spy(); 102 | 103 | class MyClass { 104 | @Memoize('resolve') 105 | fn(item: any): any { 106 | _spy(); 107 | expect(this, 'context').to.equal(myClass); 108 | 109 | return item.value; 110 | } 111 | 112 | resolve(item: any): any { 113 | expect(this, 'resolver context').to.equal(myClass); 114 | 115 | return item.key; 116 | } 117 | } 118 | 119 | const myClass = new MyClass(); 120 | 121 | myClass.fn({ key: 'test', value: 10 }); 122 | myClass.fn({ key: 'test', value: 20 }); 123 | myClass.fn({ key: 'test', value: 30 }); 124 | 125 | expect(myClass.fn({ key: 'test', value: 50 })).to.equal(10); 126 | expect(_spy.callCount).to.equal(1); 127 | 128 | myClass.fn({ key: 'test2', value: 30 }); 129 | 130 | expect(myClass.fn({ key: 'test2', value: 50 })).to.equal(30); 131 | expect(_spy.callCount).to.equal(2); 132 | }); 133 | }); 134 | 135 | describe('with custom map type', () => { 136 | it('should create the map of type', () => { 137 | class MyClass { 138 | @Memoize({ type: WeakMap }) 139 | fn(item: any): any { 140 | return item.value; 141 | } 142 | } 143 | 144 | const myClass = new MyClass(); 145 | 146 | expect((myClass.fn as any).cache).to.be.an.instanceOf(WeakMap); 147 | }); 148 | }); 149 | 150 | describe('with custom cache', () => { 151 | it('should use the provided map', () => { 152 | const cache = new Map(); 153 | 154 | class MyClass { 155 | @Memoize({ cache }) 156 | fn(item: any): any { 157 | return item.value; 158 | } 159 | } 160 | 161 | const myClass = new MyClass(); 162 | const myClass2 = new MyClass(); 163 | 164 | expect((myClass.fn as any).cache).to.equal(cache); 165 | expect((myClass2.fn as any).cache).to.equal(cache); 166 | }); 167 | }); 168 | }); 169 | -------------------------------------------------------------------------------- /src/memoize.ts: -------------------------------------------------------------------------------- 1 | import memoize = require('lodash/memoize'); 2 | 3 | import { 4 | DecoratorConfig, 5 | DecoratorFactory, 6 | ResolvableFunction, 7 | BiTypedMethodDecorator1 8 | } from './factory'; 9 | import { MemoizeApplicator } from './applicators'; 10 | import { MemoizeConfig } from './shared'; 11 | 12 | /** 13 | * Creates a function that memoizes the result of func. If resolver is provided, 14 | * it determines the cache key for storing the result based on the arguments provided to the memoized function. 15 | * By default, the first argument provided to the memoized function is used as the map cache key. 16 | * The func is invoked with the this binding of the memoized function. 17 | * 18 | * You can use a Function or a string that references a method on the class as the resolver. 19 | * You can also use a configuration object that lets provide a prexisting cache or specify 20 | * the map type to use. 21 | * 22 | * @example 23 | * 24 | * class MyClass { 25 | * @Memoize({ type: WeakMap }) 26 | * getName(item) { 27 | * return item.name; 28 | * } 29 | * 30 | * @Memoize('getName') 31 | * getLastName(item) { 32 | * return item.lastName; 33 | * } 34 | * } 35 | */ 36 | export const Memoize = DecoratorFactory.createInstanceDecorator( 37 | new DecoratorConfig(memoize, new MemoizeApplicator(), { optionalParams: true }) 38 | ) as BiTypedMethodDecorator1>; 39 | export { Memoize as memoize }; 40 | export default Memoize; 41 | -------------------------------------------------------------------------------- /src/memoizeAll.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { spy } from 'sinon'; 3 | 4 | import { MemoizeAll } from './memoizeAll'; 5 | 6 | describe('memoizeAll', () => { 7 | it('should memoize the function', () => { 8 | const _spy = spy(); 9 | 10 | class MyClass { 11 | @MemoizeAll() 12 | fn(n: number): number { 13 | _spy(n); 14 | expect(this, 'context').to.equal(myClass); 15 | 16 | return n; 17 | } 18 | } 19 | 20 | const myClass = new MyClass(); 21 | const myClass2 = new MyClass(); 22 | 23 | myClass.fn(1); 24 | myClass.fn(1); 25 | myClass.fn(1); 26 | myClass2.fn(1); 27 | myClass2.fn(1); 28 | myClass2.fn(1); 29 | 30 | expect(_spy.callCount).to.equal(1); 31 | }); 32 | 33 | it('should memoize the function (paramless)', () => { 34 | const _spy = spy(); 35 | 36 | class MyClass { 37 | @MemoizeAll 38 | fn(n: number): number { 39 | _spy(n); 40 | expect(this, 'context').to.equal(myClass); 41 | 42 | return n; 43 | } 44 | } 45 | 46 | const myClass = new MyClass(); 47 | const myClass2 = new MyClass(); 48 | 49 | myClass.fn(1); 50 | myClass.fn(1); 51 | myClass.fn(1); 52 | myClass2.fn(1); 53 | myClass2.fn(1); 54 | myClass2.fn(1); 55 | 56 | expect(_spy.callCount).to.equal(1); 57 | }); 58 | 59 | describe('with function resolver', () => { 60 | it('should resolve the key', () => { 61 | const _spy = spy(); 62 | 63 | class MyClass { 64 | @MemoizeAll((i: any) => i.key) 65 | fn(item: any): any { 66 | _spy(); 67 | expect(this, 'context').to.equal(myClass); 68 | 69 | return item.value; 70 | } 71 | } 72 | 73 | const myClass = new MyClass(); 74 | const myClass2 = new MyClass(); 75 | 76 | myClass.fn({ key: 'test', value: 10 }); 77 | myClass.fn({ key: 'test', value: 20 }); 78 | myClass.fn({ key: 'test', value: 30 }); 79 | myClass2.fn({ key: 'test', value: 40 }); 80 | myClass2.fn({ key: 'test', value: 50 }); 81 | 82 | expect(myClass.fn({ key: 'test', value: 50 })).to.equal(10); 83 | expect(_spy.callCount).to.equal(1); 84 | }); 85 | }); 86 | }); 87 | -------------------------------------------------------------------------------- /src/memoizeAll.ts: -------------------------------------------------------------------------------- 1 | import memoize = require('lodash/memoize'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator1 } from './factory'; 4 | import { MemoizeApplicator } from './applicators'; 5 | import { MemoizeConfig } from './shared'; 6 | 7 | /** 8 | * Memoizes a function on the prototype instead of the instance. All instances of the class use the same memoize cache. 9 | * @param {Function} [resolver] Optional resolver 10 | */ 11 | export const MemoizeAll = DecoratorFactory.createDecorator( 12 | new DecoratorConfig(memoize, new MemoizeApplicator(), { optionalParams: true }) 13 | ) as BiTypedMethodDecorator1>; 14 | export { MemoizeAll as memoizeAll }; 15 | export default MemoizeAll; 16 | -------------------------------------------------------------------------------- /src/mixin.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Mixin } from './mixin'; 4 | 5 | describe('mixin', () => { 6 | it('should mixin the object', () => { 7 | const myApi = { 8 | fn2() {} 9 | }; 10 | 11 | @Mixin(myApi) 12 | class MyClass { 13 | fn() {} 14 | } 15 | 16 | const myClass = new MyClass(); 17 | 18 | expect((myClass as any).fn2).to.be.a('function'); 19 | expect((MyClass.prototype as any).fn2).to.be.a('function'); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /src/mixin.ts: -------------------------------------------------------------------------------- 1 | import assign = require('lodash/assign'); 2 | 3 | /** 4 | * Mixins an object into the classes prototype. 5 | * @export 6 | * @param {...Object[]} srcs 7 | * @returns {ClassDecorator} 8 | * @example 9 | * 10 | * const myMixin = { 11 | * blorg: () => 'blorg!' 12 | * } 13 | * 14 | * @Mixin(myMixin) 15 | * class MyClass {} 16 | * 17 | * const myClass = new MyClass(); 18 | * 19 | * myClass.blorg(); // => 'blorg!' 20 | */ 21 | export function Mixin(...srcs: Object[]): ClassDecorator { 22 | return ((target: Function) => { 23 | assign(target.prototype, ...srcs); 24 | 25 | return target; 26 | }) as any; 27 | } 28 | export { Mixin as mixin }; 29 | export default Mixin; 30 | -------------------------------------------------------------------------------- /src/negate.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Negate } from './negate'; 4 | 5 | describe('negate', () => { 6 | it('should inverse the result of the function', () => { 7 | class MyClass { 8 | @Negate() 9 | fn() { 10 | return true; 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | 16 | expect(myClass.fn()).to.be.false; 17 | }); 18 | 19 | it('should inverse the result of the function (paramless)', () => { 20 | class MyClass { 21 | @Negate 22 | fn() { 23 | return true; 24 | } 25 | } 26 | 27 | const myClass = new MyClass(); 28 | 29 | expect(myClass.fn()).to.be.false; 30 | }); 31 | 32 | it('should inverse the result of the resolved function', () => { 33 | class MyClass { 34 | @Negate('fn') 35 | fn2: () => boolean; 36 | 37 | fn() { 38 | return true; 39 | } 40 | } 41 | 42 | const myClass = new MyClass(); 43 | 44 | expect(myClass.fn2()).to.be.false; 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /src/negate.ts: -------------------------------------------------------------------------------- 1 | import negate = require('lodash/negate'); 2 | 3 | import { 4 | DecoratorConfig, 5 | DecoratorFactory, 6 | ResolvableFunction, 7 | BiTypedDecorator1 8 | } from './factory'; 9 | import { PartialValueApplicator } from './applicators'; 10 | 11 | /** 12 | * Negates a functions result or, when used on a property, creates a function that 13 | * negates the result of a provided function. 14 | * @param {ResolvableFunction} [fn] A resolvable function. 15 | * @example 16 | * class MyClass { 17 | * @Negate('fn') 18 | * fn2: () => boolean; 19 | * 20 | * fn(): boolean { 21 | * return true; 22 | * } 23 | * } 24 | * 25 | * const myClass = new MyClass(); 26 | * 27 | * myClass.fn2(); //=> false 28 | */ 29 | export const Negate = DecoratorFactory.createInstanceDecorator( 30 | new DecoratorConfig(negate, new PartialValueApplicator(), { property: true, optionalParams: true }) 31 | ) as BiTypedDecorator1; 32 | export { Negate as negate }; 33 | export default Negate; 34 | -------------------------------------------------------------------------------- /src/once.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Once } from './once'; 4 | 5 | describe('once', () => { 6 | it('should invoke the method only once', () => { 7 | let calls = 0; 8 | 9 | class MyClass { 10 | @Once() 11 | fn() { 12 | calls++; 13 | } 14 | } 15 | 16 | const myClass = new MyClass(); 17 | const myClass2 = new MyClass(); 18 | 19 | myClass.fn(); 20 | myClass.fn(); 21 | myClass.fn(); 22 | 23 | expect(calls, 'single class').to.equal(1); 24 | 25 | myClass2.fn(); 26 | myClass2.fn(); 27 | myClass2.fn(); 28 | 29 | expect(calls, 'multiple class').to.equal(2); 30 | }); 31 | }); -------------------------------------------------------------------------------- /src/once.ts: -------------------------------------------------------------------------------- 1 | import once = require('lodash/once'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedDecorator } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | /** 7 | * Creates a function that is restricted to invoking func once. Repeat calls to the function return the value of the first invocation. 8 | * @example 9 | * class MyClass { 10 | * value: number = 0; 11 | * 12 | * @Once() 13 | * fn(): number { 14 | * return ++this.value; 15 | * } 16 | * } 17 | * 18 | * const myClass = new MyClass(); 19 | * 20 | * myClass.fn(); //=> 1 21 | * myClass.fn(); //=> 1 22 | * myClass.fn(); //=> 1 23 | */ 24 | export const Once = DecoratorFactory.createInstanceDecorator( 25 | new DecoratorConfig(once, new PreValueApplicator(), { setter: true, optionalParams: true }) 26 | ) as BiTypedDecorator; 27 | export { Once as once }; 28 | export default Once; 29 | -------------------------------------------------------------------------------- /src/onceAll.ts: -------------------------------------------------------------------------------- 1 | import once = require('lodash/once'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedDecorator } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | /** 7 | * Creates a function that is restricted to invoking func once. Repeat calls to the function return the value of the first invocation. 8 | * This is shared across all instances of the class. 9 | * @example 10 | * const value = 0; 11 | * 12 | * class MyClass { 13 | * @Once() 14 | * fn(): number { 15 | * return ++value; 16 | * } 17 | * } 18 | * 19 | * const myClass = new MyClass(); 20 | * const myClass2 = new MyClass(); 21 | * 22 | * myClass.fn(); //=> 1 23 | * myClass2.fn(); //=> 1 24 | */ 25 | export const OnceAll = DecoratorFactory.createDecorator( 26 | new DecoratorConfig(once, new PreValueApplicator(), { setter: true, optionalParams: true }) 27 | ) as BiTypedDecorator; 28 | export { OnceAll as onceAll }; 29 | export default OnceAll; 30 | -------------------------------------------------------------------------------- /src/overArgs.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { OverArgs } from './overArgs'; 4 | 5 | const add = (n: number) => (v: number) => v + n; 6 | 7 | describe('overArgs', () => { 8 | it('should transform each argument', () => { 9 | class MyClass { 10 | @OverArgs(add(10), add(5)) 11 | fn(a: number, b: number): [ number, number ] { 12 | return [ a, b ]; 13 | } 14 | } 15 | 16 | const myClass = new MyClass(); 17 | 18 | expect(myClass.fn(5, 50)).to.eql([ 15, 55 ]); 19 | }); 20 | }); -------------------------------------------------------------------------------- /src/overArgs.ts: -------------------------------------------------------------------------------- 1 | import overArgs = require('lodash/overArgs'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashMethodDecorator } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | const decorator = DecoratorFactory.createDecorator( 7 | new DecoratorConfig(overArgs, new PreValueApplicator(), { setter: true }) 8 | ); 9 | 10 | /** 11 | * Creates a function that invokes func with its arguments transformed. 12 | * @export 13 | * @param {...Function[]} transforms 14 | * @returns {LodashMethodDecorator} 15 | * @example 16 | * class MyClass { 17 | * @OverArgs(_.castArray) 18 | * fn(list) { 19 | * return list; 20 | * } 21 | * } 22 | * 23 | * const myClass = new MyClass(); 24 | * 25 | * myClass.fn([ 1 ]); //=> [ 1 ]; 26 | * myClass.fn(1); //=> [ 1 ]; 27 | */ 28 | export function OverArgs(...transforms: Function[]): LodashMethodDecorator { 29 | return decorator(...transforms); 30 | } 31 | export { OverArgs as overArgs }; 32 | export default decorator; 33 | -------------------------------------------------------------------------------- /src/partial.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Partial } from './partial'; 4 | 5 | describe('partial', () => { 6 | it('should create a partially applied function', () => { 7 | class MyClass { 8 | lastName: string = 'Schmo'; 9 | 10 | @Partial('fn', 'Joe') 11 | fn2: () => string; 12 | 13 | fn(name: string): string { 14 | return `${name} ${this.lastName}`; 15 | } 16 | } 17 | 18 | const myClass = new MyClass(); 19 | 20 | expect(myClass.fn2).to.be.a('function'); 21 | expect(myClass.fn2()).to.equal('Joe Schmo'); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /src/partial.ts: -------------------------------------------------------------------------------- 1 | import partial = require('lodash/partial'); 2 | 3 | import { DecoratorConfig, DecoratorFactory } from './factory'; 4 | import { PartialApplicator } from './applicators'; 5 | 6 | const decorator = DecoratorFactory.createInstanceDecorator( 7 | new DecoratorConfig(partial, new PartialApplicator(), { property: true, method: false }) 8 | ); 9 | 10 | /** 11 | * Partially applies arguments to a function. 12 | * @export 13 | * @param {...any[]} partials 14 | * @returns {PropertyDecorator} 15 | * @example 16 | * class MyClass { 17 | * lastName: string = 'Schmo'; 18 | * 19 | * @Partial('fn', 'Joe') 20 | * fn2: () => string; 21 | * 22 | * fn(name: string): string { 23 | * return `${name} ${this.lastName}`; 24 | * } 25 | * } 26 | * 27 | * const myClass = new MyClass(); 28 | * 29 | * myClass.fn2(); //=> 'Joe Schmo' 30 | */ 31 | export function Partial(...partials: any[]): PropertyDecorator { 32 | return decorator(...partials); 33 | } 34 | export { Partial as partial }; 35 | export default decorator; 36 | -------------------------------------------------------------------------------- /src/partialRight.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { PartialRight } from './partialRight'; 4 | 5 | describe('partialRight', () => { 6 | it('should create a partially applied function', () => { 7 | class MyClass { 8 | firstName: string = 'Avry'; 9 | 10 | @PartialRight('fn', 'Sojka') 11 | fn2: () => string; 12 | 13 | fn(name: string): string { 14 | return `${this.firstName} ${name}`; 15 | } 16 | } 17 | 18 | const myClass = new MyClass(); 19 | 20 | expect(myClass.fn2).to.be.a('function'); 21 | expect(myClass.fn2()).to.equal('Avry Sojka'); 22 | }); 23 | }); -------------------------------------------------------------------------------- /src/partialRight.ts: -------------------------------------------------------------------------------- 1 | import partialRight = require('lodash/partialRight'); 2 | 3 | import { DecoratorConfig, DecoratorFactory } from './factory'; 4 | import { PartialApplicator } from './applicators'; 5 | 6 | const decorator = DecoratorFactory.createInstanceDecorator( 7 | new DecoratorConfig(partialRight, new PartialApplicator(), { property: true, method: false }) 8 | ); 9 | 10 | export function PartialRight(...partials: any[]): PropertyDecorator { 11 | return decorator(...partials); 12 | } 13 | export { PartialRight as partialRight }; 14 | export default decorator; 15 | -------------------------------------------------------------------------------- /src/rearg.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Rearg } from './rearg'; 4 | 5 | describe('rearg', () => { 6 | it('should change the order of arguments', () => { 7 | class MyClass { 8 | @Rearg(2, 1, 0) 9 | fn(a: any, b: any, c: any): any[] { 10 | return [ a, b, c ]; 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | 16 | expect(myClass.fn(1, 2, 3)).to.eql([ 3, 2, 1 ]); 17 | }); 18 | 19 | it('should change the order of the resolved function', () => { 20 | class MyClass { 21 | @Rearg('fn', 2, 1, 0) 22 | fn2: Function; 23 | 24 | fn(a: any, b: any, c: any): any[] { 25 | return [ a, b, c ]; 26 | } 27 | } 28 | 29 | const myClass = new MyClass(); 30 | 31 | expect(myClass.fn2(1, 2, 3)).to.eql([ 3, 2, 1 ]); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /src/rearg.ts: -------------------------------------------------------------------------------- 1 | import rearg = require('lodash/rearg'); 2 | 3 | import { 4 | DecoratorConfig, 5 | DecoratorFactory, 6 | LodashDecorator, 7 | ResolvableFunction 8 | } from './factory'; 9 | import { PartialValueApplicator } from './applicators'; 10 | 11 | const decorator = DecoratorFactory.createInstanceDecorator( 12 | new DecoratorConfig(rearg, new PartialValueApplicator(), { property: true }) 13 | ); 14 | 15 | export function Rearg(indexes: ResolvableFunction | number | number[], ...args: Array): LodashDecorator { 16 | return decorator(indexes, ...args); 17 | } 18 | export { Rearg as rearg }; 19 | export default decorator; 20 | -------------------------------------------------------------------------------- /src/rest.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Rest } from './rest'; 4 | 5 | describe('rest', () => { 6 | it('should change the order of arguments', () => { 7 | class MyClass { 8 | @Rest(1) 9 | fn(...args: any[]) { 10 | expect(args.length).to.equal(2); 11 | expect(args[0]).to.equal(1); 12 | expect(args[1]).to.eql([ 2, 3, 4 ]); 13 | } 14 | } 15 | 16 | const myClass = new MyClass(); 17 | 18 | myClass.fn(1, 2, 3, 4); 19 | }); 20 | 21 | it('should change the order of arguments (paramless)', () => { 22 | class MyClass { 23 | @Rest 24 | fn(...args: any[]) { 25 | expect(args.length).to.equal(1); 26 | expect(args[0]).to.eql([ 1, 2, 3, 4 ]); 27 | } 28 | } 29 | 30 | const myClass = new MyClass(); 31 | 32 | myClass.fn(1, 2, 3, 4); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /src/rest.ts: -------------------------------------------------------------------------------- 1 | import rest = require('lodash/rest'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator1 } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | export const Rest = DecoratorFactory.createDecorator( 7 | new DecoratorConfig(rest, new PreValueApplicator(), { optionalParams: true }) 8 | ) as BiTypedMethodDecorator1; 9 | export { Rest as rest }; 10 | export default Rest; 11 | -------------------------------------------------------------------------------- /src/shared.ts: -------------------------------------------------------------------------------- 1 | import { DecoratorConfig, ResolvableFunction } from './factory'; 2 | 3 | export interface DebounceOptions { 4 | wait?: number; 5 | leading?: boolean; 6 | trailing?: boolean; 7 | maxWait?: number; 8 | } 9 | 10 | export interface ApplicateOptions { 11 | config: DecoratorConfig; 12 | target: any; 13 | value: any; 14 | args: any[]; 15 | instance?: Object; 16 | } 17 | 18 | export interface ThrottleOptions { 19 | leading?: boolean; 20 | trailing?: boolean; 21 | } 22 | 23 | export interface MemoizeMap { 24 | get(key: T): U; 25 | has(key: T): boolean; 26 | set(key: T, value: U): void; 27 | delete(key: T): void; 28 | clear?(): void; 29 | } 30 | 31 | export interface MemoizeConfig { 32 | resolver?: ResolvableFunction; 33 | type?: { new (...args: any[]): MemoizeMap }; 34 | cache?: MemoizeMap; 35 | } -------------------------------------------------------------------------------- /src/spread.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Spread } from './spread'; 4 | 5 | describe('spread', () => { 6 | it('should spread the arguments', () => { 7 | class MyClass { 8 | @Spread() 9 | fn(...args: any[]) { 10 | expect(args.length).to.equal(4); 11 | expect(args).to.eql([ 1, 2, 3, 4 ]); 12 | } 13 | } 14 | 15 | const myClass = new MyClass(); 16 | 17 | myClass.fn([ 1, 2, 3, 4 ]); 18 | }); 19 | 20 | it('should spread the arguments (paramless)', () => { 21 | class MyClass { 22 | @Spread 23 | fn(...args: any[]) { 24 | expect(args.length).to.equal(4); 25 | expect(args).to.eql([ 1, 2, 3, 4 ]); 26 | } 27 | } 28 | 29 | const myClass = new MyClass(); 30 | 31 | myClass.fn([ 1, 2, 3, 4 ]); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /src/spread.ts: -------------------------------------------------------------------------------- 1 | import spread = require('lodash/spread'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator1 } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | export const Spread = DecoratorFactory.createDecorator( 7 | new DecoratorConfig(spread, new PreValueApplicator(), { optionalParams: true }) 8 | ) as BiTypedMethodDecorator1; 9 | export { Spread as spread }; 10 | export default Spread; 11 | -------------------------------------------------------------------------------- /src/tap.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Tap } from './tap'; 4 | 5 | describe('tap', () => { 6 | it('should return the first argument', () => { 7 | class MyClass { 8 | @Tap() 9 | fn(n: number): any { 10 | return 10; 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | 16 | expect(myClass.fn(50)).to.equal(50); 17 | }); 18 | 19 | it('should return the first argument (paramless)', () => { 20 | class MyClass { 21 | @Tap 22 | fn(n: number): any { 23 | return 10; 24 | } 25 | } 26 | 27 | const myClass = new MyClass(); 28 | 29 | expect(myClass.fn(50)).to.equal(50); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /src/tap.ts: -------------------------------------------------------------------------------- 1 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator } from './factory'; 2 | import { PreValueApplicator } from './applicators'; 3 | import { returnAtIndex } from './utils'; 4 | 5 | /** 6 | * Returns the first argument from the function regardless of 7 | * the decorated functions return value. 8 | */ 9 | export const Tap = DecoratorFactory.createDecorator( 10 | new DecoratorConfig((fn: Function) => returnAtIndex(fn, 0), new PreValueApplicator(), { optionalParams: true }) 11 | ) as BiTypedMethodDecorator; 12 | export { Tap as tap }; 13 | export default Tap; 14 | -------------------------------------------------------------------------------- /src/throttle.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { spy } from 'sinon'; 3 | 4 | import { Throttle, ThrottleSetter, ThrottleGetter } from './throttle'; 5 | 6 | describe('throttle', () => { 7 | it('should throttle the method', (done) => { 8 | const _spy = spy(); 9 | 10 | class MyClass { 11 | @Throttle(10) 12 | fn(n: number) { 13 | _spy(); 14 | } 15 | } 16 | 17 | const myClass = new MyClass(); 18 | 19 | myClass.fn(1); 20 | myClass.fn(2); 21 | 22 | setTimeout(() => myClass.fn(3), 1); 23 | setTimeout(() => myClass.fn(4), 2); 24 | 25 | setTimeout(() => { 26 | expect(_spy.callCount).to.equal(2); 27 | done(); 28 | }, 20); 29 | }); 30 | 31 | it('should throttle the method (paramless)', (done) => { 32 | const _spy = spy(); 33 | 34 | class MyClass { 35 | @Throttle 36 | fn(n: number) { 37 | _spy(); 38 | } 39 | } 40 | 41 | const myClass = new MyClass(); 42 | 43 | myClass.fn(1); 44 | myClass.fn(2); 45 | 46 | setTimeout(() => { 47 | expect(_spy.callCount).to.equal(2); 48 | done(); 49 | }, 20); 50 | }); 51 | 52 | it('should debounce the property setter', (done) => { 53 | class MyClass { 54 | private _value: number = 100; 55 | 56 | @ThrottleSetter(10) 57 | set value(value: number) { 58 | this._value = value; 59 | } 60 | 61 | get value(): number { 62 | return this._value; 63 | } 64 | } 65 | 66 | const myClass = new MyClass(); 67 | 68 | myClass.value = 5; 69 | myClass.value = 15; 70 | 71 | setTimeout(() => myClass.value = 20, 5); 72 | 73 | expect(myClass.value).to.equal(5); 74 | 75 | setTimeout(() => { 76 | expect(myClass.value).to.equal(20); 77 | done(); 78 | }, 20); 79 | }); 80 | 81 | // Skipping this test for now. 82 | it.skip('should debounce the property getter', (done) => { 83 | class MyClass { 84 | private _value: number = 0; 85 | 86 | @ThrottleGetter(10) 87 | get value(): number { 88 | return ++this._value; 89 | } 90 | } 91 | 92 | const myClass = new MyClass(); 93 | 94 | expect(myClass.value).to.equal(1); 95 | expect(myClass.value).to.equal(1); 96 | 97 | setTimeout(() => expect(myClass.value).to.equal(1), 5); 98 | 99 | setTimeout(() => { 100 | expect(myClass.value).to.equal(2); 101 | done(); 102 | }, 20); 103 | }); 104 | 105 | it('should contain the cancel and flush methods', () => { 106 | class MyClass { 107 | @Throttle(10) 108 | fn() {} 109 | } 110 | 111 | const myClass = new MyClass(); 112 | 113 | expect((myClass.fn as any).cancel).to.be.a('function'); 114 | expect((myClass.fn as any).flush).to.be.a('function'); 115 | }); 116 | }); 117 | -------------------------------------------------------------------------------- /src/throttle.ts: -------------------------------------------------------------------------------- 1 | import throttle = require('lodash/throttle'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedDecorator2 } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | import { ThrottleOptions } from './shared'; 6 | 7 | export const Throttle = DecoratorFactory.createInstanceDecorator( 8 | new DecoratorConfig(throttle, new PreValueApplicator(), { setter: true, getter: true, optionalParams: true }) 9 | ) as BiTypedDecorator2; 10 | 11 | export const ThrottleGetter = DecoratorFactory.createInstanceDecorator( 12 | new DecoratorConfig(throttle, new PreValueApplicator(), { getter: true, optionalParams: true }) 13 | ) as BiTypedDecorator2; 14 | 15 | export const ThrottleSetter = DecoratorFactory.createInstanceDecorator( 16 | new DecoratorConfig(throttle, new PreValueApplicator(), { setter: true, optionalParams: true }) 17 | ) as BiTypedDecorator2; 18 | 19 | export { Throttle as throttle }; 20 | export { ThrottleGetter as throttleGetter }; 21 | export { ThrottleSetter as throttleSetter }; 22 | export default Throttle; 23 | -------------------------------------------------------------------------------- /src/throttleAll.ts: -------------------------------------------------------------------------------- 1 | import throttle = require('lodash/throttle'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, LodashMethodDecorator } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | import { ThrottleOptions } from './shared'; 6 | 7 | const decorator = DecoratorFactory.createDecorator( 8 | new DecoratorConfig(throttle, new PreValueApplicator(), { setter: true }) 9 | ); 10 | export function ThrottleAll(wait?: number, options?: ThrottleOptions): LodashMethodDecorator { 11 | return decorator(wait, options); 12 | } 13 | export { ThrottleAll as throttleAll }; 14 | export default decorator; 15 | -------------------------------------------------------------------------------- /src/unary.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Unary } from './unary'; 4 | 5 | describe('unary', () => { 6 | it('should only invoke with one argument', () => { 7 | class MyClass { 8 | @Unary() 9 | fn(...args: any[]): any { 10 | expect(args.length).to.equal(1); 11 | } 12 | } 13 | 14 | const myClass = new MyClass(); 15 | 16 | myClass.fn(1, 2, 3, 4); 17 | }); 18 | 19 | it('should only invoke with one argument (paramless)', () => { 20 | class MyClass { 21 | @Unary 22 | fn(...args: any[]): any { 23 | expect(args.length).to.equal(1); 24 | } 25 | } 26 | 27 | const myClass = new MyClass(); 28 | 29 | myClass.fn(1, 2, 3, 4); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /src/unary.ts: -------------------------------------------------------------------------------- 1 | import unary = require('lodash/unary'); 2 | 3 | import { DecoratorConfig, DecoratorFactory, BiTypedMethodDecorator } from './factory'; 4 | import { PreValueApplicator } from './applicators'; 5 | 6 | export const Unary = DecoratorFactory.createDecorator( 7 | new DecoratorConfig(unary, new PreValueApplicator(), { optionalParams: true }) 8 | ) as BiTypedMethodDecorator; 9 | export { Unary as unary }; 10 | export default Unary; 11 | -------------------------------------------------------------------------------- /src/utils.ts: -------------------------------------------------------------------------------- 1 | export * from './utils/log'; 2 | export * from './utils/resolveFunction'; 3 | export * from './utils/CompositeKeyWeakMap'; 4 | export * from './utils/returnAtIndex'; 5 | export * from './utils/copyMetaData'; 6 | export * from './utils/bind'; 7 | export * from './utils/wrapConstructor'; 8 | export * from './utils/assignAll'; 9 | export * from './utils/isDecoratorArgs'; 10 | export * from './utils/isPrototypeAccess'; 11 | -------------------------------------------------------------------------------- /src/utils/CompositeKeyWeakMap.ts: -------------------------------------------------------------------------------- 1 | import isUndefined = require('lodash/isUndefined'); 2 | 3 | /** 4 | * A map for weakly holding nested references. 5 | * @private 6 | * @export 7 | * @class CompositeKeyWeakMap 8 | * @template T 9 | */ 10 | export class CompositeKeyWeakMap { 11 | private _weakMap = new WeakMap(); 12 | 13 | set(keys: any[], value: T): void { 14 | let map = this._weakMap; 15 | 16 | for (let i = 0, len = keys.length - 1; i < len; i++) { 17 | const key = keys[i]; 18 | let next = map.get(key); 19 | 20 | if (!next) { 21 | next = new Map(); 22 | map.set(key, next); 23 | } 24 | 25 | map = next; 26 | } 27 | 28 | map.set(keys[keys.length - 1], value); 29 | } 30 | 31 | get(keys: any[]): T { 32 | let next = this._weakMap; 33 | 34 | for (let i = 0, len = keys.length; i < len; i++) { 35 | next = next.get(keys[i]); 36 | 37 | if (isUndefined(next)) { 38 | break; 39 | } 40 | } 41 | 42 | return next as any; 43 | } 44 | 45 | has(keys: any[]): boolean { 46 | return !isUndefined(this.get(keys)); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/utils/assignAll.ts: -------------------------------------------------------------------------------- 1 | import without = require('lodash/without'); 2 | import attempt = require('lodash/attempt'); 3 | import isObject = require('lodash/isObject'); 4 | 5 | /** 6 | * Assigns all properties from an object to another object including non enumerable 7 | * properties. 8 | * @export 9 | * @template T 10 | * @template U 11 | * @param {T} to 12 | * @param {U} from 13 | * @param {string[]} [excludes=[]] 14 | * @returns {T} 15 | */ 16 | export function assignAll(to: T, from: U, excludes: string[] = []): T { 17 | const properties = without(Object.getOwnPropertyNames(from), ...excludes); 18 | 19 | for (const prop of properties) { 20 | attempt(assignProperty, to, from, prop); 21 | } 22 | 23 | return to; 24 | } 25 | 26 | /** 27 | * Assigns a property from one object to another while retaining descriptor properties. 28 | * @export 29 | * @template T 30 | * @template U 31 | * @param {T} to 32 | * @param {U} from 33 | * @param {string} prop 34 | */ 35 | export function assignProperty(to: T, from: U, prop: string): void { 36 | const descriptor = Object.getOwnPropertyDescriptor(to, prop); 37 | 38 | if (!descriptor || descriptor.configurable) { 39 | const srcDescriptor = Object.getOwnPropertyDescriptor(from, prop); 40 | 41 | if (isObject(srcDescriptor)) { 42 | Object.defineProperty(to, prop, srcDescriptor); 43 | } else { 44 | (to as any)[prop] = (from as any)[prop]; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/utils/bind.ts: -------------------------------------------------------------------------------- 1 | import { copyMetadata } from './copyMetaData'; 2 | 3 | /** 4 | * Binds a function and copies metadata. 5 | * @private 6 | * @export 7 | * @param {Function} fn 8 | * @param {*} context 9 | * @returns {Function} 10 | */ 11 | export function bind(fn: Function, context: any): Function { 12 | return copyMetadata(fn.bind(context), fn); 13 | } -------------------------------------------------------------------------------- /src/utils/copyMetaData.ts: -------------------------------------------------------------------------------- 1 | import forOwn = require('lodash/forOwn'); 2 | 3 | /** 4 | * Used to copy over meta data from function to function. 5 | * If meta data is attached to a function. This can get lost 6 | * when wrapping functions. This tries to persist that. 7 | * @private 8 | */ 9 | export function copyMetadata(to: any, from: any): any { 10 | forOwn(from, (value: any, key: string) => to[key] = value); 11 | 12 | return to; 13 | } 14 | -------------------------------------------------------------------------------- /src/utils/isDecoratorArgs.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { isMethodOrPropertyDecoratorArgs } from './isDecoratorArgs'; 4 | 5 | describe('when determining if args are from a decorator', () => { 6 | describe('and when the arguments are gte to 2', () => { 7 | describe('and when the first argument is an object', () => { 8 | describe('and when the second argument is a string', () => { 9 | describe('and when the first argument has a constructor property', () => { 10 | describe('and when the constructors prototype is the first argument', () => { 11 | it('should return true', () => { 12 | class Test {} 13 | 14 | expect(isMethodOrPropertyDecoratorArgs(Test.prototype, 'test')).to.be.true; 15 | }); 16 | }); 17 | 18 | describe('and when the constructors prototype is not the first argument', () => { 19 | it('should return false', () => { 20 | class Test {} 21 | 22 | expect(isMethodOrPropertyDecoratorArgs({ constructor: Test }, 'test')).to.be.false; 23 | }); 24 | }); 25 | }); 26 | 27 | describe('and when the first argument does not have a constructor property', () => { 28 | it('should return false', () => { 29 | expect(isMethodOrPropertyDecoratorArgs({}, 'test')).to.be.false; 30 | }); 31 | }); 32 | }); 33 | 34 | describe('and when the second argument is not a string', () => { 35 | it('should return false', () => { 36 | expect(isMethodOrPropertyDecoratorArgs({}, 123)).to.be.false; 37 | }); 38 | }); 39 | }); 40 | 41 | describe('and when the first argument is not an object', () => { 42 | it('should return false', () => { 43 | expect(isMethodOrPropertyDecoratorArgs(true, 123)).to.be.false; 44 | }); 45 | }); 46 | }); 47 | 48 | describe('and when the arguments are gte to 2', () => { 49 | it('should return false', () => { 50 | class Test {} 51 | 52 | expect(isMethodOrPropertyDecoratorArgs(Test.prototype)).to.be.false; 53 | }); 54 | }); 55 | }); 56 | 57 | -------------------------------------------------------------------------------- /src/utils/isDecoratorArgs.ts: -------------------------------------------------------------------------------- 1 | import isObject = require('lodash/isObject'); 2 | import isFunction = require('lodash/isFunction'); 3 | import isString = require('lodash/isString'); 4 | 5 | export function isMethodOrPropertyDecoratorArgs(...args: any[]): boolean { 6 | return args.length >= 2 7 | && isObject(args[0]) 8 | && isString(args[1]) 9 | && isFunction(args[0].constructor) 10 | && args[0].constructor.prototype === args[0]; 11 | } 12 | -------------------------------------------------------------------------------- /src/utils/isPrototypeAccess.ts: -------------------------------------------------------------------------------- 1 | export function isPrototypeAccess(context: object, target: object): boolean { 2 | return context === target 3 | || (context.constructor !== target.constructor 4 | && Object.getPrototypeOf(this).constructor === target.constructor); 5 | } 6 | -------------------------------------------------------------------------------- /src/utils/log.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Creates a log message. 3 | * @private 4 | * @export 5 | * @param {string} [message=''] 6 | * @returns {string} 7 | */ 8 | export function log(message:string = ''): string { 9 | return `lodash-decorators -> ${message}`; 10 | } 11 | -------------------------------------------------------------------------------- /src/utils/resolveFunction.ts: -------------------------------------------------------------------------------- 1 | import isFunction = require('lodash/isFunction'); 2 | import isString = require('lodash/isString'); 3 | import { log } from './log'; 4 | 5 | /** 6 | * Resolves a function on the current target object. It first will 7 | * try and resolve on the context object, then the target object, 8 | * then an error will be thrown if the method can not be resolved. 9 | * @private 10 | * @param {Function|string} method The method or method name. 11 | * @param {Object} [context] The context object to resolve from. 12 | * @param {Object} [target] The target object to resolve from. 13 | * @returns {Function} The resolved function. 14 | */ 15 | export function resolveFunction(method?: string|Function, context?: any, target?: any, throwNotFound: boolean = true): any { 16 | if (isFunction(method)) { 17 | return method; 18 | } else if (isString(method)) { 19 | if (context && isFunction(context[method])) { 20 | return context[method]; 21 | } else if (target && isFunction(target[method])) { 22 | return target[method]; 23 | } 24 | } 25 | 26 | if (throwNotFound) { 27 | throw new ReferenceError(log(`Can not resolve method ${method} on any target Objects`)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/utils/returnAtIndex.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Creates a function the returns the specific index. 3 | * @private 4 | * @export 5 | * @param {Function} fn 6 | * @param {number} index 7 | * @returns {Function} 8 | */ 9 | export function returnAtIndex(fn: Function, index: number): Function { 10 | return function(...args: any[]): any { 11 | fn.call(this, ...args); 12 | 13 | return args[index]; 14 | }; 15 | } 16 | -------------------------------------------------------------------------------- /src/utils/wrapConstructor.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { wrapConstructor } from './wrapConstructor'; 4 | 5 | describe('when wrapping a constructor', () => { 6 | it('should retain the original name of the constructor', () => { 7 | class Test {} 8 | 9 | const Wrapper = wrapConstructor(Test, function Blorg() {}); 10 | 11 | expect(Wrapper.name).to.equal('Test'); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/utils/wrapConstructor.ts: -------------------------------------------------------------------------------- 1 | import { assignAll } from './assignAll'; 2 | 3 | const PROPERTY_EXCLUDES = [ 4 | 'length', 5 | 'name', 6 | 'arguments', 7 | 'called', 8 | 'prototype' 9 | ]; 10 | 11 | /** 12 | * Wraps a constructor in a wrapper function and copies all static properties 13 | * and methods to the new constructor. 14 | * @export 15 | * @param {Function} Ctor 16 | * @param {(Ctor: Function, ...args: any[]) => any} wrapper 17 | * @returns {Function} 18 | */ 19 | export function wrapConstructor(Ctor: Function, wrapper: (Ctor: Function, ...args: any[]) => any): Function { 20 | function ConstructorWrapper(...args: any[]) { 21 | return wrapper.call(this, Ctor, ...args); 22 | } 23 | 24 | ConstructorWrapper.prototype = Ctor.prototype; 25 | Object.defineProperty(ConstructorWrapper, 'name', { 26 | // These values should coincide with the default descriptor values for `name`. 27 | configurable: true, 28 | enumerable: false, 29 | value: Ctor.name, 30 | writable: false 31 | }); 32 | 33 | return assignAll(ConstructorWrapper, Ctor, PROPERTY_EXCLUDES); 34 | } 35 | -------------------------------------------------------------------------------- /src/wrap.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | 3 | import { Wrap } from './wrap'; 4 | 5 | describe('wrap', () => { 6 | it('should wrap the function', () => { 7 | class MyClass { 8 | @Wrap('fn') 9 | fn2(fn?: any, arg?: any): any { 10 | return fn(arg); 11 | } 12 | 13 | fn(n: any) { 14 | return n; 15 | } 16 | } 17 | 18 | const myClass = new MyClass(); 19 | 20 | expect(myClass.fn2(50)).to.equal(50); 21 | }); 22 | }); -------------------------------------------------------------------------------- /src/wrap.ts: -------------------------------------------------------------------------------- 1 | import wrap = require('lodash/wrap'); 2 | 3 | import { 4 | DecoratorConfig, 5 | DecoratorFactory, 6 | ResolvableFunction, 7 | LodashMethodDecorator 8 | } from './factory'; 9 | import { WrapApplicator } from './applicators'; 10 | 11 | const decorator = DecoratorFactory.createDecorator( 12 | new DecoratorConfig(wrap, new WrapApplicator()) 13 | ); 14 | 15 | export function Wrap(fnToWrap?: ResolvableFunction): LodashMethodDecorator { 16 | return decorator(fnToWrap); 17 | } 18 | export { Wrap as wrap }; 19 | export default decorator; 20 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./", 5 | "declaration": true, 6 | "declarationDir": "./" 7 | }, 8 | "exclude": [ 9 | "src/**/*.spec.ts" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /tsconfig.docs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "target": "es2015", 5 | "outDir": "./buildDocs", 6 | "sourceMap": false 7 | }, 8 | "exclude": [] 9 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "alwaysStrict": true, 4 | "experimentalDecorators": true, 5 | "moduleResolution": "node", 6 | "sourceMap": true, 7 | "inlineSources": true, 8 | "target": "es5", 9 | "strictNullChecks": true, 10 | "noImplicitAny": true, 11 | "noUnusedLocals": true, 12 | "importHelpers": true, 13 | "plugins": [ 14 | { "name": "tslint-language-service" } 15 | ], 16 | "lib": [ 17 | "es2015" 18 | ], 19 | "types": [ 20 | "node", 21 | "lodash", 22 | "mocha", 23 | "chai", 24 | "sinon" 25 | ] 26 | }, 27 | "include": [ 28 | "src/**/*" 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./test", 5 | "sourceMap": true 6 | }, 7 | "exclude": [] 8 | } 9 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": [ 4 | "tslint:recommended" 5 | ], 6 | "rules": { 7 | "quotemark": [ true, "single" ], 8 | "prefer-template": true, 9 | "prefer-const": true, 10 | "semicolon": [ true, "always" ], 11 | "ordered-imports": false, 12 | "trailing-comma": false, 13 | "eofline": false, 14 | "max-line-length": false, 15 | "max-classes-per-file": false, 16 | "member-access": false, 17 | "variable-name": [ true, "check-format", "allow-leading-underscore", "allow-pascal-case" ], 18 | "ban-types": false, 19 | "interface-name": false, 20 | "no-unused-expression": false, 21 | "only-arrow-functions": false, 22 | "no-empty": false 23 | } 24 | } 25 | --------------------------------------------------------------------------------