node.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.composeReducers = exports.subReducer = exports.cowValueModel = undefined;
  6. var _cowValueModel = require("./cow-value-model");
  7. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  8. exports.cowValueModel = _cowValueModel.cowValueModel;
  9. var subReducer = exports.subReducer = function subReducer(key, reducer) {
  10. for (var _len = arguments.length, otherKeys = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  11. otherKeys[_key - 2] = arguments[_key];
  12. }
  13. var value = (0, _cowValueModel.cowValueModel)(key),
  14. otherParts = otherKeys.map(function (each) {
  15. return (0, _cowValueModel.cowValueModel)(each);
  16. });
  17. return function (state, action) {
  18. var newSubState = reducer.apply(undefined, [value(state), action].concat(_toConsumableArray(otherParts.map(function (value) {
  19. return value(state);
  20. }))));
  21. if (typeof newSubState === "undefined") {
  22. throw new Error("The '" + key + "' reducer must not return undefined.");
  23. }
  24. return value(state, newSubState);
  25. };
  26. };
  27. var composeReducers = exports.composeReducers = function composeReducers() {
  28. for (var _len2 = arguments.length, reducers = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  29. reducers[_key2] = arguments[_key2];
  30. }
  31. return function (state, action) {
  32. return reducers.reduce(function (x, r) {
  33. return r(x, action);
  34. }, state);
  35. };
  36. };