UNPKG

1.29 kBJavaScriptView Raw
1var toString = Object.prototype.toString;
2/**
3 * Deeply clones a value to create a new instance.
4 */
5export function cloneDeep(value) {
6 return cloneDeepHelper(value);
7}
8function cloneDeepHelper(val, seen) {
9 switch (toString.call(val)) {
10 case "[object Array]": {
11 seen = seen || new Map();
12 if (seen.has(val))
13 return seen.get(val);
14 var copy_1 = val.slice(0);
15 seen.set(val, copy_1);
16 copy_1.forEach(function (child, i) {
17 copy_1[i] = cloneDeepHelper(child, seen);
18 });
19 return copy_1;
20 }
21 case "[object Object]": {
22 seen = seen || new Map();
23 if (seen.has(val))
24 return seen.get(val);
25 // High fidelity polyfills of Object.create and Object.getPrototypeOf are
26 // possible in all JS environments, so we will assume they exist/work.
27 var copy_2 = Object.create(Object.getPrototypeOf(val));
28 seen.set(val, copy_2);
29 Object.keys(val).forEach(function (key) {
30 copy_2[key] = cloneDeepHelper(val[key], seen);
31 });
32 return copy_2;
33 }
34 default:
35 return val;
36 }
37}
38//# sourceMappingURL=cloneDeep.js.map
\No newline at end of file