UNPKG

higher-order-reducers

Version:

A library of simple everyday reducer utility functions

28 lines (24 loc) 742 B
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var chain = function chain() { var reducers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; for (var i = 0; i < reducers.length; i += 1) { if (typeof reducers[i] !== 'function') { throw new Error('Element at position ' + i + ' in "chain" is not a reducer'); } } var getNext = function getNext(position, action) { return function (state) { if (position >= reducers.length) { return state; } return reducers[position](state, action, getNext(position + 1, action)); }; }; return function (state, action) { return getNext(0, action)(state); }; }; exports.default = chain;