-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
structured-clone.js
97 lines (90 loc) · 2.61 KB
/
structured-clone.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
/* eslint-disable es/no-map -- safe */
/* eslint-disable es/no-set -- safe */
'use const';
var isSymbol = require('./is-symbol');
var toObject = require('./to-object');
var getOwnPropertyNames = require('./object-get-own-property-names');
var classof = require('./classof');
function createDataCloneError(message) {
if (typeof DOMException === 'function') {
return new DOMException(message, 'DataCloneError');
}
return new Error(message);
}
/**
* Tries best to replicate structuredClone behaviour.
*
* @param {WeakMap} weakmap cache map
* @param {any} value object to clone
*/
module.exports = function structuredCloneInternal(weakmap, value) {
if (isSymbol(value)) throw createDataCloneError('Symbols are not cloneable');
if (typeof value !== 'function' && typeof value !== 'object') return value;
if (value === null) return null;
if (weakmap.has(value)) return weakmap.get(value); // effectively preserves circular references
var cloned, i, deep;
switch (classof(value)) {
case 'Boolean':
case 'BigInt':
case 'Number':
case 'String':
cloned = toObject(value.valueOf());
break;
case 'Date':
cloned = new Date(value.valueOf());
break;
case 'RegExp':
cloned = new RegExp(value);
break;
case 'Map':
cloned = new Map();
deep = true;
break;
case 'Set':
cloned = new Set();
deep = true;
break;
case 'Error':
case 'EvalError':
case 'RangeError':
case 'ReferenceError':
case 'SyntaxError':
case 'TypeError':
case 'URIError':
cloned = value.constructor(value.message.toString());
// Note: `error.stack` will point structuredClone, since there are no reliable way to modify it.
break;
case 'Array':
cloned = [];
deep = true;
break;
case 'Object':
cloned = {};
deep = true;
break;
default:
throw createDataCloneError('Uncloneable type: ' + classof(value));
}
weakmap.set(value, cloned);
if (deep) switch (classof(value)) {
case 'Map':
value.forEach(function (v, k) {
cloned.set(structuredCloneInternal(weakmap, k), structuredCloneInternal(weakmap, v));
});
break;
case 'Set':
value.forEach(function (v) {
cloned.add(structuredCloneInternal(weakmap, v));
});
break;
case 'Array':
case 'Object':
var properties = getOwnPropertyNames.f(value);
for (i = 0; i < properties.length; i++) {
cloned[structuredCloneInternal(weakmap, properties[i])] =
structuredCloneInternal(weakmap, value[properties[i]]);
}
break;
}
return cloned;
};