clone.js
5.9 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use strict";
var clone = function() {
function _instanceof(obj, type) {
return type != null && obj instanceof type;
}
var nativeMap;
try {
nativeMap = Map;
} catch (_) {
nativeMap = function() {
};
}
var nativeSet;
try {
nativeSet = Set;
} catch (_) {
nativeSet = function() {
};
}
var nativePromise;
try {
nativePromise = Promise;
} catch (_) {
nativePromise = function() {
};
}
function clone2(parent, circular, depth, prototype, includeNonEnumerable) {
if (typeof circular === "object") {
depth = circular.depth;
prototype = circular.prototype;
includeNonEnumerable = circular.includeNonEnumerable;
circular = circular.circular;
}
var allParents = [];
var allChildren = [];
var useBuffer = typeof Buffer != "undefined";
if (typeof circular == "undefined")
circular = true;
if (typeof depth == "undefined")
depth = Infinity;
function _clone(parent2, depth2) {
if (parent2 === null)
return null;
if (depth2 === 0)
return parent2;
var child;
var proto;
if (typeof parent2 != "object") {
return parent2;
}
if (_instanceof(parent2, nativeMap)) {
child = new nativeMap();
} else if (_instanceof(parent2, nativeSet)) {
child = new nativeSet();
} else if (_instanceof(parent2, nativePromise)) {
child = new nativePromise(function(resolve, reject) {
parent2.then(function(value) {
resolve(_clone(value, depth2 - 1));
}, function(err) {
reject(_clone(err, depth2 - 1));
});
});
} else if (clone2.__isArray(parent2)) {
child = [];
} else if (clone2.__isRegExp(parent2)) {
child = new RegExp(parent2.source, __getRegExpFlags(parent2));
if (parent2.lastIndex)
child.lastIndex = parent2.lastIndex;
} else if (clone2.__isDate(parent2)) {
child = new Date(parent2.getTime());
} else if (useBuffer && Buffer.isBuffer(parent2)) {
if (Buffer.from) {
child = Buffer.from(parent2);
} else {
child = new Buffer(parent2.length);
parent2.copy(child);
}
return child;
} else if (_instanceof(parent2, Error)) {
child = Object.create(parent2);
} else {
if (typeof prototype == "undefined") {
proto = Object.getPrototypeOf(parent2);
child = Object.create(proto);
} else {
child = Object.create(prototype);
proto = prototype;
}
}
if (circular) {
var index = allParents.indexOf(parent2);
if (index != -1) {
return allChildren[index];
}
allParents.push(parent2);
allChildren.push(child);
}
if (_instanceof(parent2, nativeMap)) {
parent2.forEach(function(value, key) {
var keyChild = _clone(key, depth2 - 1);
var valueChild = _clone(value, depth2 - 1);
child.set(keyChild, valueChild);
});
}
if (_instanceof(parent2, nativeSet)) {
parent2.forEach(function(value) {
var entryChild = _clone(value, depth2 - 1);
child.add(entryChild);
});
}
for (var i in parent2) {
var attrs = Object.getOwnPropertyDescriptor(parent2, i);
if (attrs) {
child[i] = _clone(parent2[i], depth2 - 1);
}
try {
var objProperty = Object.getOwnPropertyDescriptor(parent2, i);
if (objProperty.set === "undefined") {
continue;
}
child[i] = _clone(parent2[i], depth2 - 1);
} catch (e) {
if (e instanceof TypeError) {
continue;
} else if (e instanceof ReferenceError) {
continue;
}
}
}
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(parent2);
for (var i = 0; i < symbols.length; i++) {
var symbol = symbols[i];
var descriptor = Object.getOwnPropertyDescriptor(parent2, symbol);
if (descriptor && !descriptor.enumerable && !includeNonEnumerable) {
continue;
}
child[symbol] = _clone(parent2[symbol], depth2 - 1);
Object.defineProperty(child, symbol, descriptor);
}
}
if (includeNonEnumerable) {
var allPropertyNames = Object.getOwnPropertyNames(parent2);
for (var i = 0; i < allPropertyNames.length; i++) {
var propertyName = allPropertyNames[i];
var descriptor = Object.getOwnPropertyDescriptor(parent2, propertyName);
if (descriptor && descriptor.enumerable) {
continue;
}
child[propertyName] = _clone(parent2[propertyName], depth2 - 1);
Object.defineProperty(child, propertyName, descriptor);
}
}
return child;
}
return _clone(parent, depth);
}
clone2.clonePrototype = function clonePrototype(parent) {
if (parent === null)
return null;
var c = function() {
};
c.prototype = parent;
return new c();
};
function __objToStr(o) {
return Object.prototype.toString.call(o);
}
clone2.__objToStr = __objToStr;
function __isDate(o) {
return typeof o === "object" && __objToStr(o) === "[object Date]";
}
clone2.__isDate = __isDate;
function __isArray(o) {
return typeof o === "object" && __objToStr(o) === "[object Array]";
}
clone2.__isArray = __isArray;
function __isRegExp(o) {
return typeof o === "object" && __objToStr(o) === "[object RegExp]";
}
clone2.__isRegExp = __isRegExp;
function __getRegExpFlags(re) {
var flags = "";
if (re.global)
flags += "g";
if (re.ignoreCase)
flags += "i";
if (re.multiline)
flags += "m";
return flags;
}
clone2.__getRegExpFlags = __getRegExpFlags;
return clone2;
}();
exports.clone = clone;