bunshin-clone 1.8.14 → 1.8.16

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/README.md CHANGED
@@ -47,7 +47,7 @@ If `true`, preserves shared backing buffers between TypedArray and DataView inst
47
47
 
48
48
  ### `preserveDescriptors`
49
49
 
50
- If `true`, preserves property descriptors (getters/setters, etc.).
50
+ If `true`, preserves property descriptors, including non-enumerable properties.
51
51
 
52
52
  ### `preserveSymbolKeys`
53
53
 
package/dist/index.cjs CHANGED
@@ -1,170 +1,38 @@
1
1
  'use strict';
2
2
 
3
- // src/index.ts
4
- function bunshinClone(value, options = {}, refs = /* @__PURE__ */ new WeakMap()) {
5
- return clone(value, resolveOptions(options), refs);
3
+ // src/utils.ts
4
+ function isObject(value) {
5
+ return typeof value === "object" && value !== null;
6
6
  }
7
- function clone(value, settings, refs) {
7
+ function isPlainObject(value) {
8
8
  if (!isObject(value)) {
9
- return value;
10
- }
11
- if (refs.has(value)) {
12
- return refs.get(value);
13
- }
14
- const isClonePlainObject = isPlainObject(value);
15
- if (settings.preserveDescriptors && isClonePlainObject) {
16
- return cloneWithDescriptors(value, settings, refs);
17
- }
18
- if (Array.isArray(value)) {
19
- return cloneArray(value, settings, refs);
20
- }
21
- if (isClonePlainObject) {
22
- return clonePlainObject(value, settings, refs);
23
- }
24
- if (value instanceof Map) {
25
- return cloneMap(value, settings, refs);
26
- }
27
- if (value instanceof Set) {
28
- return cloneSet(value, settings, refs);
29
- }
30
- if (value instanceof Date) {
31
- const result = new Date(value.getTime());
32
- refs.set(value, result);
33
- return result;
34
- }
35
- if (value instanceof RegExp) {
36
- const { source, flags, lastIndex } = value;
37
- const result = new RegExp(source, flags);
38
- refs.set(value, result);
39
- result.lastIndex = lastIndex;
40
- return result;
41
- }
42
- if (value instanceof ArrayBuffer) {
43
- const result = value.slice(0);
44
- refs.set(value, result);
45
- return result;
46
- }
47
- if (ArrayBuffer.isView(value)) {
48
- return cloneArrayBufferView(
49
- value,
50
- settings,
51
- refs
52
- );
53
- }
54
- if (value instanceof Error) {
55
- return cloneError(value, settings, refs);
56
- }
57
- if (value instanceof Blob) {
58
- const { size, type } = value;
59
- const result = value.slice(0, size, type);
60
- refs.set(value, result);
61
- return result;
62
- }
63
- if (typeof ImageData !== "undefined" && value instanceof ImageData) {
64
- const { data, width, height, colorSpace } = value;
65
- const result = new ImageData(new Uint8ClampedArray(data), width, height, {
66
- colorSpace
67
- });
68
- refs.set(value, result);
69
- return result;
70
- }
71
- if (value instanceof URL) {
72
- const result = new URL(value.href);
73
- refs.set(value, result);
74
- return result;
75
- }
76
- if (value instanceof URLSearchParams) {
77
- const result = new URLSearchParams(value);
78
- refs.set(value, result);
79
- return result;
9
+ return false;
80
10
  }
81
- refs.set(value, value);
82
- return value;
11
+ const proto = Object.getPrototypeOf(value);
12
+ return proto === Object.prototype || proto === null;
83
13
  }
84
- var IS_ENUMERABLE = Object.prototype.propertyIsEnumerable;
85
- var OWN_DESCS = Object.getOwnPropertyDescriptors;
86
- var OWN_NAMES = Object.getOwnPropertyNames;
87
- var OWN_SYMBOLS = Object.getOwnPropertySymbols;
88
- function cloneWithDescriptors(object, settings, refs) {
89
- const result = Object.create(Object.getPrototypeOf(object));
90
- refs.set(object, result);
91
- const descs = OWN_DESCS(object);
92
- for (const key of OWN_NAMES(object)) {
93
- if (isUnsafeKey(key)) {
94
- continue;
95
- }
96
- const desc = { ...descs[key] };
97
- if ("value" in desc) {
98
- desc.value = clone(desc.value, settings, refs);
99
- }
100
- try {
101
- Object.defineProperty(result, key, desc);
102
- } catch (error) {
103
- if (settings.strictDescriptors) {
104
- throw error;
105
- }
106
- }
107
- }
108
- if (settings.preserveSymbolKeys) {
109
- for (const symbol of OWN_SYMBOLS(object)) {
110
- const desc = { ...descs[symbol] };
111
- if ("value" in desc) {
112
- desc.value = clone(desc.value, settings, refs);
113
- }
114
- try {
115
- Object.defineProperty(result, symbol, desc);
116
- } catch (error) {
117
- if (settings.strictDescriptors) {
118
- throw error;
119
- }
120
- }
121
- }
122
- }
123
- return result;
14
+ function isUnsafeKey(key) {
15
+ return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
124
16
  }
17
+
18
+ // src/clone/array.ts
125
19
  function cloneArray(array, settings, refs) {
126
- const result = isPrimitiveArray(array) ? array.slice() : array.map((item) => clone(item, settings, refs));
20
+ const result = isPrimitiveArray(array) ? array.slice() : array.map((i) => clone(i, settings, refs));
127
21
  refs.set(array, result);
128
22
  return result;
129
23
  }
130
- function clonePlainObject(object, settings, refs) {
131
- const result = Object.create(Object.getPrototypeOf(object));
132
- refs.set(object, result);
133
- for (const key of Object.keys(object)) {
134
- if (!isUnsafeKey(key)) {
135
- result[key] = clone(object[key], settings, refs);
136
- }
137
- }
138
- if (settings.preserveSymbolKeys) {
139
- for (const symbol of OWN_SYMBOLS(object)) {
140
- if (IS_ENUMERABLE.call(object, symbol)) {
141
- result[symbol] = clone(object[symbol], settings, refs);
142
- }
143
- }
144
- }
145
- return result;
146
- }
147
- function cloneMap(map, settings, refs) {
148
- const result = /* @__PURE__ */ new Map();
149
- refs.set(map, result);
150
- for (const [key, value] of map) {
151
- result.set(clone(key, settings, refs), clone(value, settings, refs));
152
- }
153
- return result;
24
+ function isPrimitiveArray(array) {
25
+ return array.every((i) => !isObject(i));
154
26
  }
155
- function cloneSet(set, settings, refs) {
156
- if (isPrimitiveSet(set)) {
157
- const result2 = new Set(set);
158
- refs.set(set, result2);
159
- return result2;
160
- }
161
- const result = /* @__PURE__ */ new Set();
162
- refs.set(set, result);
163
- for (const item of set) {
164
- result.add(clone(item, settings, refs));
165
- }
27
+
28
+ // src/clone/array-buffer.ts
29
+ function cloneArrayBuffer(buffer, refs) {
30
+ const result = buffer.slice(0);
31
+ refs.set(buffer, result);
166
32
  return result;
167
33
  }
34
+
35
+ // src/clone/array-buffer-view.ts
168
36
  function cloneArrayBufferView(view, settings, refs) {
169
37
  if (view instanceof DataView) {
170
38
  const { buffer: buffer2, byteOffset: byteOffset2, byteLength } = view;
@@ -196,6 +64,31 @@ function cloneBuffer(buffer, refs) {
196
64
  refs.set(buffer, result);
197
65
  return result;
198
66
  }
67
+
68
+ // src/clone/blob.ts
69
+ function cloneBlob(value, refs) {
70
+ const { size, type } = value;
71
+ const result = value.slice(0, size, type);
72
+ refs.set(value, result);
73
+ return result;
74
+ }
75
+
76
+ // src/clone/date.ts
77
+ function cloneDate(date, refs) {
78
+ const result = new Date(date.getTime());
79
+ refs.set(date, result);
80
+ return result;
81
+ }
82
+
83
+ // src/constants.ts
84
+ var IS_ENUMERABLE = Object.prototype.propertyIsEnumerable;
85
+ var OWN_DESCS = Object.getOwnPropertyDescriptors;
86
+ var OWN_ENUM_STRING_KEYS = Object.keys;
87
+ var OWN_ENUM_SYMBOL_KEYS = (o) => OWN_SYMBOL_KEYS(o).filter((k) => IS_ENUMERABLE.call(o, k));
88
+ var OWN_STRING_KEYS = Object.getOwnPropertyNames;
89
+ var OWN_SYMBOL_KEYS = Object.getOwnPropertySymbols;
90
+
91
+ // src/clone/error.ts
199
92
  function cloneError(error, settings, refs) {
200
93
  const result = createErrorInstance(error, settings, refs);
201
94
  refs.set(error, result);
@@ -212,7 +105,7 @@ function cloneError(error, settings, refs) {
212
105
  if ("cause" in error && cause !== void 0) {
213
106
  result.cause = clone(cause, settings, refs);
214
107
  }
215
- for (const key of Object.keys(error)) {
108
+ for (const key of OWN_ENUM_STRING_KEYS(error)) {
216
109
  Reflect.set(result, key, clone(Reflect.get(error, key), settings, refs));
217
110
  }
218
111
  return result;
@@ -244,18 +137,66 @@ function createErrorInstance(error, settings, refs) {
244
137
  result.name = name;
245
138
  return result;
246
139
  }
247
- function isObject(value) {
248
- return typeof value === "object" && value !== null;
140
+
141
+ // src/clone/image-data.ts
142
+ function cloneImageData(data, refs) {
143
+ const { data: d, width, height, colorSpace } = data;
144
+ const result = new ImageData(new Uint8ClampedArray(d), width, height, {
145
+ colorSpace
146
+ });
147
+ refs.set(data, result);
148
+ return result;
249
149
  }
250
- function isPlainObject(value) {
251
- if (!isObject(value)) {
252
- return false;
150
+
151
+ // src/clone/map.ts
152
+ function cloneMap(map, settings, refs) {
153
+ const result = /* @__PURE__ */ new Map();
154
+ refs.set(map, result);
155
+ for (const [key, value] of map) {
156
+ result.set(clone(key, settings, refs), clone(value, settings, refs));
253
157
  }
254
- const proto = Object.getPrototypeOf(value);
255
- return proto === Object.prototype || proto === null;
158
+ return result;
256
159
  }
257
- function isPrimitiveArray(array) {
258
- return array.every((item) => !isObject(item));
160
+
161
+ // src/clone/plain-object.ts
162
+ function clonePlainObject(object, settings, refs) {
163
+ const result = Object.create(Object.getPrototypeOf(object));
164
+ refs.set(object, result);
165
+ for (const key of OWN_ENUM_STRING_KEYS(object)) {
166
+ if (!isUnsafeKey(key)) {
167
+ result[key] = clone(object[key], settings, refs);
168
+ }
169
+ }
170
+ if (settings.preserveSymbolKeys) {
171
+ for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
172
+ result[key] = clone(object[key], settings, refs);
173
+ }
174
+ }
175
+ return result;
176
+ }
177
+
178
+ // src/clone/regexp.ts
179
+ function cloneRegExp(re, refs) {
180
+ const { source, flags, lastIndex } = re;
181
+ const result = new RegExp(source, flags);
182
+ refs.set(re, result);
183
+ result.lastIndex = lastIndex;
184
+ return result;
185
+ }
186
+
187
+ // src/clone/set.ts
188
+ function cloneSet(set, settings, refs) {
189
+ if (isPrimitiveSet(set)) {
190
+ const result2 = new Set(set);
191
+ refs.set(set, result2);
192
+ return result2;
193
+ }
194
+ const result = /* @__PURE__ */ new Set();
195
+ refs.set(set, result);
196
+ for (const item of set) {
197
+ result.add(clone(item, settings, refs));
198
+ }
199
+ return result;
259
200
  }
260
201
  function isPrimitiveSet(set) {
261
202
  for (const value of set) {
@@ -265,8 +206,120 @@ function isPrimitiveSet(set) {
265
206
  }
266
207
  return true;
267
208
  }
268
- function isUnsafeKey(key) {
269
- return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
209
+
210
+ // src/clone/url.ts
211
+ function cloneURL(value, refs) {
212
+ const result = new URL(value.href);
213
+ refs.set(value, result);
214
+ return result;
215
+ }
216
+
217
+ // src/clone/url-search-params.ts
218
+ function cloneURLSearchParams(value, refs) {
219
+ const result = new URLSearchParams(value);
220
+ refs.set(value, result);
221
+ return result;
222
+ }
223
+
224
+ // src/clone/with-descriptors.ts
225
+ function cloneWithDescriptors(object, settings, refs) {
226
+ const result = Object.create(Object.getPrototypeOf(object));
227
+ refs.set(object, result);
228
+ const descs = OWN_DESCS(object);
229
+ for (const key of OWN_STRING_KEYS(object)) {
230
+ if (isUnsafeKey(key)) {
231
+ continue;
232
+ }
233
+ const desc = { ...descs[key] };
234
+ if ("value" in desc) {
235
+ desc.value = clone(desc.value, settings, refs);
236
+ }
237
+ try {
238
+ Object.defineProperty(result, key, desc);
239
+ } catch (error) {
240
+ if (settings.strictDescriptors) {
241
+ throw error;
242
+ }
243
+ }
244
+ }
245
+ if (settings.preserveSymbolKeys) {
246
+ for (const key of OWN_SYMBOL_KEYS(object)) {
247
+ const desc = { ...descs[key] };
248
+ if ("value" in desc) {
249
+ desc.value = clone(desc.value, settings, refs);
250
+ }
251
+ try {
252
+ Object.defineProperty(result, key, desc);
253
+ } catch (error) {
254
+ if (settings.strictDescriptors) {
255
+ throw error;
256
+ }
257
+ }
258
+ }
259
+ }
260
+ return result;
261
+ }
262
+
263
+ // src/index.ts
264
+ function bunshinClone(value, options = {}) {
265
+ return clone(value, resolveOptions(options), /* @__PURE__ */ new WeakMap());
266
+ }
267
+ function clone(value, settings, refs) {
268
+ if (!isObject(value)) {
269
+ return value;
270
+ }
271
+ if (refs.has(value)) {
272
+ return refs.get(value);
273
+ }
274
+ const isClonePlainObject = isPlainObject(value);
275
+ if (settings.preserveDescriptors && isClonePlainObject) {
276
+ return cloneWithDescriptors(value, settings, refs);
277
+ }
278
+ if (Array.isArray(value)) {
279
+ return cloneArray(value, settings, refs);
280
+ }
281
+ if (isClonePlainObject) {
282
+ return clonePlainObject(value, settings, refs);
283
+ }
284
+ if (value instanceof Map) {
285
+ return cloneMap(value, settings, refs);
286
+ }
287
+ if (value instanceof Set) {
288
+ return cloneSet(value, settings, refs);
289
+ }
290
+ if (value instanceof Date) {
291
+ return cloneDate(value, refs);
292
+ }
293
+ if (value instanceof RegExp) {
294
+ return cloneRegExp(value, refs);
295
+ }
296
+ if (value instanceof ArrayBuffer) {
297
+ return cloneArrayBuffer(value, refs);
298
+ }
299
+ if (ArrayBuffer.isView(value)) {
300
+ return cloneArrayBufferView(
301
+ value,
302
+ settings,
303
+ refs
304
+ );
305
+ }
306
+ if (value instanceof Error) {
307
+ return cloneError(value, settings, refs);
308
+ }
309
+ if (value instanceof Blob) {
310
+ return cloneBlob(value, refs);
311
+ }
312
+ if (typeof ImageData !== "undefined" && value instanceof ImageData) {
313
+ return cloneImageData(value, refs);
314
+ }
315
+ if (value instanceof URL) {
316
+ return cloneURL(value, refs);
317
+ }
318
+ if (value instanceof URLSearchParams) {
319
+ return cloneURLSearchParams(value, refs);
320
+ }
321
+ refs.set(value, value);
322
+ return value;
270
323
  }
271
324
  function resolveOptions(options) {
272
325
  let {
@@ -300,7 +353,4 @@ function resolveOptions(options) {
300
353
  }
301
354
 
302
355
  exports.bunshinClone = bunshinClone;
303
- exports.isObject = isObject;
304
- exports.isPlainObject = isPlainObject;
305
- exports.isPrimitiveArray = isPrimitiveArray;
306
- exports.isUnsafeKey = isUnsafeKey;
356
+ exports.clone = clone;
package/dist/index.d.cts CHANGED
@@ -1,15 +1,13 @@
1
+ type Refs = WeakMap<object, object>;
2
+
1
3
  interface BunshinCloneOptions {
2
4
  preserveBufferSharing: boolean;
3
5
  preserveDescriptors: boolean;
4
6
  preserveSymbolKeys: boolean;
5
7
  strictDescriptors: boolean;
6
8
  }
7
- type PlainObject = Record<PropertyKey, unknown>;
8
- type Refs = WeakMap<object, object>;
9
- declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>, refs?: Refs): T;
10
- declare function isObject(value: unknown): value is object;
11
- declare function isPlainObject(value: unknown): value is PlainObject;
12
- declare function isPrimitiveArray(array: unknown[]): boolean;
13
- declare function isUnsafeKey(key: PropertyKey): boolean;
14
9
 
15
- export { type BunshinCloneOptions, type PlainObject, bunshinClone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
10
+ declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>): T;
11
+ declare function clone<T>(value: T, settings: BunshinCloneOptions, refs: Refs): T;
12
+
13
+ export { bunshinClone, clone };
package/dist/index.d.ts CHANGED
@@ -1,15 +1,13 @@
1
+ type Refs = WeakMap<object, object>;
2
+
1
3
  interface BunshinCloneOptions {
2
4
  preserveBufferSharing: boolean;
3
5
  preserveDescriptors: boolean;
4
6
  preserveSymbolKeys: boolean;
5
7
  strictDescriptors: boolean;
6
8
  }
7
- type PlainObject = Record<PropertyKey, unknown>;
8
- type Refs = WeakMap<object, object>;
9
- declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>, refs?: Refs): T;
10
- declare function isObject(value: unknown): value is object;
11
- declare function isPlainObject(value: unknown): value is PlainObject;
12
- declare function isPrimitiveArray(array: unknown[]): boolean;
13
- declare function isUnsafeKey(key: PropertyKey): boolean;
14
9
 
15
- export { type BunshinCloneOptions, type PlainObject, bunshinClone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
10
+ declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>): T;
11
+ declare function clone<T>(value: T, settings: BunshinCloneOptions, refs: Refs): T;
12
+
13
+ export { bunshinClone, clone };
package/dist/index.js CHANGED
@@ -1,168 +1,36 @@
1
- // src/index.ts
2
- function bunshinClone(value, options = {}, refs = /* @__PURE__ */ new WeakMap()) {
3
- return clone(value, resolveOptions(options), refs);
1
+ // src/utils.ts
2
+ function isObject(value) {
3
+ return typeof value === "object" && value !== null;
4
4
  }
5
- function clone(value, settings, refs) {
5
+ function isPlainObject(value) {
6
6
  if (!isObject(value)) {
7
- return value;
8
- }
9
- if (refs.has(value)) {
10
- return refs.get(value);
11
- }
12
- const isClonePlainObject = isPlainObject(value);
13
- if (settings.preserveDescriptors && isClonePlainObject) {
14
- return cloneWithDescriptors(value, settings, refs);
15
- }
16
- if (Array.isArray(value)) {
17
- return cloneArray(value, settings, refs);
18
- }
19
- if (isClonePlainObject) {
20
- return clonePlainObject(value, settings, refs);
21
- }
22
- if (value instanceof Map) {
23
- return cloneMap(value, settings, refs);
24
- }
25
- if (value instanceof Set) {
26
- return cloneSet(value, settings, refs);
27
- }
28
- if (value instanceof Date) {
29
- const result = new Date(value.getTime());
30
- refs.set(value, result);
31
- return result;
32
- }
33
- if (value instanceof RegExp) {
34
- const { source, flags, lastIndex } = value;
35
- const result = new RegExp(source, flags);
36
- refs.set(value, result);
37
- result.lastIndex = lastIndex;
38
- return result;
39
- }
40
- if (value instanceof ArrayBuffer) {
41
- const result = value.slice(0);
42
- refs.set(value, result);
43
- return result;
44
- }
45
- if (ArrayBuffer.isView(value)) {
46
- return cloneArrayBufferView(
47
- value,
48
- settings,
49
- refs
50
- );
51
- }
52
- if (value instanceof Error) {
53
- return cloneError(value, settings, refs);
54
- }
55
- if (value instanceof Blob) {
56
- const { size, type } = value;
57
- const result = value.slice(0, size, type);
58
- refs.set(value, result);
59
- return result;
60
- }
61
- if (typeof ImageData !== "undefined" && value instanceof ImageData) {
62
- const { data, width, height, colorSpace } = value;
63
- const result = new ImageData(new Uint8ClampedArray(data), width, height, {
64
- colorSpace
65
- });
66
- refs.set(value, result);
67
- return result;
68
- }
69
- if (value instanceof URL) {
70
- const result = new URL(value.href);
71
- refs.set(value, result);
72
- return result;
73
- }
74
- if (value instanceof URLSearchParams) {
75
- const result = new URLSearchParams(value);
76
- refs.set(value, result);
77
- return result;
7
+ return false;
78
8
  }
79
- refs.set(value, value);
80
- return value;
9
+ const proto = Object.getPrototypeOf(value);
10
+ return proto === Object.prototype || proto === null;
81
11
  }
82
- var IS_ENUMERABLE = Object.prototype.propertyIsEnumerable;
83
- var OWN_DESCS = Object.getOwnPropertyDescriptors;
84
- var OWN_NAMES = Object.getOwnPropertyNames;
85
- var OWN_SYMBOLS = Object.getOwnPropertySymbols;
86
- function cloneWithDescriptors(object, settings, refs) {
87
- const result = Object.create(Object.getPrototypeOf(object));
88
- refs.set(object, result);
89
- const descs = OWN_DESCS(object);
90
- for (const key of OWN_NAMES(object)) {
91
- if (isUnsafeKey(key)) {
92
- continue;
93
- }
94
- const desc = { ...descs[key] };
95
- if ("value" in desc) {
96
- desc.value = clone(desc.value, settings, refs);
97
- }
98
- try {
99
- Object.defineProperty(result, key, desc);
100
- } catch (error) {
101
- if (settings.strictDescriptors) {
102
- throw error;
103
- }
104
- }
105
- }
106
- if (settings.preserveSymbolKeys) {
107
- for (const symbol of OWN_SYMBOLS(object)) {
108
- const desc = { ...descs[symbol] };
109
- if ("value" in desc) {
110
- desc.value = clone(desc.value, settings, refs);
111
- }
112
- try {
113
- Object.defineProperty(result, symbol, desc);
114
- } catch (error) {
115
- if (settings.strictDescriptors) {
116
- throw error;
117
- }
118
- }
119
- }
120
- }
121
- return result;
12
+ function isUnsafeKey(key) {
13
+ return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
122
14
  }
15
+
16
+ // src/clone/array.ts
123
17
  function cloneArray(array, settings, refs) {
124
- const result = isPrimitiveArray(array) ? array.slice() : array.map((item) => clone(item, settings, refs));
18
+ const result = isPrimitiveArray(array) ? array.slice() : array.map((i) => clone(i, settings, refs));
125
19
  refs.set(array, result);
126
20
  return result;
127
21
  }
128
- function clonePlainObject(object, settings, refs) {
129
- const result = Object.create(Object.getPrototypeOf(object));
130
- refs.set(object, result);
131
- for (const key of Object.keys(object)) {
132
- if (!isUnsafeKey(key)) {
133
- result[key] = clone(object[key], settings, refs);
134
- }
135
- }
136
- if (settings.preserveSymbolKeys) {
137
- for (const symbol of OWN_SYMBOLS(object)) {
138
- if (IS_ENUMERABLE.call(object, symbol)) {
139
- result[symbol] = clone(object[symbol], settings, refs);
140
- }
141
- }
142
- }
143
- return result;
144
- }
145
- function cloneMap(map, settings, refs) {
146
- const result = /* @__PURE__ */ new Map();
147
- refs.set(map, result);
148
- for (const [key, value] of map) {
149
- result.set(clone(key, settings, refs), clone(value, settings, refs));
150
- }
151
- return result;
22
+ function isPrimitiveArray(array) {
23
+ return array.every((i) => !isObject(i));
152
24
  }
153
- function cloneSet(set, settings, refs) {
154
- if (isPrimitiveSet(set)) {
155
- const result2 = new Set(set);
156
- refs.set(set, result2);
157
- return result2;
158
- }
159
- const result = /* @__PURE__ */ new Set();
160
- refs.set(set, result);
161
- for (const item of set) {
162
- result.add(clone(item, settings, refs));
163
- }
25
+
26
+ // src/clone/array-buffer.ts
27
+ function cloneArrayBuffer(buffer, refs) {
28
+ const result = buffer.slice(0);
29
+ refs.set(buffer, result);
164
30
  return result;
165
31
  }
32
+
33
+ // src/clone/array-buffer-view.ts
166
34
  function cloneArrayBufferView(view, settings, refs) {
167
35
  if (view instanceof DataView) {
168
36
  const { buffer: buffer2, byteOffset: byteOffset2, byteLength } = view;
@@ -194,6 +62,31 @@ function cloneBuffer(buffer, refs) {
194
62
  refs.set(buffer, result);
195
63
  return result;
196
64
  }
65
+
66
+ // src/clone/blob.ts
67
+ function cloneBlob(value, refs) {
68
+ const { size, type } = value;
69
+ const result = value.slice(0, size, type);
70
+ refs.set(value, result);
71
+ return result;
72
+ }
73
+
74
+ // src/clone/date.ts
75
+ function cloneDate(date, refs) {
76
+ const result = new Date(date.getTime());
77
+ refs.set(date, result);
78
+ return result;
79
+ }
80
+
81
+ // src/constants.ts
82
+ var IS_ENUMERABLE = Object.prototype.propertyIsEnumerable;
83
+ var OWN_DESCS = Object.getOwnPropertyDescriptors;
84
+ var OWN_ENUM_STRING_KEYS = Object.keys;
85
+ var OWN_ENUM_SYMBOL_KEYS = (o) => OWN_SYMBOL_KEYS(o).filter((k) => IS_ENUMERABLE.call(o, k));
86
+ var OWN_STRING_KEYS = Object.getOwnPropertyNames;
87
+ var OWN_SYMBOL_KEYS = Object.getOwnPropertySymbols;
88
+
89
+ // src/clone/error.ts
197
90
  function cloneError(error, settings, refs) {
198
91
  const result = createErrorInstance(error, settings, refs);
199
92
  refs.set(error, result);
@@ -210,7 +103,7 @@ function cloneError(error, settings, refs) {
210
103
  if ("cause" in error && cause !== void 0) {
211
104
  result.cause = clone(cause, settings, refs);
212
105
  }
213
- for (const key of Object.keys(error)) {
106
+ for (const key of OWN_ENUM_STRING_KEYS(error)) {
214
107
  Reflect.set(result, key, clone(Reflect.get(error, key), settings, refs));
215
108
  }
216
109
  return result;
@@ -242,18 +135,66 @@ function createErrorInstance(error, settings, refs) {
242
135
  result.name = name;
243
136
  return result;
244
137
  }
245
- function isObject(value) {
246
- return typeof value === "object" && value !== null;
138
+
139
+ // src/clone/image-data.ts
140
+ function cloneImageData(data, refs) {
141
+ const { data: d, width, height, colorSpace } = data;
142
+ const result = new ImageData(new Uint8ClampedArray(d), width, height, {
143
+ colorSpace
144
+ });
145
+ refs.set(data, result);
146
+ return result;
247
147
  }
248
- function isPlainObject(value) {
249
- if (!isObject(value)) {
250
- return false;
148
+
149
+ // src/clone/map.ts
150
+ function cloneMap(map, settings, refs) {
151
+ const result = /* @__PURE__ */ new Map();
152
+ refs.set(map, result);
153
+ for (const [key, value] of map) {
154
+ result.set(clone(key, settings, refs), clone(value, settings, refs));
251
155
  }
252
- const proto = Object.getPrototypeOf(value);
253
- return proto === Object.prototype || proto === null;
156
+ return result;
254
157
  }
255
- function isPrimitiveArray(array) {
256
- return array.every((item) => !isObject(item));
158
+
159
+ // src/clone/plain-object.ts
160
+ function clonePlainObject(object, settings, refs) {
161
+ const result = Object.create(Object.getPrototypeOf(object));
162
+ refs.set(object, result);
163
+ for (const key of OWN_ENUM_STRING_KEYS(object)) {
164
+ if (!isUnsafeKey(key)) {
165
+ result[key] = clone(object[key], settings, refs);
166
+ }
167
+ }
168
+ if (settings.preserveSymbolKeys) {
169
+ for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
170
+ result[key] = clone(object[key], settings, refs);
171
+ }
172
+ }
173
+ return result;
174
+ }
175
+
176
+ // src/clone/regexp.ts
177
+ function cloneRegExp(re, refs) {
178
+ const { source, flags, lastIndex } = re;
179
+ const result = new RegExp(source, flags);
180
+ refs.set(re, result);
181
+ result.lastIndex = lastIndex;
182
+ return result;
183
+ }
184
+
185
+ // src/clone/set.ts
186
+ function cloneSet(set, settings, refs) {
187
+ if (isPrimitiveSet(set)) {
188
+ const result2 = new Set(set);
189
+ refs.set(set, result2);
190
+ return result2;
191
+ }
192
+ const result = /* @__PURE__ */ new Set();
193
+ refs.set(set, result);
194
+ for (const item of set) {
195
+ result.add(clone(item, settings, refs));
196
+ }
197
+ return result;
257
198
  }
258
199
  function isPrimitiveSet(set) {
259
200
  for (const value of set) {
@@ -263,8 +204,120 @@ function isPrimitiveSet(set) {
263
204
  }
264
205
  return true;
265
206
  }
266
- function isUnsafeKey(key) {
267
- return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
207
+
208
+ // src/clone/url.ts
209
+ function cloneURL(value, refs) {
210
+ const result = new URL(value.href);
211
+ refs.set(value, result);
212
+ return result;
213
+ }
214
+
215
+ // src/clone/url-search-params.ts
216
+ function cloneURLSearchParams(value, refs) {
217
+ const result = new URLSearchParams(value);
218
+ refs.set(value, result);
219
+ return result;
220
+ }
221
+
222
+ // src/clone/with-descriptors.ts
223
+ function cloneWithDescriptors(object, settings, refs) {
224
+ const result = Object.create(Object.getPrototypeOf(object));
225
+ refs.set(object, result);
226
+ const descs = OWN_DESCS(object);
227
+ for (const key of OWN_STRING_KEYS(object)) {
228
+ if (isUnsafeKey(key)) {
229
+ continue;
230
+ }
231
+ const desc = { ...descs[key] };
232
+ if ("value" in desc) {
233
+ desc.value = clone(desc.value, settings, refs);
234
+ }
235
+ try {
236
+ Object.defineProperty(result, key, desc);
237
+ } catch (error) {
238
+ if (settings.strictDescriptors) {
239
+ throw error;
240
+ }
241
+ }
242
+ }
243
+ if (settings.preserveSymbolKeys) {
244
+ for (const key of OWN_SYMBOL_KEYS(object)) {
245
+ const desc = { ...descs[key] };
246
+ if ("value" in desc) {
247
+ desc.value = clone(desc.value, settings, refs);
248
+ }
249
+ try {
250
+ Object.defineProperty(result, key, desc);
251
+ } catch (error) {
252
+ if (settings.strictDescriptors) {
253
+ throw error;
254
+ }
255
+ }
256
+ }
257
+ }
258
+ return result;
259
+ }
260
+
261
+ // src/index.ts
262
+ function bunshinClone(value, options = {}) {
263
+ return clone(value, resolveOptions(options), /* @__PURE__ */ new WeakMap());
264
+ }
265
+ function clone(value, settings, refs) {
266
+ if (!isObject(value)) {
267
+ return value;
268
+ }
269
+ if (refs.has(value)) {
270
+ return refs.get(value);
271
+ }
272
+ const isClonePlainObject = isPlainObject(value);
273
+ if (settings.preserveDescriptors && isClonePlainObject) {
274
+ return cloneWithDescriptors(value, settings, refs);
275
+ }
276
+ if (Array.isArray(value)) {
277
+ return cloneArray(value, settings, refs);
278
+ }
279
+ if (isClonePlainObject) {
280
+ return clonePlainObject(value, settings, refs);
281
+ }
282
+ if (value instanceof Map) {
283
+ return cloneMap(value, settings, refs);
284
+ }
285
+ if (value instanceof Set) {
286
+ return cloneSet(value, settings, refs);
287
+ }
288
+ if (value instanceof Date) {
289
+ return cloneDate(value, refs);
290
+ }
291
+ if (value instanceof RegExp) {
292
+ return cloneRegExp(value, refs);
293
+ }
294
+ if (value instanceof ArrayBuffer) {
295
+ return cloneArrayBuffer(value, refs);
296
+ }
297
+ if (ArrayBuffer.isView(value)) {
298
+ return cloneArrayBufferView(
299
+ value,
300
+ settings,
301
+ refs
302
+ );
303
+ }
304
+ if (value instanceof Error) {
305
+ return cloneError(value, settings, refs);
306
+ }
307
+ if (value instanceof Blob) {
308
+ return cloneBlob(value, refs);
309
+ }
310
+ if (typeof ImageData !== "undefined" && value instanceof ImageData) {
311
+ return cloneImageData(value, refs);
312
+ }
313
+ if (value instanceof URL) {
314
+ return cloneURL(value, refs);
315
+ }
316
+ if (value instanceof URLSearchParams) {
317
+ return cloneURLSearchParams(value, refs);
318
+ }
319
+ refs.set(value, value);
320
+ return value;
268
321
  }
269
322
  function resolveOptions(options) {
270
323
  let {
@@ -297,4 +350,4 @@ function resolveOptions(options) {
297
350
  };
298
351
  }
299
352
 
300
- export { bunshinClone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
353
+ export { bunshinClone, clone };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunshin-clone",
3
- "version": "1.8.14",
3
+ "version": "1.8.16",
4
4
  "description": "High-performance deep clone utility with descriptor support",
5
5
  "keywords": [
6
6
  "array",