index.mjs
1.5 KB
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
export function klona(x) {
if (typeof x !== 'object') return x;
var k, tmp, str=Object.prototype.toString.call(x);
if (str === '[object Object]') {
if (x.constructor !== Object && typeof x.constructor === 'function') {
tmp = new x.constructor();
for (k in x) {
if (tmp.hasOwnProperty(k) && tmp[k] !== x[k]) {
tmp[k] = klona(x[k]);
}
}
} else {
tmp = {}; // null
for (k in x) {
if (k === '__proto__') {
Object.defineProperty(tmp, k, {
value: klona(x[k]),
configurable: true,
enumerable: true,
writable: true,
});
} else {
tmp[k] = klona(x[k]);
}
}
}
return tmp;
}
if (str === '[object Array]') {
k = x.length;
for (tmp=Array(k); k--;) {
tmp[k] = klona(x[k]);
}
return tmp;
}
if (str === '[object Set]') {
tmp = new Set;
x.forEach(function (val) {
tmp.add(klona(val));
});
return tmp;
}
if (str === '[object Map]') {
tmp = new Map;
x.forEach(function (val, key) {
tmp.set(klona(key), klona(val));
});
return tmp;
}
if (str === '[object Date]') {
return new Date(+x);
}
if (str === '[object RegExp]') {
tmp = new RegExp(x.source, x.flags);
tmp.lastIndex = x.lastIndex;
return tmp;
}
if (str === '[object DataView]') {
return new x.constructor( klona(x.buffer) );
}
if (str === '[object ArrayBuffer]') {
return x.slice(0);
}
// ArrayBuffer.isView(x)
// ~> `new` bcuz `Buffer.slice` => ref
if (str.slice(-6) === 'Array]') {
return new x.constructor(x);
}
return x;
}