bunshin-clone 2.0.4 → 2.0.6

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.
@@ -189,8 +189,22 @@ function isUnsafeKey(key) {
189
189
  function clonePlainObject(object, settings, refs) {
190
190
  const result = Object.create(Object.getPrototypeOf(object));
191
191
  refs.set(object, result);
192
- for (const key of (!settings.preserveSymbolKeys ? OWN_ENUM_STRING_KEYS : OWN_ENUM_KEYS)(object)) {
193
- result[key] = clone(object[key], settings, refs);
192
+ const { preserveDescriptors, preserveSymbolKeys } = settings;
193
+ if (!preserveDescriptors) {
194
+ for (const key of (!preserveSymbolKeys ? OWN_ENUM_STRING_KEYS : OWN_ENUM_KEYS)(object)) {
195
+ result[key] = clone(object[key], settings, refs);
196
+ }
197
+ return result;
198
+ }
199
+ const descs = OWN_DESCS(object);
200
+ for (const key of (!preserveSymbolKeys ? OWN_STRING_KEYS : OWN_KEYS)(
201
+ object
202
+ )) {
203
+ const desc = { ...descs[key] };
204
+ if ("value" in desc) {
205
+ desc.value = clone(desc.value, settings, refs);
206
+ }
207
+ Object.defineProperty(result, key, desc);
194
208
  }
195
209
  return result;
196
210
  }
@@ -241,21 +255,30 @@ function cloneURLSearchParams(params, refs) {
241
255
  return result;
242
256
  }
243
257
 
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
- for (const key of (!settings.preserveSymbolKeys ? OWN_STRING_KEYS : OWN_KEYS)(
250
- object
251
- )) {
252
- const desc = { ...descs[key] };
253
- if ("value" in desc) {
254
- desc.value = clone(desc.value, settings, refs);
255
- }
256
- Object.defineProperty(result, key, desc);
258
+ // src/options.ts
259
+ function resolveOptions(options) {
260
+ let {
261
+ preserveBufferSharing = false,
262
+ preserveDescriptors = false,
263
+ preserveSymbolKeys = false
264
+ } = options;
265
+ if (typeof preserveBufferSharing !== "boolean") {
266
+ console.warn("Invalid preserveBufferSharing option. Fallback: false.");
267
+ preserveBufferSharing = false;
257
268
  }
258
- return result;
269
+ if (typeof preserveDescriptors !== "boolean") {
270
+ console.warn("Invalid preserveDescriptors option. Fallback: false.");
271
+ preserveDescriptors = false;
272
+ }
273
+ if (typeof preserveSymbolKeys !== "boolean") {
274
+ console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
275
+ preserveSymbolKeys = false;
276
+ }
277
+ return {
278
+ preserveBufferSharing,
279
+ preserveDescriptors,
280
+ preserveSymbolKeys
281
+ };
259
282
  }
260
283
 
261
284
  // src/index.ts
@@ -269,14 +292,10 @@ function clone(value, settings, refs) {
269
292
  if (refs.has(value)) {
270
293
  return refs.get(value);
271
294
  }
272
- const isClonePlainObject = isPlainObject(value);
273
- if (settings.preserveDescriptors && isClonePlainObject) {
274
- return cloneWithDescriptors(value, settings, refs);
275
- }
276
295
  if (Array.isArray(value)) {
277
296
  return cloneArray(value, settings, refs);
278
297
  }
279
- if (isClonePlainObject) {
298
+ if (isPlainObject(value)) {
280
299
  return clonePlainObject(value, settings, refs);
281
300
  }
282
301
  if (value instanceof Map) {
@@ -319,30 +338,6 @@ function clone(value, settings, refs) {
319
338
  refs.set(value, value);
320
339
  return value;
321
340
  }
322
- function resolveOptions(options) {
323
- let {
324
- preserveBufferSharing = false,
325
- preserveDescriptors = false,
326
- preserveSymbolKeys = false
327
- } = options;
328
- if (typeof preserveBufferSharing !== "boolean") {
329
- console.warn("Invalid preserveBufferSharing option. Fallback: false.");
330
- preserveBufferSharing = false;
331
- }
332
- if (typeof preserveDescriptors !== "boolean") {
333
- console.warn("Invalid preserveDescriptors option. Fallback: false.");
334
- preserveDescriptors = false;
335
- }
336
- if (typeof preserveSymbolKeys !== "boolean") {
337
- console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
338
- preserveSymbolKeys = false;
339
- }
340
- return {
341
- preserveBufferSharing,
342
- preserveDescriptors,
343
- preserveSymbolKeys
344
- };
345
- }
346
341
 
347
342
  exports.bunshinClone = bunshinClone;
348
343
  exports.clone = clone;
@@ -187,8 +187,22 @@ function isUnsafeKey(key) {
187
187
  function clonePlainObject(object, settings, refs) {
188
188
  const result = Object.create(Object.getPrototypeOf(object));
189
189
  refs.set(object, result);
190
- for (const key of (!settings.preserveSymbolKeys ? OWN_ENUM_STRING_KEYS : OWN_ENUM_KEYS)(object)) {
191
- result[key] = clone(object[key], settings, refs);
190
+ const { preserveDescriptors, preserveSymbolKeys } = settings;
191
+ if (!preserveDescriptors) {
192
+ for (const key of (!preserveSymbolKeys ? OWN_ENUM_STRING_KEYS : OWN_ENUM_KEYS)(object)) {
193
+ result[key] = clone(object[key], settings, refs);
194
+ }
195
+ return result;
196
+ }
197
+ const descs = OWN_DESCS(object);
198
+ for (const key of (!preserveSymbolKeys ? OWN_STRING_KEYS : OWN_KEYS)(
199
+ object
200
+ )) {
201
+ const desc = { ...descs[key] };
202
+ if ("value" in desc) {
203
+ desc.value = clone(desc.value, settings, refs);
204
+ }
205
+ Object.defineProperty(result, key, desc);
192
206
  }
193
207
  return result;
194
208
  }
@@ -239,21 +253,30 @@ function cloneURLSearchParams(params, refs) {
239
253
  return result;
240
254
  }
241
255
 
242
- // src/clone/with-descriptors.ts
243
- function cloneWithDescriptors(object, settings, refs) {
244
- const result = Object.create(Object.getPrototypeOf(object));
245
- refs.set(object, result);
246
- const descs = OWN_DESCS(object);
247
- for (const key of (!settings.preserveSymbolKeys ? OWN_STRING_KEYS : OWN_KEYS)(
248
- object
249
- )) {
250
- const desc = { ...descs[key] };
251
- if ("value" in desc) {
252
- desc.value = clone(desc.value, settings, refs);
253
- }
254
- Object.defineProperty(result, key, desc);
256
+ // src/options.ts
257
+ function resolveOptions(options) {
258
+ let {
259
+ preserveBufferSharing = false,
260
+ preserveDescriptors = false,
261
+ preserveSymbolKeys = false
262
+ } = options;
263
+ if (typeof preserveBufferSharing !== "boolean") {
264
+ console.warn("Invalid preserveBufferSharing option. Fallback: false.");
265
+ preserveBufferSharing = false;
255
266
  }
256
- return result;
267
+ if (typeof preserveDescriptors !== "boolean") {
268
+ console.warn("Invalid preserveDescriptors option. Fallback: false.");
269
+ preserveDescriptors = false;
270
+ }
271
+ if (typeof preserveSymbolKeys !== "boolean") {
272
+ console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
273
+ preserveSymbolKeys = false;
274
+ }
275
+ return {
276
+ preserveBufferSharing,
277
+ preserveDescriptors,
278
+ preserveSymbolKeys
279
+ };
257
280
  }
258
281
 
259
282
  // src/index.ts
@@ -267,14 +290,10 @@ function clone(value, settings, refs) {
267
290
  if (refs.has(value)) {
268
291
  return refs.get(value);
269
292
  }
270
- const isClonePlainObject = isPlainObject(value);
271
- if (settings.preserveDescriptors && isClonePlainObject) {
272
- return cloneWithDescriptors(value, settings, refs);
273
- }
274
293
  if (Array.isArray(value)) {
275
294
  return cloneArray(value, settings, refs);
276
295
  }
277
- if (isClonePlainObject) {
296
+ if (isPlainObject(value)) {
278
297
  return clonePlainObject(value, settings, refs);
279
298
  }
280
299
  if (value instanceof Map) {
@@ -317,29 +336,5 @@ function clone(value, settings, refs) {
317
336
  refs.set(value, value);
318
337
  return value;
319
338
  }
320
- function resolveOptions(options) {
321
- let {
322
- preserveBufferSharing = false,
323
- preserveDescriptors = false,
324
- preserveSymbolKeys = false
325
- } = options;
326
- if (typeof preserveBufferSharing !== "boolean") {
327
- console.warn("Invalid preserveBufferSharing option. Fallback: false.");
328
- preserveBufferSharing = false;
329
- }
330
- if (typeof preserveDescriptors !== "boolean") {
331
- console.warn("Invalid preserveDescriptors option. Fallback: false.");
332
- preserveDescriptors = false;
333
- }
334
- if (typeof preserveSymbolKeys !== "boolean") {
335
- console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
336
- preserveSymbolKeys = false;
337
- }
338
- return {
339
- preserveBufferSharing,
340
- preserveDescriptors,
341
- preserveSymbolKeys
342
- };
343
- }
344
339
 
345
340
  export { bunshinClone, clone, isObject, isPlainObject, isPrimitiveArray };
package/dist/index.cjs CHANGED
@@ -151,8 +151,22 @@ function cloneMap(map, settings, refs) {
151
151
  function clonePlainObject(object, settings, refs) {
152
152
  const result = Object.create(Object.getPrototypeOf(object));
153
153
  refs.set(object, result);
154
- for (const key of (!settings.preserveSymbolKeys ? own.OWN_ENUM_STRING_KEYS : own.OWN_ENUM_KEYS)(object)) {
155
- result[key] = clone(object[key], settings, refs);
154
+ const { preserveDescriptors, preserveSymbolKeys } = settings;
155
+ if (!preserveDescriptors) {
156
+ for (const key of (!preserveSymbolKeys ? own.OWN_ENUM_STRING_KEYS : own.OWN_ENUM_KEYS)(object)) {
157
+ result[key] = clone(object[key], settings, refs);
158
+ }
159
+ return result;
160
+ }
161
+ const descs = own.OWN_DESCS(object);
162
+ for (const key of (!preserveSymbolKeys ? own.OWN_STRING_KEYS : own.OWN_KEYS)(
163
+ object
164
+ )) {
165
+ const desc = { ...descs[key] };
166
+ if ("value" in desc) {
167
+ desc.value = clone(desc.value, settings, refs);
168
+ }
169
+ Object.defineProperty(result, key, desc);
156
170
  }
157
171
  return result;
158
172
  }
@@ -202,20 +216,31 @@ function cloneURLSearchParams(params, refs) {
202
216
  refs.set(params, result);
203
217
  return result;
204
218
  }
205
- function cloneWithDescriptors(object, settings, refs) {
206
- const result = Object.create(Object.getPrototypeOf(object));
207
- refs.set(object, result);
208
- const descs = own.OWN_DESCS(object);
209
- for (const key of (!settings.preserveSymbolKeys ? own.OWN_STRING_KEYS : own.OWN_KEYS)(
210
- object
211
- )) {
212
- const desc = { ...descs[key] };
213
- if ("value" in desc) {
214
- desc.value = clone(desc.value, settings, refs);
215
- }
216
- Object.defineProperty(result, key, desc);
219
+
220
+ // src/options.ts
221
+ function resolveOptions(options) {
222
+ let {
223
+ preserveBufferSharing = false,
224
+ preserveDescriptors = false,
225
+ preserveSymbolKeys = false
226
+ } = options;
227
+ if (typeof preserveBufferSharing !== "boolean") {
228
+ console.warn("Invalid preserveBufferSharing option. Fallback: false.");
229
+ preserveBufferSharing = false;
217
230
  }
218
- return result;
231
+ if (typeof preserveDescriptors !== "boolean") {
232
+ console.warn("Invalid preserveDescriptors option. Fallback: false.");
233
+ preserveDescriptors = false;
234
+ }
235
+ if (typeof preserveSymbolKeys !== "boolean") {
236
+ console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
237
+ preserveSymbolKeys = false;
238
+ }
239
+ return {
240
+ preserveBufferSharing,
241
+ preserveDescriptors,
242
+ preserveSymbolKeys
243
+ };
219
244
  }
220
245
 
221
246
  // src/index.ts
@@ -229,14 +254,10 @@ function clone(value, settings, refs) {
229
254
  if (refs.has(value)) {
230
255
  return refs.get(value);
231
256
  }
232
- const isClonePlainObject = isPlainObject(value);
233
- if (settings.preserveDescriptors && isClonePlainObject) {
234
- return cloneWithDescriptors(value, settings, refs);
235
- }
236
257
  if (Array.isArray(value)) {
237
258
  return cloneArray(value, settings, refs);
238
259
  }
239
- if (isClonePlainObject) {
260
+ if (isPlainObject(value)) {
240
261
  return clonePlainObject(value, settings, refs);
241
262
  }
242
263
  if (value instanceof Map) {
@@ -279,30 +300,6 @@ function clone(value, settings, refs) {
279
300
  refs.set(value, value);
280
301
  return value;
281
302
  }
282
- function resolveOptions(options) {
283
- let {
284
- preserveBufferSharing = false,
285
- preserveDescriptors = false,
286
- preserveSymbolKeys = false
287
- } = options;
288
- if (typeof preserveBufferSharing !== "boolean") {
289
- console.warn("Invalid preserveBufferSharing option. Fallback: false.");
290
- preserveBufferSharing = false;
291
- }
292
- if (typeof preserveDescriptors !== "boolean") {
293
- console.warn("Invalid preserveDescriptors option. Fallback: false.");
294
- preserveDescriptors = false;
295
- }
296
- if (typeof preserveSymbolKeys !== "boolean") {
297
- console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
298
- preserveSymbolKeys = false;
299
- }
300
- return {
301
- preserveBufferSharing,
302
- preserveDescriptors,
303
- preserveSymbolKeys
304
- };
305
- }
306
303
 
307
304
  exports.bunshinClone = bunshinClone;
308
305
  exports.clone = clone;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { OWN_DESCS, OWN_STRING_KEYS, OWN_KEYS, OWN_ENUM_STRING_KEYS, OWN_ENUM_KEYS } from '@y14e/own';
1
+ import { OWN_ENUM_STRING_KEYS, OWN_ENUM_KEYS, OWN_DESCS, OWN_STRING_KEYS, OWN_KEYS } from '@y14e/own';
2
2
 
3
3
  // src/utils.ts
4
4
  function isObject(value) {
@@ -149,8 +149,22 @@ function cloneMap(map, settings, refs) {
149
149
  function clonePlainObject(object, settings, refs) {
150
150
  const result = Object.create(Object.getPrototypeOf(object));
151
151
  refs.set(object, result);
152
- for (const key of (!settings.preserveSymbolKeys ? OWN_ENUM_STRING_KEYS : OWN_ENUM_KEYS)(object)) {
153
- result[key] = clone(object[key], settings, refs);
152
+ const { preserveDescriptors, preserveSymbolKeys } = settings;
153
+ if (!preserveDescriptors) {
154
+ for (const key of (!preserveSymbolKeys ? OWN_ENUM_STRING_KEYS : OWN_ENUM_KEYS)(object)) {
155
+ result[key] = clone(object[key], settings, refs);
156
+ }
157
+ return result;
158
+ }
159
+ const descs = OWN_DESCS(object);
160
+ for (const key of (!preserveSymbolKeys ? OWN_STRING_KEYS : OWN_KEYS)(
161
+ object
162
+ )) {
163
+ const desc = { ...descs[key] };
164
+ if ("value" in desc) {
165
+ desc.value = clone(desc.value, settings, refs);
166
+ }
167
+ Object.defineProperty(result, key, desc);
154
168
  }
155
169
  return result;
156
170
  }
@@ -200,20 +214,31 @@ function cloneURLSearchParams(params, refs) {
200
214
  refs.set(params, result);
201
215
  return result;
202
216
  }
203
- function cloneWithDescriptors(object, settings, refs) {
204
- const result = Object.create(Object.getPrototypeOf(object));
205
- refs.set(object, result);
206
- const descs = OWN_DESCS(object);
207
- for (const key of (!settings.preserveSymbolKeys ? OWN_STRING_KEYS : OWN_KEYS)(
208
- object
209
- )) {
210
- const desc = { ...descs[key] };
211
- if ("value" in desc) {
212
- desc.value = clone(desc.value, settings, refs);
213
- }
214
- Object.defineProperty(result, key, desc);
217
+
218
+ // src/options.ts
219
+ function resolveOptions(options) {
220
+ let {
221
+ preserveBufferSharing = false,
222
+ preserveDescriptors = false,
223
+ preserveSymbolKeys = false
224
+ } = options;
225
+ if (typeof preserveBufferSharing !== "boolean") {
226
+ console.warn("Invalid preserveBufferSharing option. Fallback: false.");
227
+ preserveBufferSharing = false;
215
228
  }
216
- return result;
229
+ if (typeof preserveDescriptors !== "boolean") {
230
+ console.warn("Invalid preserveDescriptors option. Fallback: false.");
231
+ preserveDescriptors = false;
232
+ }
233
+ if (typeof preserveSymbolKeys !== "boolean") {
234
+ console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
235
+ preserveSymbolKeys = false;
236
+ }
237
+ return {
238
+ preserveBufferSharing,
239
+ preserveDescriptors,
240
+ preserveSymbolKeys
241
+ };
217
242
  }
218
243
 
219
244
  // src/index.ts
@@ -227,14 +252,10 @@ function clone(value, settings, refs) {
227
252
  if (refs.has(value)) {
228
253
  return refs.get(value);
229
254
  }
230
- const isClonePlainObject = isPlainObject(value);
231
- if (settings.preserveDescriptors && isClonePlainObject) {
232
- return cloneWithDescriptors(value, settings, refs);
233
- }
234
255
  if (Array.isArray(value)) {
235
256
  return cloneArray(value, settings, refs);
236
257
  }
237
- if (isClonePlainObject) {
258
+ if (isPlainObject(value)) {
238
259
  return clonePlainObject(value, settings, refs);
239
260
  }
240
261
  if (value instanceof Map) {
@@ -277,29 +298,5 @@ function clone(value, settings, refs) {
277
298
  refs.set(value, value);
278
299
  return value;
279
300
  }
280
- function resolveOptions(options) {
281
- let {
282
- preserveBufferSharing = false,
283
- preserveDescriptors = false,
284
- preserveSymbolKeys = false
285
- } = options;
286
- if (typeof preserveBufferSharing !== "boolean") {
287
- console.warn("Invalid preserveBufferSharing option. Fallback: false.");
288
- preserveBufferSharing = false;
289
- }
290
- if (typeof preserveDescriptors !== "boolean") {
291
- console.warn("Invalid preserveDescriptors option. Fallback: false.");
292
- preserveDescriptors = false;
293
- }
294
- if (typeof preserveSymbolKeys !== "boolean") {
295
- console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
296
- preserveSymbolKeys = false;
297
- }
298
- return {
299
- preserveBufferSharing,
300
- preserveDescriptors,
301
- preserveSymbolKeys
302
- };
303
- }
304
301
 
305
302
  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.4",
3
+ "version": "2.0.6",
4
4
  "description": "High-performance deep clone utility with descriptor support",
5
5
  "keywords": [
6
6
  "array",
@@ -68,6 +68,9 @@
68
68
  "prepublishOnly": "npm run build",
69
69
  "test": "bun test"
70
70
  },
71
+ "dependencies": {
72
+ "@y14e/own": "^1.0.3"
73
+ },
71
74
  "devDependencies": {
72
75
  "bun-types": "latest",
73
76
  "tsup": "^8.0.0",
@@ -79,8 +82,5 @@
79
82
  },
80
83
  "allowScripts": {
81
84
  "esbuild": true
82
- },
83
- "dependencies": {
84
- "@y14e/own": "^1.0.3"
85
85
  }
86
86
  }