UNPKG

higher-order-reducers

Version:

A library of simple everyday reducer utility functions

33 lines (28 loc) 897 B
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var linkedChain = function linkedChain() { 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 "linkedChain" is not a reducer'); } } var getNext = function getNext(position, action) { return function (state) { if (position >= reducers.length) { return state; } var next = getNext(position + 1, action); if (reducers[position].length < 3) { return next(reducers[position](state, action)); } return reducers[position](state, action, next); }; }; return function (state, action) { return getNext(0, action)(state); }; }; exports.default = linkedChain;