-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
27 lines (25 loc) · 850 Bytes
/
index.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
function clone(type, hash = new WeakMap) {
const referenceTypes = ['Array', 'Object', 'Map', 'Set', 'Date'];
const isType = Object.prototype.toString.call(type);
if (
!new RegExp(referenceTypes.join('|')).test(isType) ||
type instanceof WeakMap ||
type instanceof WeakSet
) return type;
if (hash.has(type)) {
return hash.get(type);
}
const c = new type.constructor;
if (type instanceof Map) {
type.forEach((value, key) => c.set(clone(key), clone(value)));
}
if (type instanceof Set) {
type.forEach((value) => c.add(clone(value)));
}
if (type instanceof Date) {
return new Date(type);
}
hash.set(type, c);
return Object.assign(c, ...Object.keys(type).map((prop) => ({ [prop]: clone(type[prop], hash) })));
}
export default clone;