Functional chain
A micro engine to create a chainable function APIs
Installation
npm install --save functional-chain
Basic example
Two arguments must be passed to the Chainable
method in order to generate the function chain
- An object of function builders. Each representing one method of the api
- A
"resolve"
method, that will receive an array of all the functions, that have been chained. Its role is to call the functions and return the final output
const Chainable = ; const API = const multiplyBy = API; const operation = and and; // 33 * 2 => 66 // 66 - 6 => 60 // 60 / 2 => 30 const res = ; console // -> 30
Usage case example: Redux reducer
Let's write an example of a Redux store, which uses our chainable methods to apply transformations to the store.
First version, a standard reducer:
const initialState = requestCount: 0 loading: false // ...; ;
Now, we take this a we try to add it some fun function chaining.
Let's build a little helper API to combine changes together
const API = ;
Now we use that in our store to define state transformations
const increment decrement = ; const transformations = "START_REQUEST": and; "END_REQUEST": and;;