-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduxdecorator.js
372 lines (328 loc) · 10.8 KB
/
reduxdecorator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"use strict";
var _extends =
Object.assign ||
function(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
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;
};
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);
}
}
(function(global, factory) {
(typeof exports === "undefined" ? "undefined" : _typeof(exports)) ===
"object" && typeof module !== "undefined"
? factory(exports)
: typeof define === "function" && define.amd
? define(["exports"], factory)
: factory((global.reduxdecorator = {}));
})(undefined, function(exports) {
"use strict";
var combine = function combine() {
for (
var _len = arguments.length, funcs = Array(_len), _key = 0;
_key < _len;
_key++
) {
funcs[_key] = arguments[_key];
}
return function(state, action) {
return funcs.reduce(function(prev, cur) {
if (typeof cur === "function") {
return cur(prev, action);
} else {
return prev;
}
}, state);
};
};
var simple_reducer = function simple_reducer(key, callback, def) {
var simple_reducer_ret = function simple_reducer_ret() {
var state =
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : def;
var action =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
switch (action.type) {
case key:
state = callback(state, action);
}
return state;
};
simple_reducer_ret.reduxdecorator_key = key;
return simple_reducer_ret;
};
var number_transform = function number_transform(key) {
return function(func) {
return simple_reducer(key, function(state, action) {
return func(state, action);
});
};
};
var number_increment = function number_increment(key) {
return simple_reducer(key, function(state, action) {
return state + 1;
});
};
var number_decrement = function number_decrement(key) {
return simple_reducer(key, function(state, action) {
return state - 1;
});
};
var number_reducers = /*#__PURE__*/ Object.freeze({
number_transform: number_transform,
number_increment: number_increment,
number_decrement: number_decrement
});
var array_append = function array_append(key) {
return function(obj) {
return simple_reducer(key, function(state, action) {
return [].concat(_toConsumableArray(state), [obj(state, action)]);
});
};
};
var array_prepend = function array_prepend(key) {
return function(obj) {
return simple_reducer(key, function(state, action) {
return [obj(state, action)].concat(_toConsumableArray(state));
});
};
};
var array_remove_all = function array_remove_all(key) {
return simple_reducer(key, function() {
return [];
});
};
var array_remove_index = function array_remove_index(key) {
return function(index) {
return simple_reducer(key, function(state, action) {
var copy = [].concat(_toConsumableArray(state));
var ind = index(state, action);
if (!Number.isInteger(ind) || ind < 0 || ind >= copy.length) {
throw new Error("redux-decoratorerror: invalid index returned");
}
copy.splice(ind, 1);
return copy;
});
};
};
var array_set = function array_set(key) {
return function(obj) {
return simple_reducer(key, function(state, action) {
var newArr = obj(state, action);
if (!Array.isArray(newArr)) {
throw new Error(
"redux-decorator error: array_set must return an array"
);
}
//The user modified the original array state
if (newArr === state) {
throw new Error(
"redux-decorator error: original array was returned from array_set, please make sure you copy the state."
);
}
return newArr;
});
};
};
var array_reducers = /*#__PURE__*/ Object.freeze({
array_append: array_append,
array_prepend: array_prepend,
array_remove_all: array_remove_all,
array_remove_index: array_remove_index,
array_set: array_set
});
var boolean_set = function boolean_set(key) {
return function(func) {
return simple_reducer(key, func);
};
};
var boolean_toggle = function boolean_toggle(key) {
return simple_reducer(key, function(state, action) {
return !state;
});
};
var boolean_reducers = /*#__PURE__*/ Object.freeze({
boolean_set: boolean_set,
boolean_toggle: boolean_toggle
});
function default_state(def) {
return function default_state_ret() {
var state =
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : def;
var action = arguments[1];
return state;
};
}
var array_append$1 = array_reducers.array_append,
array_prepend$1 = array_reducers.array_prepend,
array_remove_all$1 = array_reducers.array_remove_all,
array_remove_index$1 = array_reducers.array_remove_index,
array_set$1 = array_reducers.array_set;
var boolean_set$1 = boolean_reducers.boolean_set,
boolean_toggle$1 = boolean_reducers.boolean_toggle;
var number_transform$1 = number_reducers.number_transform,
number_increment$1 = number_reducers.number_increment,
number_decrement$1 = number_reducers.number_decrement;
function redup(objKey, actionType, initialState) {
return function redup_return(target, key, descriptor) {
var descriptorCopy = _extends({}, descriptor);
descriptorCopy.writable = false;
descriptorCopy.enumerable = false;
var nested = objKey.indexOf(".") != -1;
if (nested) {
var splitKey = objKey.split(".");
for (var i = 0; i < splitKey.length - 1; i++) {
var _key2 = splitKey[i];
if (!(_key2 in target)) {
target[_key2] = {};
}
target = target[_key2];
}
objKey = splitKey[splitKey.length - 1];
}
var reducer =
"initializer" in descriptor
? descriptor.initializer()
: descriptor.value;
if (typeof reducer !== "function") {
throw new Error(
"redux-decorator: Invalid reducer function " + reducer.toString()
);
}
if (target[objKey]) {
if (!Array.isArray(target[objKey])) throw new Error("Invalid target");
var arr = target[objKey];
arr[arr.length] = simple_reducer(actionType, reducer, initialState);
} else {
target[objKey] = [simple_reducer(actionType, reducer, initialState)];
}
return descriptorCopy;
};
}
function create_reducer(tree) {
var options =
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cache =
arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var nested =
arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
function create_reducer_ret() {
var prevState =
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var action = arguments[1];
var cacheHasKey = function cacheHasKey(key) {
return cache && cache[key];
};
var optionsCopy = Object.assign({}, options);
if (!("useCache" in optionsCopy)) {
optionsCopy.useCache = true;
}
var isObject = function isObject(obj) {
return (
obj === Object(obj) &&
Object.prototype.toString.call(obj) !== "[object Array]"
);
};
if (!tree || !isObject(tree)) {
throw new Error("Invalid tree");
}
var callReducers = function callReducers(nested) {
return function(prev, cur) {
if (typeof cur === "function") {
//Lazy load items into cache
if (cur.reduxdecorator_key && optionsCopy.useCache) {
cur.reduxdecorator_property = [].concat(
_toConsumableArray(nested)
);
cache[cur.reduxdecorator_key] = cur;
}
return cur(prev, action);
} else {
return prev;
}
};
};
if (
cacheHasKey(action.type) &&
typeof cache[action.type] === "function" &&
"reduxdecorator_property" in cache[action.type] &&
optionsCopy.useCache
) {
var reducer = cache[action.type];
if (!("reduxdecorator_property" in reducer)) {
throw new Error("Missing reduxdecorator_property");
}
var properties = reducer.reduxdecorator_property;
var last = properties[properties.length - 1];
var nextToLast = properties[properties.length - 2];
if (properties.length == 1) {
prevState[properties[0]] = reducer(prevState[properties[0]], action);
} else {
var current = null;
for (var i = 0; i < properties.length - 1; i++) {
current = prevState[properties[i]];
}
current[last] = reducer(current[last], action);
}
} else {
for (var key in tree) {
var val = tree[key];
nested[nested.length] = key;
if (isObject(val)) {
prevState[key] = prevState[key] || {};
create_reducer(val, options, cache, nested)(prevState[key], action);
} else if (Array.isArray(val)) {
prevState[key] = val.reduce(callReducers(nested), prevState[key]);
} else {
prevState[key] = val;
}
nested = [];
}
}
return prevState;
}
return create_reducer_ret;
}
exports.create_reducer = create_reducer;
exports.combine = combine;
exports.array_append = array_append$1;
exports.array_prepend = array_prepend$1;
exports.array_remove_all = array_remove_all$1;
exports.array_remove_index = array_remove_index$1;
exports.array_set = array_set$1;
exports.boolean_set = boolean_set$1;
exports.boolean_toggle = boolean_toggle$1;
exports.number_transform = number_transform$1;
exports.number_increment = number_increment$1;
exports.number_decrement = number_decrement$1;
exports.simple_reducer = simple_reducer;
exports.default_state = default_state;
exports.redup = redup;
Object.defineProperty(exports, "__esModule", { value: true });
});