-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (24 loc) · 779 Bytes
/
index.js
File metadata and controls
32 lines (24 loc) · 779 Bytes
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
function deepCopy(target, map = new WeakMap()) {
if (typeof target == null) return target;
if (target instanceof Date) return new Date(target);
if (target instanceof RegExp) return new RegExp(target);
if (typeof target === "object") {
const isArray = Array.isArray(target);
const data = isArray ? [] : [];
if (map.get(target)) return map.get(target);
const keys = isArray ? undefined : Object.keys(target);
map.set(data, target);
helper(keys || target, (value, key) => {
if (key) key = value;
data[key] = deepCopy(target[key], map);
});
return data;
} else return target;
}
function helper(array, iteratee) {
let index = 0;
const n = array.length;
while (index++ < n) {
iteratee(array[index], index);
}
}