1 | 'use strict';
|
2 |
|
3 | function fclone(obj, refs) {
|
4 | if (!obj || "object" !== typeof obj) return obj;
|
5 |
|
6 | if (obj instanceof Date) {
|
7 | let copy = new Date();
|
8 | copy.setTime(obj.getTime());
|
9 | return copy;
|
10 | }
|
11 |
|
12 | if (Buffer !== undefined && Buffer.isBuffer(obj)) {
|
13 | return new Buffer(obj);
|
14 | }
|
15 |
|
16 | if (!refs) { refs = []; }
|
17 |
|
18 | if (Array.isArray(obj)) {
|
19 | refs[refs.length] = obj;
|
20 | let l = obj.length;
|
21 | let i = -1;
|
22 | let copy = [];
|
23 |
|
24 | while (l > ++i) {
|
25 | copy[i] = ~refs.indexOf(obj[i]) ? '[Circular]' : fclone(obj[i], refs);
|
26 | }
|
27 |
|
28 | refs.length && refs.length--
|
29 | return copy;
|
30 | }
|
31 |
|
32 | refs[refs.length] = obj;
|
33 | let copy = {};
|
34 |
|
35 | if (obj instanceof Error) {
|
36 | copy.name = obj.name;
|
37 | copy.message = obj.message;
|
38 | copy.stack = obj.stack;
|
39 | refs.length && refs.length--
|
40 | return copy;
|
41 | }
|
42 |
|
43 | let keys = Object.keys(obj);
|
44 | let l = keys.length;
|
45 |
|
46 | while(l--) {
|
47 | let k = keys[l]
|
48 | copy[k] = ~refs.indexOf(obj[k]) ? '[Circular]' : fclone(obj[k], refs);
|
49 | }
|
50 |
|
51 | refs.length && refs.length--
|
52 | return copy;
|
53 | }
|