-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathserializer.js
34 lines (32 loc) · 874 Bytes
/
serializer.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
export function replacer(_key, value) {
if (value instanceof RegExp) {
return {
__serialized_type: 'RegExp',
source: value.source,
flags: value.flags,
};
}
return value;
}
export function reviver(_key, value) {
if (typeof value === 'object' && value !== null) {
// eslint-disable-next-line no-underscore-dangle
if (value.__serialized_type === 'RegExp') {
return new RegExp(value.source, value.flags);
}
// eslint-disable-next-line no-underscore-dangle
else if (value.__serialized_type === 'Function') {
// eslint-disable-next-line no-new-func
return new Function(
...value.args,
` try {
return (${value.fnBody})(${value.args.join(',')})
} catch(err) {
console.error("[Invalid Function call]", err);
}
`
);
}
}
return value;
}