1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.pathToArr = pathToArr;
|
7 | exports.getSlashStrPath = getSlashStrPath;
|
8 | exports.getDotStrPath = getDotStrPath;
|
9 | exports.combineReducers = combineReducers;
|
10 | exports.pathFromMeta = pathFromMeta;
|
11 | exports.updateItemInArray = updateItemInArray;
|
12 | exports.createReducer = createReducer;
|
13 | exports.preserveValuesFromState = preserveValuesFromState;
|
14 |
|
15 | var _flatten2 = _interopRequireDefault(require("lodash/flatten"));
|
16 |
|
17 | var _trimStart2 = _interopRequireDefault(require("lodash/trimStart"));
|
18 |
|
19 | var _replace2 = _interopRequireDefault(require("lodash/replace"));
|
20 |
|
21 | var _pick2 = _interopRequireDefault(require("lodash/pick"));
|
22 |
|
23 | var _isArray2 = _interopRequireDefault(require("lodash/isArray"));
|
24 |
|
25 | var _isBoolean2 = _interopRequireDefault(require("lodash/isBoolean"));
|
26 |
|
27 | var _isFunction2 = _interopRequireDefault(require("lodash/isFunction"));
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 |
|
31 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
32 |
|
33 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
34 |
|
35 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
|
36 |
|
37 | function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
|
38 |
|
39 | function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
|
40 |
|
41 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
|
42 |
|
43 | function pathToArr(path) {
|
44 | return path ? path.split(/\//).filter(function (p) {
|
45 | return !!p;
|
46 | }) : [];
|
47 | }
|
48 |
|
49 | function getSlashStrPath(path) {
|
50 | return (0, _trimStart2.default)((0, _replace2.default)(path, /[.]/g, '/'), '/');
|
51 | }
|
52 |
|
53 | function getDotStrPath(path) {
|
54 | return pathToArr(path).join('.');
|
55 | }
|
56 |
|
57 | function combineReducers(reducers) {
|
58 | return function () {
|
59 | var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
60 | var action = arguments.length > 1 ? arguments[1] : undefined;
|
61 | return Object.keys(reducers).reduce(function (nextState, key) {
|
62 | nextState[key] = reducers[key](state[key], action);
|
63 | return nextState;
|
64 | }, {});
|
65 | };
|
66 | }
|
67 |
|
68 | function pathFromMeta(meta) {
|
69 | if (!meta) {
|
70 | throw new Error('Action meta is required to build path for reducers.');
|
71 | }
|
72 |
|
73 | var collection = meta.collection,
|
74 | doc = meta.doc,
|
75 | subcollections = meta.subcollections,
|
76 | storeAs = meta.storeAs;
|
77 |
|
78 | if (storeAs) {
|
79 | return doc ? [storeAs, doc] : [storeAs];
|
80 | }
|
81 |
|
82 | if (meta.path) {
|
83 | return meta.path.split('/');
|
84 | }
|
85 |
|
86 | if (!collection) {
|
87 | throw new Error('Collection is required to construct reducer path.');
|
88 | }
|
89 |
|
90 | var basePath = [collection];
|
91 |
|
92 | if (doc) {
|
93 | basePath = [].concat(_toConsumableArray(basePath), [doc]);
|
94 | }
|
95 |
|
96 | if (!subcollections) {
|
97 | return basePath;
|
98 | }
|
99 |
|
100 | var mappedCollections = subcollections.map(pathFromMeta);
|
101 | return [].concat(_toConsumableArray(basePath), _toConsumableArray((0, _flatten2.default)(mappedCollections)));
|
102 | }
|
103 |
|
104 | function updateItemInArray(array, itemId, updateItemCallback) {
|
105 | var matchFound = false;
|
106 | var modified = array.map(function (item) {
|
107 | if (!item || item.id !== itemId) {
|
108 | return item;
|
109 | }
|
110 |
|
111 | matchFound = true;
|
112 | var updatedItem = updateItemCallback(item);
|
113 | return updatedItem;
|
114 | });
|
115 |
|
116 | if (!matchFound) {
|
117 | modified.push(updateItemCallback({
|
118 | id: itemId
|
119 | }));
|
120 | }
|
121 |
|
122 | return modified;
|
123 | }
|
124 |
|
125 | function createReducer(initialState, handlers) {
|
126 | return function reducer() {
|
127 | var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
|
128 | var action = arguments.length > 1 ? arguments[1] : undefined;
|
129 |
|
130 | if (handlers.hasOwnProperty(action.type)) {
|
131 | return handlers[action.type](state, action);
|
132 | }
|
133 |
|
134 | return state;
|
135 | };
|
136 | }
|
137 |
|
138 | function preserveValuesFromState(state, preserveSetting, nextState) {
|
139 | if ((0, _isBoolean2.default)(preserveSetting)) {
|
140 | return nextState ? _objectSpread({}, state, nextState) : state;
|
141 | }
|
142 |
|
143 | if ((0, _isFunction2.default)(preserveSetting)) {
|
144 | return preserveSetting(state, nextState);
|
145 | }
|
146 |
|
147 | if ((0, _isArray2.default)(preserveSetting)) {
|
148 | return (0, _pick2.default)(state, preserveSetting);
|
149 | }
|
150 |
|
151 | throw new Error('Invalid preserve parameter. It must be an Object or an Array.');
|
152 | } |
\ | No newline at end of file |