-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcloneCircular.js
133 lines (130 loc) · 3.54 KB
/
qcloneCircular.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
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
'use strict';
function qcloneCircular(o){
const __qhop__ = Object.prototype.hasOwnProperty;
const {
getPrototypeOf: __gpo__,
create: __cr__
} = Object;
const __Generator__ = __gpo__(function*(){});
let __o__ = [], __c__ = [];
function qclone(o){
if(typeof o !== 'object' || o === null)return o;
switch(o.constructor){
case Object:
let i,l,c = {};
__o__.push(o);__c__.push(c);
for(i in o){
if(__qhop__.call(o, i)){
l = o[i];
if(typeof l !== 'object' || l === null){
c[i] = l;
}else{
let pi = __o__.indexOf(l);
c[i] = (~pi) ? __c__[pi] : qclone(l);
}
}
}
__o__.pop();__c__.pop();
return c;
case Array:
let p,j = o.length, a = new Array(j);
for(let i = 0; i < j; i++){
p = o[i];
if(typeof p !== 'object' || p === null){
a[i] = p;
}else{
let pi = __o__.indexOf(p);
a[i] = (~pi) ? __c__[pi] : qclone(p);
}
}
return a;
case undefined:
__o__.push(o);__c__.push(c);
let q,y,d = Object.create(null);
for(q in o){
y = o[q];
if(typeof y !== 'object' || y === null){
d[q] = y;
}else{
let pi = __o__.indexOf(y);
d[q] = (~pi) ? __c__[pi] : qclone(y);
}
}
__o__.pop();__c__.pop();
return d;
case Map:
let b,m,x = new Map();
for(b of o){
m = b[1];
(typeof m !== 'object' || m === null) ? x.set(b[0], m) : x.set(b[0], qclone(m));
}
return x;
case Set:
let v,n = new Set();
for(v of o){
(typeof v !== 'object' || v === null) ? n.add(v) : n.add(qclone(v));
}
return n;
case Promise:
case Symbol:
case WeakMap:
case WeakSet:
case __Generator__:
return o;
case String:
return String(o);
case Number:
return Number(o);
case Date:
return new Date(o);
case Boolean:
return Boolean(o);
case RegExp:
let r = new RegExp(o.source,
o.flags &&
(o.global ? 'g':'') +
(o.ignoreCase ? 'i':'') +
(o.multiline ? 'm':'') +
(o.unicode ? 'u':'') +
(o.sticky ? 'y':''));
r.lastIndex = o.lastIndex;
return r;
// case BigInt:
// return Object(o.valueOf());
case DataView:
return new DataView(new Uint8Array(new Uint8Array(o.buffer)).buffer, o.byteOffset, o.byteLength);
case ArrayBuffer:
//case SharedArrayBuffer:
return new Uint8Array(new Uint8Array(o)).buffer;
case Error:
case RangeError:
case EvalError:
case ReferenceError:
case SyntaxError:
case TypeError:
case URIError:
//case InternalError:
//case AggregateError:
return o;
default:
if(o.BYTES_PER_ELEMENT)return new o.constructor(o);
let s,u,f = __cr__(__gpo__(o));
__o__.push(o);__c__.push(f);
for(s in o){
if(__qhop__.call(o, s)){
u = o[s];
if(typeof u !== 'object' || u === null){
f[s] = u;
}else{
let pi = __o__.indexOf(u);
f[s] = (~pi) ? __c__[pi] : qclone(u);
}
}
}
__o__.pop();__c__.pop();
return f;
}
};
return qclone(o);
}
module.exports = qcloneCircular;