chains together a series of reducer functions through functional composition
npm install --save '@warren-bank/redux-compose-reducers'
const composeReducers = require('@warren-bank/redux-compose-reducers')
const reducer_1 = (state, action) => state + 1
const reducer_2 = (state, action) => state - 1
const reducer_chain_1 = composeReducers(reducer_1, reducer_2) // (state, action) => state + 1 - 1
const reducer_chain_2 = composeReducers(reducer_chain_1, reducer_1) // (state, action) => state + 1 - 1 + 1
const reducer_chain_3 = composeReducers(reducer_chain_1, reducer_2) // (state, action) => state + 1 - 1 - 1
const reducer_chain_4 = composeReducers(reducer_chain_2, reducer_chain_3) // (state, action) => state + 1 - 1 + 1 + 1 - 1 - 1
expect( reducer_1( 5, {}) ).toBe(6)
expect( reducer_2( 5, {}) ).toBe(4)
expect( reducer_chain_1(5, {}) ).toBe(5)
expect( reducer_chain_2(5, {}) ).toBe(6)
expect( reducer_chain_3(5, {}) ).toBe(4)
expect( reducer_chain_4(5, {}) ).toBe(5)
-
files in repo:
-
files hosted in CDN:
-
run the usage example:
-
global variable(s):
- window.composeReducers
-
https://github.com/micro-js/compose-reducers
- same outcome
- different methodology
- calls each reducer in a loop
- doesn't use functional composition
- calls each reducer in a loop
-
https://github.com/palantir/redoodle
- nearly identical to
@micro-js/compose-reducers
- adds TypeScript definitions
- restricts input/output value types
- disallows the "composed" reducer to be used for initialization
state
cannot beundefined
- disallows the "composed" reducer to be used for initialization
- restricts input/output value types
- nearly identical to
-
https://github.com/acdlite/reduce-reducers
- similar methodology to
@micro-js/compose-reducers
- makes clever use of
Array.reduce()
to iterate through the reducers in a loop- doesn't use functional composition
- makes clever use of
- similar methodology to
- copyright: Warren Bank
- license: GPL-2.0