MCPcopy
hub / github.com/Unitech/pm2 / fclone

Function fclone

modules/fclone.js:3–47  ·  view source on GitHub ↗
(obj, refs)

Source from the content-addressed store, hash-verified

1'use strict';
2
3function fclone(obj, refs) {
4 if (!obj || typeof obj !== 'object') return obj;
5
6 if (obj instanceof Date) return new Date(obj);
7 if (Buffer.isBuffer(obj)) return Buffer.from(obj);
8 if (ArrayBuffer.isView(obj)) return obj.slice(0);
9
10 if (!refs) refs = [];
11
12 // handle array-like objects (arguments, NodeList, etc.)
13 let isArr = Array.isArray(obj);
14 if (!isArr) {
15 let len = obj.length;
16 isArr = typeof len === 'number' && (len === 0 || (len - 1) in obj) && typeof obj.indexOf === 'function';
17 }
18
19 if (isArr) {
20 refs.push(obj);
21 let copy = new Array(obj.length);
22 for (let i = 0; i < obj.length; i++) {
23 let v = obj[i];
24 copy[i] = (v && typeof v === 'object' && refs.indexOf(v) !== -1) ? '[Circular]' : fclone(v, refs);
25 }
26 refs.pop();
27 return copy;
28 }
29
30 refs.push(obj);
31 let copy = {};
32
33 if (obj instanceof Error) {
34 copy.name = obj.name;
35 copy.message = obj.message;
36 copy.stack = obj.stack;
37 }
38
39 let keys = Object.keys(obj);
40 for (let i = 0; i < keys.length; i++) {
41 let v = obj[keys[i]];
42 copy[keys[i]] = (v && typeof v === 'object' && refs.indexOf(v) !== -1) ? '[Circular]' : fclone(v, refs);
43 }
44
45 refs.pop();
46 return copy;
47}
48
49module.exports = fclone;

Callers 5

Utility.jsFile · 0.50
Common.jsFile · 0.50
_operateMethod · 0.50
fclone.mocha.jsFile · 0.50
API.jsFile · 0.50

Calls 2

sliceMethod · 0.80
indexOfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…