bunshin-clone 1.9.2 → 2.0.0

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,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var own = require('@y14e/own');
4
+
3
5
  // src/utils.ts
4
6
  function isObject(value) {
5
7
  return typeof value === "object" && value !== null;
@@ -11,9 +13,6 @@ function isPlainObject(value) {
11
13
  const proto = Object.getPrototypeOf(value);
12
14
  return proto === Object.prototype || proto === null;
13
15
  }
14
- function isUnsafeKey(key) {
15
- return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
16
- }
17
16
 
18
17
  // src/clone/array.ts
19
18
  function cloneArray(array, settings, refs) {
@@ -80,14 +79,6 @@ function cloneDate(date, refs) {
80
79
  return result;
81
80
  }
82
81
 
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
82
  // src/clone/error.ts
92
83
  function cloneError(error, settings, refs) {
93
84
  const result = createErrorInstance(error, settings, refs);
@@ -105,7 +96,7 @@ function cloneError(error, settings, refs) {
105
96
  if ("cause" in error && cause !== void 0) {
106
97
  result.cause = clone(cause, settings, refs);
107
98
  }
108
- for (const key of OWN_ENUM_STRING_KEYS(error)) {
99
+ for (const key of Object.keys(error)) {
109
100
  Reflect.set(result, key, clone(Reflect.get(error, key), settings, refs));
110
101
  }
111
102
  return result;
@@ -157,19 +148,18 @@ function cloneMap(map, settings, refs) {
157
148
  }
158
149
  return result;
159
150
  }
160
-
161
- // src/clone/plain-object.ts
162
151
  function clonePlainObject(object, settings, refs) {
163
152
  const result = Object.create(Object.getPrototypeOf(object));
164
153
  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
- }
154
+ function copy(key) {
155
+ result[key] = clone(object[key], settings, refs);
156
+ }
157
+ for (const key of own.OWN_ENUM_STRING_KEYS(object)) {
158
+ copy(key);
169
159
  }
170
160
  if (settings.preserveSymbolKeys) {
171
- for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
172
- result[key] = clone(object[key], settings, refs);
161
+ for (const key of own.OWN_ENUM_SYMBOL_KEYS(object)) {
162
+ copy(key);
173
163
  }
174
164
  }
175
165
  return result;
@@ -220,16 +210,11 @@ function cloneURLSearchParams(params, refs) {
220
210
  refs.set(params, result);
221
211
  return result;
222
212
  }
223
-
224
- // src/clone/with-descriptors.ts
225
213
  function cloneWithDescriptors(object, settings, refs) {
226
214
  const result = Object.create(Object.getPrototypeOf(object));
227
215
  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
- }
216
+ const descs = own.OWN_DESCS(object);
217
+ function copy(key) {
233
218
  const desc = { ...descs[key] };
234
219
  if ("value" in desc) {
235
220
  desc.value = clone(desc.value, settings, refs);
@@ -242,19 +227,12 @@ function cloneWithDescriptors(object, settings, refs) {
242
227
  }
243
228
  }
244
229
  }
230
+ for (const key of own.OWN_STRING_KEYS(object)) {
231
+ copy(key);
232
+ }
245
233
  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
- }
234
+ for (const key of own.OWN_SYMBOL_KEYS(object)) {
235
+ copy(key);
258
236
  }
259
237
  }
260
238
  return result;
@@ -352,14 +330,8 @@ function resolveOptions(options) {
352
330
  };
353
331
  }
354
332
 
355
- exports.OWN_DESCS = OWN_DESCS;
356
- exports.OWN_ENUM_STRING_KEYS = OWN_ENUM_STRING_KEYS;
357
- exports.OWN_ENUM_SYMBOL_KEYS = OWN_ENUM_SYMBOL_KEYS;
358
- exports.OWN_STRING_KEYS = OWN_STRING_KEYS;
359
- exports.OWN_SYMBOL_KEYS = OWN_SYMBOL_KEYS;
360
333
  exports.bunshinClone = bunshinClone;
361
334
  exports.clone = clone;
362
335
  exports.isObject = isObject;
363
336
  exports.isPlainObject = isPlainObject;
364
337
  exports.isPrimitiveArray = isPrimitiveArray;
365
- exports.isUnsafeKey = isUnsafeKey;
package/dist/index.d.cts CHANGED
@@ -10,22 +10,10 @@ type PlainObject = Record<PropertyKey, unknown>;
10
10
 
11
11
  declare function isPrimitiveArray(array: unknown[]): boolean;
12
12
 
13
- declare const OWN_DESCS: <T>(o: T) => { [P in keyof T]: TypedPropertyDescriptor<T[P]>; } & {
14
- [x: string]: PropertyDescriptor;
15
- };
16
- declare const OWN_ENUM_STRING_KEYS: {
17
- (o: object): string[];
18
- (o: {}): string[];
19
- };
20
- declare const OWN_ENUM_SYMBOL_KEYS: (o: PlainObject) => symbol[];
21
- declare const OWN_STRING_KEYS: (o: any) => string[];
22
- declare const OWN_SYMBOL_KEYS: (o: any) => symbol[];
23
-
24
13
  declare function isObject(value: unknown): value is object;
25
14
  declare function isPlainObject(value: unknown): value is PlainObject;
26
- declare function isUnsafeKey(key: PropertyKey): boolean;
27
15
 
28
16
  declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>): T;
29
17
  declare function clone<T>(value: T, settings: BunshinCloneOptions, refs: Refs): T;
30
18
 
31
- export { type BunshinCloneOptions, OWN_DESCS, OWN_ENUM_STRING_KEYS, OWN_ENUM_SYMBOL_KEYS, OWN_STRING_KEYS, OWN_SYMBOL_KEYS, type PlainObject, bunshinClone, clone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
19
+ export { type BunshinCloneOptions, type PlainObject, bunshinClone, clone, isObject, isPlainObject, isPrimitiveArray };
package/dist/index.d.ts CHANGED
@@ -10,22 +10,10 @@ type PlainObject = Record<PropertyKey, unknown>;
10
10
 
11
11
  declare function isPrimitiveArray(array: unknown[]): boolean;
12
12
 
13
- declare const OWN_DESCS: <T>(o: T) => { [P in keyof T]: TypedPropertyDescriptor<T[P]>; } & {
14
- [x: string]: PropertyDescriptor;
15
- };
16
- declare const OWN_ENUM_STRING_KEYS: {
17
- (o: object): string[];
18
- (o: {}): string[];
19
- };
20
- declare const OWN_ENUM_SYMBOL_KEYS: (o: PlainObject) => symbol[];
21
- declare const OWN_STRING_KEYS: (o: any) => string[];
22
- declare const OWN_SYMBOL_KEYS: (o: any) => symbol[];
23
-
24
13
  declare function isObject(value: unknown): value is object;
25
14
  declare function isPlainObject(value: unknown): value is PlainObject;
26
- declare function isUnsafeKey(key: PropertyKey): boolean;
27
15
 
28
16
  declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>): T;
29
17
  declare function clone<T>(value: T, settings: BunshinCloneOptions, refs: Refs): T;
30
18
 
31
- export { type BunshinCloneOptions, OWN_DESCS, OWN_ENUM_STRING_KEYS, OWN_ENUM_SYMBOL_KEYS, OWN_STRING_KEYS, OWN_SYMBOL_KEYS, type PlainObject, bunshinClone, clone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
19
+ export { type BunshinCloneOptions, type PlainObject, bunshinClone, clone, isObject, isPlainObject, isPrimitiveArray };
package/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { OWN_DESCS, OWN_STRING_KEYS, OWN_SYMBOL_KEYS, OWN_ENUM_STRING_KEYS, OWN_ENUM_SYMBOL_KEYS } from '@y14e/own';
2
+
1
3
  // src/utils.ts
2
4
  function isObject(value) {
3
5
  return typeof value === "object" && value !== null;
@@ -9,9 +11,6 @@ function isPlainObject(value) {
9
11
  const proto = Object.getPrototypeOf(value);
10
12
  return proto === Object.prototype || proto === null;
11
13
  }
12
- function isUnsafeKey(key) {
13
- return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
14
- }
15
14
 
16
15
  // src/clone/array.ts
17
16
  function cloneArray(array, settings, refs) {
@@ -78,14 +77,6 @@ function cloneDate(date, refs) {
78
77
  return result;
79
78
  }
80
79
 
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
80
  // src/clone/error.ts
90
81
  function cloneError(error, settings, refs) {
91
82
  const result = createErrorInstance(error, settings, refs);
@@ -103,7 +94,7 @@ function cloneError(error, settings, refs) {
103
94
  if ("cause" in error && cause !== void 0) {
104
95
  result.cause = clone(cause, settings, refs);
105
96
  }
106
- for (const key of OWN_ENUM_STRING_KEYS(error)) {
97
+ for (const key of Object.keys(error)) {
107
98
  Reflect.set(result, key, clone(Reflect.get(error, key), settings, refs));
108
99
  }
109
100
  return result;
@@ -155,19 +146,18 @@ function cloneMap(map, settings, refs) {
155
146
  }
156
147
  return result;
157
148
  }
158
-
159
- // src/clone/plain-object.ts
160
149
  function clonePlainObject(object, settings, refs) {
161
150
  const result = Object.create(Object.getPrototypeOf(object));
162
151
  refs.set(object, result);
152
+ function copy(key) {
153
+ result[key] = clone(object[key], settings, refs);
154
+ }
163
155
  for (const key of OWN_ENUM_STRING_KEYS(object)) {
164
- if (!isUnsafeKey(key)) {
165
- result[key] = clone(object[key], settings, refs);
166
- }
156
+ copy(key);
167
157
  }
168
158
  if (settings.preserveSymbolKeys) {
169
159
  for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
170
- result[key] = clone(object[key], settings, refs);
160
+ copy(key);
171
161
  }
172
162
  }
173
163
  return result;
@@ -218,16 +208,11 @@ function cloneURLSearchParams(params, refs) {
218
208
  refs.set(params, result);
219
209
  return result;
220
210
  }
221
-
222
- // src/clone/with-descriptors.ts
223
211
  function cloneWithDescriptors(object, settings, refs) {
224
212
  const result = Object.create(Object.getPrototypeOf(object));
225
213
  refs.set(object, result);
226
214
  const descs = OWN_DESCS(object);
227
- for (const key of OWN_STRING_KEYS(object)) {
228
- if (isUnsafeKey(key)) {
229
- continue;
230
- }
215
+ function copy(key) {
231
216
  const desc = { ...descs[key] };
232
217
  if ("value" in desc) {
233
218
  desc.value = clone(desc.value, settings, refs);
@@ -240,19 +225,12 @@ function cloneWithDescriptors(object, settings, refs) {
240
225
  }
241
226
  }
242
227
  }
228
+ for (const key of OWN_STRING_KEYS(object)) {
229
+ copy(key);
230
+ }
243
231
  if (settings.preserveSymbolKeys) {
244
232
  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
- }
233
+ copy(key);
256
234
  }
257
235
  }
258
236
  return result;
@@ -350,4 +328,4 @@ function resolveOptions(options) {
350
328
  };
351
329
  }
352
330
 
353
- export { OWN_DESCS, OWN_ENUM_STRING_KEYS, OWN_ENUM_SYMBOL_KEYS, OWN_STRING_KEYS, OWN_SYMBOL_KEYS, bunshinClone, clone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
331
+ export { bunshinClone, clone, isObject, isPlainObject, isPrimitiveArray };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunshin-clone",
3
- "version": "1.9.2",
3
+ "version": "2.0.0",
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.0"
73
+ },
71
74
  "devDependencies": {
72
75
  "bun-types": "latest",
73
76
  "tsup": "^8.0.0",