-
Notifications
You must be signed in to change notification settings - Fork 0
/
object-clone.js
81 lines (81 loc) · 2.43 KB
/
object-clone.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
if (!Symbol.clone) {
Symbol.clone = Symbol.for("Symbol.clone");
Date.prototype[Symbol.clone] = function* () {
yield new Date(this.valueOf());
};
Set.prototype[Symbol.clone] = function* (clone) {
const ref = new Set();
yield ref;
this.forEach((value) => ref.add(clone(value)));
};
Map.prototype[Symbol.clone] = function* (clone) {
const ref = new Map();
yield ref;
this.forEach((value, key) => ref.set(key, clone(value)));
};
[
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array,
BigInt64Array,
BigUint64Array,
].forEach((constructor) => {
constructor.prototype[Symbol.clone] = function* (clone) {
const ref = new this.constructor(this.length);
yield ref;
ref.set(this.map((value) => clone(value)));
};
});
RegExp.prototype[Symbol.clone] = function* () {
yield new RegExp(this);
};
Array.prototype[Symbol.clone] = function* (clone) {
const ref = new this.constructor(this.length);
yield ref;
this.forEach((value, index) => {
ref[index] = clone(value);
});
};
Object.prototype[Symbol.clone] = function* (clone) {
const ref = new this.constructor();
yield ref;
Object.entries(this).forEach(([key, value]) => {
ref[key] = clone(value);
});
};
}
if (typeof Object.clone !== "function") {
const unclonable = /^\[object (?:Undefined|Null|Boolean|Number|BigInt|String|Symbol|Module|Weak(?:Set|Map)|(?:Shared)?ArrayBuffer|DataView)\]$/;
const clone = (obj, map, methodMap) => {
if (unclonable.test(Object.prototype.toString.call(obj))) return obj;
if (map.has(obj)) return map.get(obj);
const cloneMethod =
methodMap.get(obj.constructor) ??
(Object.hasOwnProperty.call(obj, Symbol.clone) ||
Object.hasOwnProperty.call(obj.constructor.prototype, Symbol.clone))
? obj[Symbol.clone]
: null;
if (!cloneMethod) return obj;
const ref = { current: undefined };
const wrappedClone = (obj) => {
if (ref.current) {
return clone(obj, map, methodMap);
}
};
const cloneIterator = cloneMethod.call(obj, wrappedClone);
ref.current = cloneIterator.next().value;
map.set(obj, ref.current);
if (ref.current) {
cloneIterator.next();
}
return ref.current;
};
Object.clone = (obj, methodMap) =>
clone(obj, new Map(), methodMap || new Map());
}