UNPKG

higher-order-reducers

Version:

A library of simple everyday reducer utility functions

38 lines (28 loc) 1.52 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _isPlainObject = require('is-plain-object'); var _isPlainObject2 = _interopRequireDefault(_isPlainObject); var _objectAssign = require('object-assign'); var _objectAssign2 = _interopRequireDefault(_objectAssign); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var mergeStates = function mergeStates(reducer) { if (typeof reducer !== 'function') { throw new Error('Argument supplied to "mergeStates" is not a reducer'); } return function (state, action) { if (!(0, _isPlainObject2.default)(state)) { var type = typeof state === 'undefined' ? 'undefined' : _typeof(state); throw new Error('"mergeStates" only works with plain object states, but got a "' + type + '"'); } var nextState = reducer(state, action); if (!(0, _isPlainObject2.default)(nextState)) { var _type = typeof nextState === 'undefined' ? 'undefined' : _typeof(nextState); throw new Error('The reducer in "mergeStates" did not return a plain object. Instead it was a "' + _type + '"'); } return (0, _objectAssign2.default)({}, state, nextState); }; }; exports.default = mergeStates;