-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
130 lines (114 loc) · 4.05 KB
/
index.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
import getParams from 'get-params';
import jsan from 'jsan';
import nanoid from 'nanoid/non-secure';
import seralizeImmutable from 'remotedev-serialize/immutable/serialize';
export function generateId(id) {
return id || nanoid(7);
}
function flatTree(obj, namespace = '') {
let functions = [];
Object.keys(obj).forEach(key => {
const prop = obj[key];
if (typeof prop === 'function') {
functions.push({
name: namespace + (key || prop.name || 'anonymous'),
func: prop,
args: getParams(prop),
});
} else if (typeof prop === 'object') {
functions = functions.concat(flatTree(prop, namespace + key + '.'));
}
});
return functions;
}
export function getMethods(obj) {
if (typeof obj !== 'object') return undefined;
let functions;
let m;
if (obj.__proto__) m = obj.__proto__.__proto__;
if (!m) m = obj;
Object.getOwnPropertyNames(m).forEach(key => {
const propDescriptor = Object.getOwnPropertyDescriptor(m, key);
if (!propDescriptor || 'get' in propDescriptor || 'set' in propDescriptor) return;
const prop = m[key];
if (typeof prop === 'function' && key !== 'constructor') {
if (!functions) functions = [];
functions.push({
name: key || prop.name || 'anonymous',
args: getParams(prop),
});
}
});
return functions;
}
export function getActionsArray(actionCreators) {
if (Array.isArray(actionCreators)) return actionCreators;
return flatTree(actionCreators);
}
/* eslint-disable no-new-func */
const interpretArg = (arg) => (new Function('return ' + arg))();
function evalArgs(inArgs, restArgs) {
const args = inArgs.map(interpretArg);
if (!restArgs) return args;
const rest = interpretArg(restArgs);
if (Array.isArray(rest)) return args.concat(...rest);
throw new Error('rest must be an array');
}
export function evalAction(action, actionCreators) {
if (typeof action === 'string') {
return (new Function('return ' + action))();
}
const actionCreator = actionCreators[action.selected].func;
const args = evalArgs(action.args, action.rest);
return actionCreator(...args);
}
export function evalMethod(action, obj) {
if (typeof action === 'string') {
return (new Function('return ' + action)).call(obj);
}
const args = evalArgs(action.args, action.rest);
return (new Function('args', `return this.${action.name}(args)`)).apply(obj, args);
}
/* eslint-enable */
function tryCatchStringify(obj) {
try {
return JSON.stringify(obj);
} catch (err) {
/* eslint-disable no-console */
if (process.env.NODE_ENV !== 'production') console.log('Failed to stringify', err);
/* eslint-enable no-console */
return jsan.stringify(obj, null, null, { circular: '[CIRCULAR]' });
}
}
export function stringify(obj, serialize) {
if (typeof serialize === 'undefined') {
return tryCatchStringify(obj);
}
if (serialize === true) {
return jsan.stringify(obj, function (key, value) {
if (value && typeof value.toJS === 'function') return value.toJS();
return value;
}, null, true);
}
return jsan.stringify(obj, serialize.replacer, null, serialize.options);
}
export function getSeralizeParameter(config, param) {
const serialize = config.serialize;
if (serialize) {
if (serialize === true) return { options: true };
if (serialize.immutable) {
return {
replacer: seralizeImmutable(serialize.immutable, serialize.refs).replacer,
options: serialize.options || true
};
}
if (!serialize.replacer) return { options: serialize.options };
return { replacer: serialize.replacer, options: serialize.options || true };
}
const value = config[param];
if (typeof value === 'undefined') return undefined;
console.warn(`\`${param}\` parameter for Redux DevTools Extension is deprecated. Use \`serialize\` parameter instead: https://github.com/zalmoxisus/redux-devtools-extension/releases/tag/v2.12.1`); // eslint-disable-line
if (typeof serializeState === 'boolean') return { options: value };
if (typeof serializeState === 'function') return { replacer: value };
return value;
}