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