1 | var toString = Object.prototype.toString;
|
2 |
|
3 |
|
4 |
|
5 | export function cloneDeep(value) {
|
6 | return cloneDeepHelper(value);
|
7 | }
|
8 | function 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 |
|
26 |
|
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 |
|
\ | No newline at end of file |