|
1 | | -const aggregator = (paths: any, object: any) => { |
2 | | - return paths.reduce((delta: any, path: any) => { |
3 | | - return set(delta, path, get(object, path)); |
4 | | - }, {}); |
5 | | -}; |
6 | | - |
7 | | -function assignInWith(target: any, source: any, customizer?: (targetValue: any, sourceValue: any) => any) { |
8 | | - Object.entries(source).forEach(([field, value]) => { |
9 | | - if (customizer) { |
10 | | - target[field] = customizer(target[field], value); |
11 | | - } else { |
12 | | - target[field] = value; |
13 | | - } |
14 | | - }); |
15 | | - return target; |
16 | | -} |
17 | | - |
18 | | -function isUndefined(value: any) { |
19 | | - return value === undefined; |
20 | | -} |
21 | | - |
22 | | -function isObject(value: any) { |
23 | | - const type = typeof value; |
24 | | - return value != null && (type === 'object' || type === 'function'); |
25 | | -} |
26 | | - |
27 | | -function isString(value: any): value is string { |
28 | | - return typeof value === 'string' || value instanceof String; |
29 | | -} |
30 | | - |
31 | | -function isFunction(value: any): value is (...args: any[]) => any { |
32 | | - return typeof value === 'function'; |
33 | | -} |
34 | | -function set(object: object, path: string, value: any) { |
35 | | - path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties |
36 | | - path = path.replace(/^\./, ''); // strip a leading dot |
37 | | - const paths = path.split('.'); |
38 | | - const lastProperty = paths.pop(); |
39 | | - const finalValue = paths.reduceRight( |
40 | | - (finalObject, prop) => { |
41 | | - return { [prop]: finalObject }; |
42 | | - }, |
43 | | - { [lastProperty]: value } |
44 | | - ); |
45 | | - |
46 | | - return { ...object, ...finalValue }; |
47 | | -} |
48 | | -function get(object: object, path: string) { |
49 | | - path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties |
50 | | - path = path.replace(/^\./, ''); // strip a leading dot |
51 | | - const a = path.split('.'); |
52 | | - for (let i = 0, n = a.length; i < n; ++i) { |
53 | | - const k = a[i]; |
54 | | - if (isObject(object) && k in object) { |
55 | | - object = object[k]; |
56 | | - } else { |
57 | | - return; |
58 | | - } |
59 | | - } |
60 | | - return object; |
61 | | -} |
62 | | - |
63 | | -function zipObject(props: string[], values: any[]) { |
64 | | - return props.reduce((prev, prop, i) => { |
65 | | - return { ...prev, [prop]: values[i] }; |
66 | | - }, {}); |
67 | | -} |
| 1 | +import { isString, get, isFunction, isObject, zipObject, isUndefined, assignInWith, aggregator } from './helpers'; |
68 | 2 |
|
69 | 3 | type ActionString = string; |
70 | 4 | interface ActionFunction { |
|
0 commit comments