-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
structured-clone.js
115 lines (107 loc) · 3.08 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* eslint-disable es/no-map -- safe */
/* eslint-disable es/no-set -- safe */
'use const';
var isSymbol = require('../internals/is-symbol');
var getOwnPropertyNames = require('../internals/object-get-own-property-names');
var classof = require('../internals/classof');
var getBuiltin = require('../internals/get-built-in');
var isObject = require('../internals/is-object');
var has = require('../internals/has');
var Set = getBuiltin('Set');
var Map = getBuiltin('Map');
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 (!isObject(value)) return value;
if (value === null) return null;
if (weakmap.has(value)) return weakmap.get(value); // effectively preserves circular references
var cloned, i, deep;
var type = classof(value);
switch (type) {
case 'Boolean':
case 'BigInt':
case 'Number':
case 'String':
cloned = Object(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());
deep = true; // clone stack after storing in the weakmap
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 (type) {
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 'Error':
// Attempt to clone the stack.
if (
!has(value, 'stack') && // Chrome, Safari
!has(Error.prototype, 'stack') // Firefox
) break;
try {
cloned.stack = structuredCloneInternal(weakmap, value.stack);
} catch (_) {
return cloned; // Stack cloning not avaliable.
}
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;
};