bunshin-clone 1.8.15 → 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/dist/index.cjs CHANGED
@@ -1,171 +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 OWN_DESCS = Object.getOwnPropertyDescriptors;
85
- var OWN_ENUM_STRING_KEYS = Object.keys;
86
- var OWN_ENUM_SYMBOL_KEYS = (object) => OWN_SYMBOL_KEYS(object).filter(
87
- (k) => Object.prototype.propertyIsEnumerable.call(object, k)
88
- );
89
- var OWN_STRING_KEYS = Object.getOwnPropertyNames;
90
- var OWN_SYMBOL_KEYS = Object.getOwnPropertySymbols;
91
- function cloneWithDescriptors(object, settings, refs) {
92
- const result = Object.create(Object.getPrototypeOf(object));
93
- refs.set(object, result);
94
- const descs = OWN_DESCS(object);
95
- for (const key of OWN_STRING_KEYS(object)) {
96
- if (isUnsafeKey(key)) {
97
- continue;
98
- }
99
- const desc = { ...descs[key] };
100
- if ("value" in desc) {
101
- desc.value = clone(desc.value, settings, refs);
102
- }
103
- try {
104
- Object.defineProperty(result, key, desc);
105
- } catch (error) {
106
- if (settings.strictDescriptors) {
107
- throw error;
108
- }
109
- }
110
- }
111
- if (settings.preserveSymbolKeys) {
112
- for (const key of OWN_SYMBOL_KEYS(object)) {
113
- const desc = { ...descs[key] };
114
- if ("value" in desc) {
115
- desc.value = clone(desc.value, settings, refs);
116
- }
117
- try {
118
- Object.defineProperty(result, key, desc);
119
- } catch (error) {
120
- if (settings.strictDescriptors) {
121
- throw error;
122
- }
123
- }
124
- }
125
- }
126
- return result;
14
+ function isUnsafeKey(key) {
15
+ return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
127
16
  }
17
+
18
+ // src/clone/array.ts
128
19
  function cloneArray(array, settings, refs) {
129
- 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));
130
21
  refs.set(array, result);
131
22
  return result;
132
23
  }
133
- function clonePlainObject(object, settings, refs) {
134
- const result = Object.create(Object.getPrototypeOf(object));
135
- refs.set(object, result);
136
- for (const key of OWN_ENUM_STRING_KEYS(object)) {
137
- if (!isUnsafeKey(key)) {
138
- result[key] = clone(object[key], settings, refs);
139
- }
140
- }
141
- if (settings.preserveSymbolKeys) {
142
- for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
143
- result[key] = clone(object[key], settings, refs);
144
- }
145
- }
146
- return result;
147
- }
148
- function cloneMap(map, settings, refs) {
149
- const result = /* @__PURE__ */ new Map();
150
- refs.set(map, result);
151
- for (const [key, value] of map) {
152
- result.set(clone(key, settings, refs), clone(value, settings, refs));
153
- }
154
- return result;
24
+ function isPrimitiveArray(array) {
25
+ return array.every((i) => !isObject(i));
155
26
  }
156
- function cloneSet(set, settings, refs) {
157
- if (isPrimitiveSet(set)) {
158
- const result2 = new Set(set);
159
- refs.set(set, result2);
160
- return result2;
161
- }
162
- const result = /* @__PURE__ */ new Set();
163
- refs.set(set, result);
164
- for (const item of set) {
165
- result.add(clone(item, settings, refs));
166
- }
27
+
28
+ // src/clone/array-buffer.ts
29
+ function cloneArrayBuffer(buffer, refs) {
30
+ const result = buffer.slice(0);
31
+ refs.set(buffer, result);
167
32
  return result;
168
33
  }
34
+
35
+ // src/clone/array-buffer-view.ts
169
36
  function cloneArrayBufferView(view, settings, refs) {
170
37
  if (view instanceof DataView) {
171
38
  const { buffer: buffer2, byteOffset: byteOffset2, byteLength } = view;
@@ -197,6 +64,31 @@ function cloneBuffer(buffer, refs) {
197
64
  refs.set(buffer, result);
198
65
  return result;
199
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
200
92
  function cloneError(error, settings, refs) {
201
93
  const result = createErrorInstance(error, settings, refs);
202
94
  refs.set(error, result);
@@ -245,18 +137,66 @@ function createErrorInstance(error, settings, refs) {
245
137
  result.name = name;
246
138
  return result;
247
139
  }
248
- function isObject(value) {
249
- 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;
250
149
  }
251
- function isPlainObject(value) {
252
- if (!isObject(value)) {
253
- 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));
254
157
  }
255
- const proto = Object.getPrototypeOf(value);
256
- return proto === Object.prototype || proto === null;
158
+ return result;
257
159
  }
258
- function isPrimitiveArray(array) {
259
- 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;
260
200
  }
261
201
  function isPrimitiveSet(set) {
262
202
  for (const value of set) {
@@ -266,8 +206,120 @@ function isPrimitiveSet(set) {
266
206
  }
267
207
  return true;
268
208
  }
269
- function isUnsafeKey(key) {
270
- 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;
271
323
  }
272
324
  function resolveOptions(options) {
273
325
  let {
@@ -301,7 +353,4 @@ function resolveOptions(options) {
301
353
  }
302
354
 
303
355
  exports.bunshinClone = bunshinClone;
304
- exports.isObject = isObject;
305
- exports.isPlainObject = isPlainObject;
306
- exports.isPrimitiveArray = isPrimitiveArray;
307
- 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,169 +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 OWN_DESCS = Object.getOwnPropertyDescriptors;
83
- var OWN_ENUM_STRING_KEYS = Object.keys;
84
- var OWN_ENUM_SYMBOL_KEYS = (object) => OWN_SYMBOL_KEYS(object).filter(
85
- (k) => Object.prototype.propertyIsEnumerable.call(object, k)
86
- );
87
- var OWN_STRING_KEYS = Object.getOwnPropertyNames;
88
- var OWN_SYMBOL_KEYS = Object.getOwnPropertySymbols;
89
- function cloneWithDescriptors(object, settings, refs) {
90
- const result = Object.create(Object.getPrototypeOf(object));
91
- refs.set(object, result);
92
- const descs = OWN_DESCS(object);
93
- for (const key of OWN_STRING_KEYS(object)) {
94
- if (isUnsafeKey(key)) {
95
- continue;
96
- }
97
- const desc = { ...descs[key] };
98
- if ("value" in desc) {
99
- desc.value = clone(desc.value, settings, refs);
100
- }
101
- try {
102
- Object.defineProperty(result, key, desc);
103
- } catch (error) {
104
- if (settings.strictDescriptors) {
105
- throw error;
106
- }
107
- }
108
- }
109
- if (settings.preserveSymbolKeys) {
110
- for (const key of OWN_SYMBOL_KEYS(object)) {
111
- const desc = { ...descs[key] };
112
- if ("value" in desc) {
113
- desc.value = clone(desc.value, settings, refs);
114
- }
115
- try {
116
- Object.defineProperty(result, key, desc);
117
- } catch (error) {
118
- if (settings.strictDescriptors) {
119
- throw error;
120
- }
121
- }
122
- }
123
- }
124
- return result;
12
+ function isUnsafeKey(key) {
13
+ return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
125
14
  }
15
+
16
+ // src/clone/array.ts
126
17
  function cloneArray(array, settings, refs) {
127
- 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));
128
19
  refs.set(array, result);
129
20
  return result;
130
21
  }
131
- function clonePlainObject(object, settings, refs) {
132
- const result = Object.create(Object.getPrototypeOf(object));
133
- refs.set(object, result);
134
- for (const key of OWN_ENUM_STRING_KEYS(object)) {
135
- if (!isUnsafeKey(key)) {
136
- result[key] = clone(object[key], settings, refs);
137
- }
138
- }
139
- if (settings.preserveSymbolKeys) {
140
- for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
141
- result[key] = clone(object[key], settings, refs);
142
- }
143
- }
144
- return result;
145
- }
146
- function cloneMap(map, settings, refs) {
147
- const result = /* @__PURE__ */ new Map();
148
- refs.set(map, result);
149
- for (const [key, value] of map) {
150
- result.set(clone(key, settings, refs), clone(value, settings, refs));
151
- }
152
- return result;
22
+ function isPrimitiveArray(array) {
23
+ return array.every((i) => !isObject(i));
153
24
  }
154
- function cloneSet(set, settings, refs) {
155
- if (isPrimitiveSet(set)) {
156
- const result2 = new Set(set);
157
- refs.set(set, result2);
158
- return result2;
159
- }
160
- const result = /* @__PURE__ */ new Set();
161
- refs.set(set, result);
162
- for (const item of set) {
163
- result.add(clone(item, settings, refs));
164
- }
25
+
26
+ // src/clone/array-buffer.ts
27
+ function cloneArrayBuffer(buffer, refs) {
28
+ const result = buffer.slice(0);
29
+ refs.set(buffer, result);
165
30
  return result;
166
31
  }
32
+
33
+ // src/clone/array-buffer-view.ts
167
34
  function cloneArrayBufferView(view, settings, refs) {
168
35
  if (view instanceof DataView) {
169
36
  const { buffer: buffer2, byteOffset: byteOffset2, byteLength } = view;
@@ -195,6 +62,31 @@ function cloneBuffer(buffer, refs) {
195
62
  refs.set(buffer, result);
196
63
  return result;
197
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
198
90
  function cloneError(error, settings, refs) {
199
91
  const result = createErrorInstance(error, settings, refs);
200
92
  refs.set(error, result);
@@ -243,18 +135,66 @@ function createErrorInstance(error, settings, refs) {
243
135
  result.name = name;
244
136
  return result;
245
137
  }
246
- function isObject(value) {
247
- 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;
248
147
  }
249
- function isPlainObject(value) {
250
- if (!isObject(value)) {
251
- 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));
252
155
  }
253
- const proto = Object.getPrototypeOf(value);
254
- return proto === Object.prototype || proto === null;
156
+ return result;
255
157
  }
256
- function isPrimitiveArray(array) {
257
- 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;
258
198
  }
259
199
  function isPrimitiveSet(set) {
260
200
  for (const value of set) {
@@ -264,8 +204,120 @@ function isPrimitiveSet(set) {
264
204
  }
265
205
  return true;
266
206
  }
267
- function isUnsafeKey(key) {
268
- 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;
269
321
  }
270
322
  function resolveOptions(options) {
271
323
  let {
@@ -298,4 +350,4 @@ function resolveOptions(options) {
298
350
  };
299
351
  }
300
352
 
301
- 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.15",
3
+ "version": "1.8.16",
4
4
  "description": "High-performance deep clone utility with descriptor support",
5
5
  "keywords": [
6
6
  "array",