bunshin-clone 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.bundle.cjs +371 -0
- package/dist/index.bundle.js +365 -0
- package/package.json +2 -2
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/utils.ts
|
|
4
|
+
function isObject(value) {
|
|
5
|
+
return typeof value === "object" && value !== null;
|
|
6
|
+
}
|
|
7
|
+
function isPlainObject(value) {
|
|
8
|
+
if (!isObject(value)) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const proto = Object.getPrototypeOf(value);
|
|
12
|
+
return proto === Object.prototype || proto === null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/clone/array.ts
|
|
16
|
+
function cloneArray(array, settings, refs) {
|
|
17
|
+
const result = isPrimitiveArray(array) ? array.slice() : array.map((i) => clone(i, settings, refs));
|
|
18
|
+
refs.set(array, result);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
function isPrimitiveArray(array) {
|
|
22
|
+
return array.every((i) => !isObject(i));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/clone/array-buffer.ts
|
|
26
|
+
function cloneArrayBuffer(buffer, refs) {
|
|
27
|
+
const result = buffer.slice(0);
|
|
28
|
+
refs.set(buffer, result);
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/clone/array-buffer-view.ts
|
|
33
|
+
function cloneArrayBufferView(view, settings, refs) {
|
|
34
|
+
if (view instanceof DataView) {
|
|
35
|
+
const { buffer: buffer2, byteOffset: byteOffset2, byteLength } = view;
|
|
36
|
+
const result2 = new DataView(
|
|
37
|
+
cloneBuffer(buffer2, refs),
|
|
38
|
+
byteOffset2,
|
|
39
|
+
byteLength
|
|
40
|
+
);
|
|
41
|
+
refs.set(view, result2);
|
|
42
|
+
return result2;
|
|
43
|
+
}
|
|
44
|
+
if (!settings.preserveBufferSharing) {
|
|
45
|
+
const Ctor2 = view.constructor;
|
|
46
|
+
const result2 = new Ctor2(view);
|
|
47
|
+
refs.set(view, result2);
|
|
48
|
+
return result2;
|
|
49
|
+
}
|
|
50
|
+
const Ctor = view.constructor;
|
|
51
|
+
const { buffer, byteOffset, length } = view;
|
|
52
|
+
const result = new Ctor(cloneBuffer(buffer, refs), byteOffset, length);
|
|
53
|
+
refs.set(view, result);
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
function cloneBuffer(buffer, refs) {
|
|
57
|
+
if (refs.has(buffer)) {
|
|
58
|
+
return refs.get(buffer);
|
|
59
|
+
}
|
|
60
|
+
const result = buffer.slice(0);
|
|
61
|
+
refs.set(buffer, result);
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/clone/blob.ts
|
|
66
|
+
function cloneBlob(blob, refs) {
|
|
67
|
+
const { size, type } = blob;
|
|
68
|
+
const result = blob.slice(0, size, type);
|
|
69
|
+
refs.set(blob, result);
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/clone/date.ts
|
|
74
|
+
function cloneDate(date, refs) {
|
|
75
|
+
const result = new Date(date.getTime());
|
|
76
|
+
refs.set(date, result);
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/clone/error.ts
|
|
81
|
+
function cloneError(error, settings, refs) {
|
|
82
|
+
const result = createErrorInstance(error, settings, refs);
|
|
83
|
+
refs.set(error, result);
|
|
84
|
+
if (error instanceof DOMException) {
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
const { stack, cause } = error;
|
|
88
|
+
if (stack) {
|
|
89
|
+
try {
|
|
90
|
+
result.stack = stack;
|
|
91
|
+
} catch {
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if ("cause" in error && cause !== void 0) {
|
|
95
|
+
result.cause = clone(cause, settings, refs);
|
|
96
|
+
}
|
|
97
|
+
for (const key of Object.keys(error)) {
|
|
98
|
+
Reflect.set(result, key, clone(Reflect.get(error, key), settings, refs));
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
var ERROR_CTORS = {
|
|
103
|
+
EvalError,
|
|
104
|
+
RangeError,
|
|
105
|
+
ReferenceError,
|
|
106
|
+
SyntaxError,
|
|
107
|
+
TypeError,
|
|
108
|
+
URIError
|
|
109
|
+
};
|
|
110
|
+
function createErrorInstance(error, settings, refs) {
|
|
111
|
+
const { message, name } = error;
|
|
112
|
+
if (error instanceof DOMException) {
|
|
113
|
+
return new DOMException(message, name);
|
|
114
|
+
}
|
|
115
|
+
if (error instanceof AggregateError) {
|
|
116
|
+
return new AggregateError(
|
|
117
|
+
error.errors.map((e) => clone(e, settings, refs)),
|
|
118
|
+
message
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const Ctor = ERROR_CTORS[name];
|
|
122
|
+
if (Ctor) {
|
|
123
|
+
return new Ctor(message);
|
|
124
|
+
}
|
|
125
|
+
const result = new Error(message);
|
|
126
|
+
result.name = name;
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/clone/image-data.ts
|
|
131
|
+
function cloneImageData(data, refs) {
|
|
132
|
+
const { data: d, width, height, colorSpace } = data;
|
|
133
|
+
const result = new ImageData(new Uint8ClampedArray(d), width, height, {
|
|
134
|
+
colorSpace
|
|
135
|
+
});
|
|
136
|
+
refs.set(data, result);
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/clone/map.ts
|
|
141
|
+
function cloneMap(map, settings, refs) {
|
|
142
|
+
const result = /* @__PURE__ */ new Map();
|
|
143
|
+
refs.set(map, result);
|
|
144
|
+
for (const [key, value] of map) {
|
|
145
|
+
result.set(clone(key, settings, refs), clone(value, settings, refs));
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
var OWN_DESCS = Object.getOwnPropertyDescriptors;
|
|
150
|
+
function OWN_ENUM_STRING_KEYS(object) {
|
|
151
|
+
const keys = Object.keys(object);
|
|
152
|
+
for (const key of keys) {
|
|
153
|
+
if (isUnsafeKey(key)) {
|
|
154
|
+
keys.splice(keys.indexOf(key), 1);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return keys;
|
|
158
|
+
}
|
|
159
|
+
function OWN_ENUM_SYMBOL_KEYS(object) {
|
|
160
|
+
const keys = OWN_SYMBOL_KEYS(object);
|
|
161
|
+
for (const key of keys) {
|
|
162
|
+
if (!Object.prototype.propertyIsEnumerable.call(object, key)) {
|
|
163
|
+
keys.splice(keys.indexOf(key), 1);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return keys;
|
|
167
|
+
}
|
|
168
|
+
function OWN_STRING_KEYS(object) {
|
|
169
|
+
const keys = Object.getOwnPropertyNames(object);
|
|
170
|
+
for (const key of keys) {
|
|
171
|
+
if (isUnsafeKey(key)) {
|
|
172
|
+
keys.splice(keys.indexOf(key), 1);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return keys;
|
|
176
|
+
}
|
|
177
|
+
var OWN_SYMBOL_KEYS = Object.getOwnPropertySymbols;
|
|
178
|
+
function isUnsafeKey(key) {
|
|
179
|
+
return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/clone/plain-object.ts
|
|
183
|
+
function clonePlainObject(object, settings, refs) {
|
|
184
|
+
const result = Object.create(Object.getPrototypeOf(object));
|
|
185
|
+
refs.set(object, result);
|
|
186
|
+
function copy(key) {
|
|
187
|
+
result[key] = clone(object[key], settings, refs);
|
|
188
|
+
}
|
|
189
|
+
for (const key of OWN_ENUM_STRING_KEYS(object)) {
|
|
190
|
+
copy(key);
|
|
191
|
+
}
|
|
192
|
+
if (settings.preserveSymbolKeys) {
|
|
193
|
+
for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
|
|
194
|
+
copy(key);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/clone/regexp.ts
|
|
201
|
+
function cloneRegExp(re, refs) {
|
|
202
|
+
const { source, flags, lastIndex } = re;
|
|
203
|
+
const result = new RegExp(source, flags);
|
|
204
|
+
refs.set(re, result);
|
|
205
|
+
result.lastIndex = lastIndex;
|
|
206
|
+
return result;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/clone/set.ts
|
|
210
|
+
function cloneSet(set, settings, refs) {
|
|
211
|
+
if (isPrimitiveSet(set)) {
|
|
212
|
+
const result2 = new Set(set);
|
|
213
|
+
refs.set(set, result2);
|
|
214
|
+
return result2;
|
|
215
|
+
}
|
|
216
|
+
const result = /* @__PURE__ */ new Set();
|
|
217
|
+
refs.set(set, result);
|
|
218
|
+
for (const item of set) {
|
|
219
|
+
result.add(clone(item, settings, refs));
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
function isPrimitiveSet(set) {
|
|
224
|
+
for (const value of set) {
|
|
225
|
+
if (isObject(value)) {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// src/clone/url.ts
|
|
233
|
+
function cloneURL(url, refs) {
|
|
234
|
+
const result = new URL(url.href);
|
|
235
|
+
refs.set(url, result);
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/clone/url-search-params.ts
|
|
240
|
+
function cloneURLSearchParams(params, refs) {
|
|
241
|
+
const result = new URLSearchParams(params);
|
|
242
|
+
refs.set(params, result);
|
|
243
|
+
return result;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/clone/with-descriptors.ts
|
|
247
|
+
function cloneWithDescriptors(object, settings, refs) {
|
|
248
|
+
const result = Object.create(Object.getPrototypeOf(object));
|
|
249
|
+
refs.set(object, result);
|
|
250
|
+
const descs = OWN_DESCS(object);
|
|
251
|
+
function copy(key) {
|
|
252
|
+
const desc = { ...descs[key] };
|
|
253
|
+
if ("value" in desc) {
|
|
254
|
+
desc.value = clone(desc.value, settings, refs);
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
Object.defineProperty(result, key, desc);
|
|
258
|
+
} catch (error) {
|
|
259
|
+
if (settings.strictDescriptors) {
|
|
260
|
+
throw error;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
for (const key of OWN_STRING_KEYS(object)) {
|
|
265
|
+
copy(key);
|
|
266
|
+
}
|
|
267
|
+
if (settings.preserveSymbolKeys) {
|
|
268
|
+
for (const key of OWN_SYMBOL_KEYS(object)) {
|
|
269
|
+
copy(key);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return result;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// src/index.ts
|
|
276
|
+
function bunshinClone(value, options = {}) {
|
|
277
|
+
return clone(value, resolveOptions(options), /* @__PURE__ */ new WeakMap());
|
|
278
|
+
}
|
|
279
|
+
function clone(value, settings, refs) {
|
|
280
|
+
if (!isObject(value)) {
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
283
|
+
if (refs.has(value)) {
|
|
284
|
+
return refs.get(value);
|
|
285
|
+
}
|
|
286
|
+
const isClonePlainObject = isPlainObject(value);
|
|
287
|
+
if (settings.preserveDescriptors && isClonePlainObject) {
|
|
288
|
+
return cloneWithDescriptors(value, settings, refs);
|
|
289
|
+
}
|
|
290
|
+
if (Array.isArray(value)) {
|
|
291
|
+
return cloneArray(value, settings, refs);
|
|
292
|
+
}
|
|
293
|
+
if (isClonePlainObject) {
|
|
294
|
+
return clonePlainObject(value, settings, refs);
|
|
295
|
+
}
|
|
296
|
+
if (value instanceof Map) {
|
|
297
|
+
return cloneMap(value, settings, refs);
|
|
298
|
+
}
|
|
299
|
+
if (value instanceof Set) {
|
|
300
|
+
return cloneSet(value, settings, refs);
|
|
301
|
+
}
|
|
302
|
+
if (value instanceof Date) {
|
|
303
|
+
return cloneDate(value, refs);
|
|
304
|
+
}
|
|
305
|
+
if (value instanceof RegExp) {
|
|
306
|
+
return cloneRegExp(value, refs);
|
|
307
|
+
}
|
|
308
|
+
if (value instanceof ArrayBuffer) {
|
|
309
|
+
return cloneArrayBuffer(value, refs);
|
|
310
|
+
}
|
|
311
|
+
if (ArrayBuffer.isView(value)) {
|
|
312
|
+
return cloneArrayBufferView(
|
|
313
|
+
value,
|
|
314
|
+
settings,
|
|
315
|
+
refs
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
if (value instanceof Error) {
|
|
319
|
+
return cloneError(value, settings, refs);
|
|
320
|
+
}
|
|
321
|
+
if (value instanceof Blob) {
|
|
322
|
+
return cloneBlob(value, refs);
|
|
323
|
+
}
|
|
324
|
+
if (typeof ImageData !== "undefined" && value instanceof ImageData) {
|
|
325
|
+
return cloneImageData(value, refs);
|
|
326
|
+
}
|
|
327
|
+
if (value instanceof URL) {
|
|
328
|
+
return cloneURL(value, refs);
|
|
329
|
+
}
|
|
330
|
+
if (value instanceof URLSearchParams) {
|
|
331
|
+
return cloneURLSearchParams(value, refs);
|
|
332
|
+
}
|
|
333
|
+
refs.set(value, value);
|
|
334
|
+
return value;
|
|
335
|
+
}
|
|
336
|
+
function resolveOptions(options) {
|
|
337
|
+
let {
|
|
338
|
+
preserveBufferSharing = false,
|
|
339
|
+
preserveDescriptors = false,
|
|
340
|
+
strictDescriptors = false,
|
|
341
|
+
preserveSymbolKeys = false
|
|
342
|
+
} = options;
|
|
343
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
344
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
345
|
+
preserveSymbolKeys = false;
|
|
346
|
+
}
|
|
347
|
+
if (typeof preserveBufferSharing !== "boolean") {
|
|
348
|
+
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
349
|
+
preserveBufferSharing = false;
|
|
350
|
+
}
|
|
351
|
+
if (typeof preserveDescriptors !== "boolean") {
|
|
352
|
+
console.warn("Invalid preserveDescriptors option. Fallback: false.");
|
|
353
|
+
preserveDescriptors = false;
|
|
354
|
+
}
|
|
355
|
+
if (typeof strictDescriptors !== "boolean") {
|
|
356
|
+
console.warn("Invalid strictDescriptors option. Fallback: false.");
|
|
357
|
+
strictDescriptors = false;
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
preserveBufferSharing,
|
|
361
|
+
preserveDescriptors,
|
|
362
|
+
preserveSymbolKeys,
|
|
363
|
+
strictDescriptors
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
exports.bunshinClone = bunshinClone;
|
|
368
|
+
exports.clone = clone;
|
|
369
|
+
exports.isObject = isObject;
|
|
370
|
+
exports.isPlainObject = isPlainObject;
|
|
371
|
+
exports.isPrimitiveArray = isPrimitiveArray;
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
function isObject(value) {
|
|
3
|
+
return typeof value === "object" && value !== null;
|
|
4
|
+
}
|
|
5
|
+
function isPlainObject(value) {
|
|
6
|
+
if (!isObject(value)) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const proto = Object.getPrototypeOf(value);
|
|
10
|
+
return proto === Object.prototype || proto === null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/clone/array.ts
|
|
14
|
+
function cloneArray(array, settings, refs) {
|
|
15
|
+
const result = isPrimitiveArray(array) ? array.slice() : array.map((i) => clone(i, settings, refs));
|
|
16
|
+
refs.set(array, result);
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
function isPrimitiveArray(array) {
|
|
20
|
+
return array.every((i) => !isObject(i));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/clone/array-buffer.ts
|
|
24
|
+
function cloneArrayBuffer(buffer, refs) {
|
|
25
|
+
const result = buffer.slice(0);
|
|
26
|
+
refs.set(buffer, result);
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/clone/array-buffer-view.ts
|
|
31
|
+
function cloneArrayBufferView(view, settings, refs) {
|
|
32
|
+
if (view instanceof DataView) {
|
|
33
|
+
const { buffer: buffer2, byteOffset: byteOffset2, byteLength } = view;
|
|
34
|
+
const result2 = new DataView(
|
|
35
|
+
cloneBuffer(buffer2, refs),
|
|
36
|
+
byteOffset2,
|
|
37
|
+
byteLength
|
|
38
|
+
);
|
|
39
|
+
refs.set(view, result2);
|
|
40
|
+
return result2;
|
|
41
|
+
}
|
|
42
|
+
if (!settings.preserveBufferSharing) {
|
|
43
|
+
const Ctor2 = view.constructor;
|
|
44
|
+
const result2 = new Ctor2(view);
|
|
45
|
+
refs.set(view, result2);
|
|
46
|
+
return result2;
|
|
47
|
+
}
|
|
48
|
+
const Ctor = view.constructor;
|
|
49
|
+
const { buffer, byteOffset, length } = view;
|
|
50
|
+
const result = new Ctor(cloneBuffer(buffer, refs), byteOffset, length);
|
|
51
|
+
refs.set(view, result);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
function cloneBuffer(buffer, refs) {
|
|
55
|
+
if (refs.has(buffer)) {
|
|
56
|
+
return refs.get(buffer);
|
|
57
|
+
}
|
|
58
|
+
const result = buffer.slice(0);
|
|
59
|
+
refs.set(buffer, result);
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/clone/blob.ts
|
|
64
|
+
function cloneBlob(blob, refs) {
|
|
65
|
+
const { size, type } = blob;
|
|
66
|
+
const result = blob.slice(0, size, type);
|
|
67
|
+
refs.set(blob, result);
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/clone/date.ts
|
|
72
|
+
function cloneDate(date, refs) {
|
|
73
|
+
const result = new Date(date.getTime());
|
|
74
|
+
refs.set(date, result);
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/clone/error.ts
|
|
79
|
+
function cloneError(error, settings, refs) {
|
|
80
|
+
const result = createErrorInstance(error, settings, refs);
|
|
81
|
+
refs.set(error, result);
|
|
82
|
+
if (error instanceof DOMException) {
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
const { stack, cause } = error;
|
|
86
|
+
if (stack) {
|
|
87
|
+
try {
|
|
88
|
+
result.stack = stack;
|
|
89
|
+
} catch {
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if ("cause" in error && cause !== void 0) {
|
|
93
|
+
result.cause = clone(cause, settings, refs);
|
|
94
|
+
}
|
|
95
|
+
for (const key of Object.keys(error)) {
|
|
96
|
+
Reflect.set(result, key, clone(Reflect.get(error, key), settings, refs));
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
var ERROR_CTORS = {
|
|
101
|
+
EvalError,
|
|
102
|
+
RangeError,
|
|
103
|
+
ReferenceError,
|
|
104
|
+
SyntaxError,
|
|
105
|
+
TypeError,
|
|
106
|
+
URIError
|
|
107
|
+
};
|
|
108
|
+
function createErrorInstance(error, settings, refs) {
|
|
109
|
+
const { message, name } = error;
|
|
110
|
+
if (error instanceof DOMException) {
|
|
111
|
+
return new DOMException(message, name);
|
|
112
|
+
}
|
|
113
|
+
if (error instanceof AggregateError) {
|
|
114
|
+
return new AggregateError(
|
|
115
|
+
error.errors.map((e) => clone(e, settings, refs)),
|
|
116
|
+
message
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const Ctor = ERROR_CTORS[name];
|
|
120
|
+
if (Ctor) {
|
|
121
|
+
return new Ctor(message);
|
|
122
|
+
}
|
|
123
|
+
const result = new Error(message);
|
|
124
|
+
result.name = name;
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/clone/image-data.ts
|
|
129
|
+
function cloneImageData(data, refs) {
|
|
130
|
+
const { data: d, width, height, colorSpace } = data;
|
|
131
|
+
const result = new ImageData(new Uint8ClampedArray(d), width, height, {
|
|
132
|
+
colorSpace
|
|
133
|
+
});
|
|
134
|
+
refs.set(data, result);
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/clone/map.ts
|
|
139
|
+
function cloneMap(map, settings, refs) {
|
|
140
|
+
const result = /* @__PURE__ */ new Map();
|
|
141
|
+
refs.set(map, result);
|
|
142
|
+
for (const [key, value] of map) {
|
|
143
|
+
result.set(clone(key, settings, refs), clone(value, settings, refs));
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
var OWN_DESCS = Object.getOwnPropertyDescriptors;
|
|
148
|
+
function OWN_ENUM_STRING_KEYS(object) {
|
|
149
|
+
const keys = Object.keys(object);
|
|
150
|
+
for (const key of keys) {
|
|
151
|
+
if (isUnsafeKey(key)) {
|
|
152
|
+
keys.splice(keys.indexOf(key), 1);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return keys;
|
|
156
|
+
}
|
|
157
|
+
function OWN_ENUM_SYMBOL_KEYS(object) {
|
|
158
|
+
const keys = OWN_SYMBOL_KEYS(object);
|
|
159
|
+
for (const key of keys) {
|
|
160
|
+
if (!Object.prototype.propertyIsEnumerable.call(object, key)) {
|
|
161
|
+
keys.splice(keys.indexOf(key), 1);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return keys;
|
|
165
|
+
}
|
|
166
|
+
function OWN_STRING_KEYS(object) {
|
|
167
|
+
const keys = Object.getOwnPropertyNames(object);
|
|
168
|
+
for (const key of keys) {
|
|
169
|
+
if (isUnsafeKey(key)) {
|
|
170
|
+
keys.splice(keys.indexOf(key), 1);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return keys;
|
|
174
|
+
}
|
|
175
|
+
var OWN_SYMBOL_KEYS = Object.getOwnPropertySymbols;
|
|
176
|
+
function isUnsafeKey(key) {
|
|
177
|
+
return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// src/clone/plain-object.ts
|
|
181
|
+
function clonePlainObject(object, settings, refs) {
|
|
182
|
+
const result = Object.create(Object.getPrototypeOf(object));
|
|
183
|
+
refs.set(object, result);
|
|
184
|
+
function copy(key) {
|
|
185
|
+
result[key] = clone(object[key], settings, refs);
|
|
186
|
+
}
|
|
187
|
+
for (const key of OWN_ENUM_STRING_KEYS(object)) {
|
|
188
|
+
copy(key);
|
|
189
|
+
}
|
|
190
|
+
if (settings.preserveSymbolKeys) {
|
|
191
|
+
for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
|
|
192
|
+
copy(key);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// src/clone/regexp.ts
|
|
199
|
+
function cloneRegExp(re, refs) {
|
|
200
|
+
const { source, flags, lastIndex } = re;
|
|
201
|
+
const result = new RegExp(source, flags);
|
|
202
|
+
refs.set(re, result);
|
|
203
|
+
result.lastIndex = lastIndex;
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// src/clone/set.ts
|
|
208
|
+
function cloneSet(set, settings, refs) {
|
|
209
|
+
if (isPrimitiveSet(set)) {
|
|
210
|
+
const result2 = new Set(set);
|
|
211
|
+
refs.set(set, result2);
|
|
212
|
+
return result2;
|
|
213
|
+
}
|
|
214
|
+
const result = /* @__PURE__ */ new Set();
|
|
215
|
+
refs.set(set, result);
|
|
216
|
+
for (const item of set) {
|
|
217
|
+
result.add(clone(item, settings, refs));
|
|
218
|
+
}
|
|
219
|
+
return result;
|
|
220
|
+
}
|
|
221
|
+
function isPrimitiveSet(set) {
|
|
222
|
+
for (const value of set) {
|
|
223
|
+
if (isObject(value)) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// src/clone/url.ts
|
|
231
|
+
function cloneURL(url, refs) {
|
|
232
|
+
const result = new URL(url.href);
|
|
233
|
+
refs.set(url, result);
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// src/clone/url-search-params.ts
|
|
238
|
+
function cloneURLSearchParams(params, refs) {
|
|
239
|
+
const result = new URLSearchParams(params);
|
|
240
|
+
refs.set(params, result);
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// src/clone/with-descriptors.ts
|
|
245
|
+
function cloneWithDescriptors(object, settings, refs) {
|
|
246
|
+
const result = Object.create(Object.getPrototypeOf(object));
|
|
247
|
+
refs.set(object, result);
|
|
248
|
+
const descs = OWN_DESCS(object);
|
|
249
|
+
function copy(key) {
|
|
250
|
+
const desc = { ...descs[key] };
|
|
251
|
+
if ("value" in desc) {
|
|
252
|
+
desc.value = clone(desc.value, settings, refs);
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
Object.defineProperty(result, key, desc);
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (settings.strictDescriptors) {
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
for (const key of OWN_STRING_KEYS(object)) {
|
|
263
|
+
copy(key);
|
|
264
|
+
}
|
|
265
|
+
if (settings.preserveSymbolKeys) {
|
|
266
|
+
for (const key of OWN_SYMBOL_KEYS(object)) {
|
|
267
|
+
copy(key);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// src/index.ts
|
|
274
|
+
function bunshinClone(value, options = {}) {
|
|
275
|
+
return clone(value, resolveOptions(options), /* @__PURE__ */ new WeakMap());
|
|
276
|
+
}
|
|
277
|
+
function clone(value, settings, refs) {
|
|
278
|
+
if (!isObject(value)) {
|
|
279
|
+
return value;
|
|
280
|
+
}
|
|
281
|
+
if (refs.has(value)) {
|
|
282
|
+
return refs.get(value);
|
|
283
|
+
}
|
|
284
|
+
const isClonePlainObject = isPlainObject(value);
|
|
285
|
+
if (settings.preserveDescriptors && isClonePlainObject) {
|
|
286
|
+
return cloneWithDescriptors(value, settings, refs);
|
|
287
|
+
}
|
|
288
|
+
if (Array.isArray(value)) {
|
|
289
|
+
return cloneArray(value, settings, refs);
|
|
290
|
+
}
|
|
291
|
+
if (isClonePlainObject) {
|
|
292
|
+
return clonePlainObject(value, settings, refs);
|
|
293
|
+
}
|
|
294
|
+
if (value instanceof Map) {
|
|
295
|
+
return cloneMap(value, settings, refs);
|
|
296
|
+
}
|
|
297
|
+
if (value instanceof Set) {
|
|
298
|
+
return cloneSet(value, settings, refs);
|
|
299
|
+
}
|
|
300
|
+
if (value instanceof Date) {
|
|
301
|
+
return cloneDate(value, refs);
|
|
302
|
+
}
|
|
303
|
+
if (value instanceof RegExp) {
|
|
304
|
+
return cloneRegExp(value, refs);
|
|
305
|
+
}
|
|
306
|
+
if (value instanceof ArrayBuffer) {
|
|
307
|
+
return cloneArrayBuffer(value, refs);
|
|
308
|
+
}
|
|
309
|
+
if (ArrayBuffer.isView(value)) {
|
|
310
|
+
return cloneArrayBufferView(
|
|
311
|
+
value,
|
|
312
|
+
settings,
|
|
313
|
+
refs
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
if (value instanceof Error) {
|
|
317
|
+
return cloneError(value, settings, refs);
|
|
318
|
+
}
|
|
319
|
+
if (value instanceof Blob) {
|
|
320
|
+
return cloneBlob(value, refs);
|
|
321
|
+
}
|
|
322
|
+
if (typeof ImageData !== "undefined" && value instanceof ImageData) {
|
|
323
|
+
return cloneImageData(value, refs);
|
|
324
|
+
}
|
|
325
|
+
if (value instanceof URL) {
|
|
326
|
+
return cloneURL(value, refs);
|
|
327
|
+
}
|
|
328
|
+
if (value instanceof URLSearchParams) {
|
|
329
|
+
return cloneURLSearchParams(value, refs);
|
|
330
|
+
}
|
|
331
|
+
refs.set(value, value);
|
|
332
|
+
return value;
|
|
333
|
+
}
|
|
334
|
+
function resolveOptions(options) {
|
|
335
|
+
let {
|
|
336
|
+
preserveBufferSharing = false,
|
|
337
|
+
preserveDescriptors = false,
|
|
338
|
+
strictDescriptors = false,
|
|
339
|
+
preserveSymbolKeys = false
|
|
340
|
+
} = options;
|
|
341
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
342
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
343
|
+
preserveSymbolKeys = false;
|
|
344
|
+
}
|
|
345
|
+
if (typeof preserveBufferSharing !== "boolean") {
|
|
346
|
+
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
347
|
+
preserveBufferSharing = false;
|
|
348
|
+
}
|
|
349
|
+
if (typeof preserveDescriptors !== "boolean") {
|
|
350
|
+
console.warn("Invalid preserveDescriptors option. Fallback: false.");
|
|
351
|
+
preserveDescriptors = false;
|
|
352
|
+
}
|
|
353
|
+
if (typeof strictDescriptors !== "boolean") {
|
|
354
|
+
console.warn("Invalid strictDescriptors option. Fallback: false.");
|
|
355
|
+
strictDescriptors = false;
|
|
356
|
+
}
|
|
357
|
+
return {
|
|
358
|
+
preserveBufferSharing,
|
|
359
|
+
preserveDescriptors,
|
|
360
|
+
preserveSymbolKeys,
|
|
361
|
+
strictDescriptors
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export { bunshinClone, clone, isObject, isPlainObject, isPrimitiveArray };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bunshin-clone",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "High-performance deep clone utility with descriptor support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"array",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"test": "bun test"
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
|
-
"@y14e/own": "^1.0.
|
|
72
|
+
"@y14e/own": "^1.0.1"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
75
|
"bun-types": "latest",
|