higher-order-reducers
Version:
A library of simple everyday reducer utility functions
23 lines (19 loc) • 643 B
JavaScript
const linkedChain = (reducers = []) => {
for (let i = 0; i < reducers.length; i += 1) {
if (typeof reducers[i] !== 'function') {
throw new Error(`Element at position ${i} in "linkedChain" is not a reducer`);
}
}
const getNext = (position, action) => (state) => {
if (position >= reducers.length) {
return state;
}
const next = getNext(position + 1, action);
if (reducers[position].length < 3) {
return next(reducers[position](state, action));
}
return reducers[position](state, action, next);
};
return (state, action) => getNext(0, action)(state);
};
export default linkedChain;