https://github.com/fabiospampinato/call-chainer
Combine a regular function and a class so that methods of the class become chainable properties of the function that get called automatically.
https://github.com/fabiospampinato/call-chainer
automatic call chain methods options properties
Last synced: 4 months ago
JSON representation
Combine a regular function and a class so that methods of the class become chainable properties of the function that get called automatically.
- Host: GitHub
- URL: https://github.com/fabiospampinato/call-chainer
- Owner: fabiospampinato
- License: mit
- Created: 2021-07-22T01:07:37.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2025-01-25T18:58:03.000Z (over 1 year ago)
- Last Synced: 2025-10-13T06:32:16.924Z (8 months ago)
- Topics: automatic, call, chain, methods, options, properties
- Language: TypeScript
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# Call Chainer
Combine a regular function and a class so that methods of the class become chainable properties of the function that get called automatically.
## Install
```sh
npm install call-chainer
```
## Usage
```ts
import chainer from 'call-chainer';
// First of all you need a class containing some methods
// It can contain non-methods too, those are not attached to the function
class Methods {
flags = {
always: false,
skip: false,
todo: false
}
always = () => {
this.flags.always = true;
}
skip = () => {
this.flags.skip = true;
}
todo = () => {
this.flags.todo = true;
}
}
// Then you need a function
// The function must receive as its first argument an instance of Methods
// That first argument is not exposed in the final chained function
const fn = ( methods: Methods, title: string ) => {
console.log ( title );
console.log ( methods.flags );
};
// Finally you need to chain the two together to get a new, chained, function
const fnChained = chainer ( Methods, fn );
// Then you can call methods on the chained function just by retrieving them as properties
fnChained.always.skip ( 'My title' );
// => 'My Title'
// => { always: true, skip: true, todo: false }
fnChained ( 'My title' );
// => 'My Title'
// => { always: false, skip: false, todo: false }
```
## License
MIT © Fabio Spampinato