@vates/compose
Compose functions from left to right
Install
Installation of the npm package:
> npm install --save @vates/compose
Usage
import { compose } from '@vates/compose'
const add2 = x => x + 2
const mul3 = x => x * 3
const f = compose(add2, mul3)
console.log(f(5))
The call context (this
) of the composed function is forwarded to all functions.
The first function is called with all arguments of the composed function:
const add = (x, y) => x + y
const mul3 = x => x * 3
const f = compose(add, mul3)
console.log(f(4, 5))
Functions may also be passed in an array:
const f = compose([add2, mul3])
Options can be passed as first parameter:
const f = compose(
{
async: true,
right: true,
},
[add2, mul3]
)
Functions can receive extra parameters:
const isIn = (value, min, max) => min <= value && value <= max
const f = compose([
[add, 2],
[isIn, 3, 10],
])
console.log(f(1))
Note: if the first function is defined with extra parameters, it will only receive the first value passed to the composed function, instead of all the parameters.
Contributions
Contributions are very welcomed, either on the documentation or on
the code.
You may:
- report any issue
you've encountered;
- fork and create a pull request.
License
ISC © Vates SAS