bunshin-clone 1.9.2 → 1.9.3

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
@@ -11,9 +11,6 @@ function isPlainObject(value) {
11
11
  const proto = Object.getPrototypeOf(value);
12
12
  return proto === Object.prototype || proto === null;
13
13
  }
14
- function isUnsafeKey(key) {
15
- return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
16
- }
17
14
 
18
15
  // src/clone/array.ts
19
16
  function cloneArray(array, settings, refs) {
@@ -80,14 +77,6 @@ function cloneDate(date, refs) {
80
77
  return result;
81
78
  }
82
79
 
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
80
  // src/clone/error.ts
92
81
  function cloneError(error, settings, refs) {
93
82
  const result = createErrorInstance(error, settings, refs);
@@ -105,7 +94,7 @@ function cloneError(error, settings, refs) {
105
94
  if ("cause" in error && cause !== void 0) {
106
95
  result.cause = clone(cause, settings, refs);
107
96
  }
108
- for (const key of OWN_ENUM_STRING_KEYS(error)) {
97
+ for (const key of Object.keys(error)) {
109
98
  Reflect.set(result, key, clone(Reflect.get(error, key), settings, refs));
110
99
  }
111
100
  return result;
@@ -158,18 +147,53 @@ function cloneMap(map, settings, refs) {
158
147
  return result;
159
148
  }
160
149
 
150
+ // src/own.ts
151
+ var OWN_DESCS = Object.getOwnPropertyDescriptors;
152
+ var OWN_ENUM_STRING_KEYS = (object) => {
153
+ const keys = Object.keys(object);
154
+ for (const key of keys) {
155
+ if (isUnsafeKey(key)) {
156
+ keys.splice(keys.indexOf(key), 1);
157
+ }
158
+ }
159
+ return keys;
160
+ };
161
+ var OWN_ENUM_SYMBOL_KEYS = (object) => {
162
+ const keys = OWN_SYMBOL_KEYS(object);
163
+ for (const key of keys) {
164
+ if (!Object.prototype.propertyIsEnumerable.call(object, key)) {
165
+ keys.splice(keys.indexOf(key), 1);
166
+ }
167
+ }
168
+ return keys;
169
+ };
170
+ var OWN_STRING_KEYS = (object) => {
171
+ const keys = Object.getOwnPropertyNames(object);
172
+ for (const key of keys) {
173
+ if (isUnsafeKey(key)) {
174
+ keys.splice(keys.indexOf(key), 1);
175
+ }
176
+ }
177
+ return keys;
178
+ };
179
+ var OWN_SYMBOL_KEYS = Object.getOwnPropertySymbols;
180
+ function isUnsafeKey(key) {
181
+ return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
182
+ }
183
+
161
184
  // src/clone/plain-object.ts
162
185
  function clonePlainObject(object, settings, refs) {
163
186
  const result = Object.create(Object.getPrototypeOf(object));
164
187
  refs.set(object, result);
188
+ function copy(key) {
189
+ result[key] = clone(object[key], settings, refs);
190
+ }
165
191
  for (const key of OWN_ENUM_STRING_KEYS(object)) {
166
- if (!isUnsafeKey(key)) {
167
- result[key] = clone(object[key], settings, refs);
168
- }
192
+ copy(key);
169
193
  }
170
194
  if (settings.preserveSymbolKeys) {
171
195
  for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
172
- result[key] = clone(object[key], settings, refs);
196
+ copy(key);
173
197
  }
174
198
  }
175
199
  return result;
@@ -226,10 +250,7 @@ function cloneWithDescriptors(object, settings, refs) {
226
250
  const result = Object.create(Object.getPrototypeOf(object));
227
251
  refs.set(object, result);
228
252
  const descs = OWN_DESCS(object);
229
- for (const key of OWN_STRING_KEYS(object)) {
230
- if (isUnsafeKey(key)) {
231
- continue;
232
- }
253
+ function copy(key) {
233
254
  const desc = { ...descs[key] };
234
255
  if ("value" in desc) {
235
256
  desc.value = clone(desc.value, settings, refs);
@@ -242,19 +263,12 @@ function cloneWithDescriptors(object, settings, refs) {
242
263
  }
243
264
  }
244
265
  }
266
+ for (const key of OWN_STRING_KEYS(object)) {
267
+ copy(key);
268
+ }
245
269
  if (settings.preserveSymbolKeys) {
246
270
  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
- }
271
+ copy(key);
258
272
  }
259
273
  }
260
274
  return result;
@@ -362,4 +376,3 @@ exports.clone = clone;
362
376
  exports.isObject = isObject;
363
377
  exports.isPlainObject = isPlainObject;
364
378
  exports.isPrimitiveArray = isPrimitiveArray;
365
- exports.isUnsafeKey = isUnsafeKey;
package/dist/index.d.cts CHANGED
@@ -13,19 +13,15 @@ declare function isPrimitiveArray(array: unknown[]): boolean;
13
13
  declare const OWN_DESCS: <T>(o: T) => { [P in keyof T]: TypedPropertyDescriptor<T[P]>; } & {
14
14
  [x: string]: PropertyDescriptor;
15
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[];
16
+ declare const OWN_ENUM_STRING_KEYS: (object: PlainObject) => string[];
17
+ declare const OWN_ENUM_SYMBOL_KEYS: (object: PlainObject) => symbol[];
18
+ declare const OWN_STRING_KEYS: (object: PlainObject) => string[];
22
19
  declare const OWN_SYMBOL_KEYS: (o: any) => symbol[];
23
20
 
24
21
  declare function isObject(value: unknown): value is object;
25
22
  declare function isPlainObject(value: unknown): value is PlainObject;
26
- declare function isUnsafeKey(key: PropertyKey): boolean;
27
23
 
28
24
  declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>): T;
29
25
  declare function clone<T>(value: T, settings: BunshinCloneOptions, refs: Refs): T;
30
26
 
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 };
27
+ 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 };
package/dist/index.d.ts CHANGED
@@ -13,19 +13,15 @@ declare function isPrimitiveArray(array: unknown[]): boolean;
13
13
  declare const OWN_DESCS: <T>(o: T) => { [P in keyof T]: TypedPropertyDescriptor<T[P]>; } & {
14
14
  [x: string]: PropertyDescriptor;
15
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[];
16
+ declare const OWN_ENUM_STRING_KEYS: (object: PlainObject) => string[];
17
+ declare const OWN_ENUM_SYMBOL_KEYS: (object: PlainObject) => symbol[];
18
+ declare const OWN_STRING_KEYS: (object: PlainObject) => string[];
22
19
  declare const OWN_SYMBOL_KEYS: (o: any) => symbol[];
23
20
 
24
21
  declare function isObject(value: unknown): value is object;
25
22
  declare function isPlainObject(value: unknown): value is PlainObject;
26
- declare function isUnsafeKey(key: PropertyKey): boolean;
27
23
 
28
24
  declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>): T;
29
25
  declare function clone<T>(value: T, settings: BunshinCloneOptions, refs: Refs): T;
30
26
 
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 };
27
+ 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 };
package/dist/index.js CHANGED
@@ -9,9 +9,6 @@ function isPlainObject(value) {
9
9
  const proto = Object.getPrototypeOf(value);
10
10
  return proto === Object.prototype || proto === null;
11
11
  }
12
- function isUnsafeKey(key) {
13
- return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
14
- }
15
12
 
16
13
  // src/clone/array.ts
17
14
  function cloneArray(array, settings, refs) {
@@ -78,14 +75,6 @@ function cloneDate(date, refs) {
78
75
  return result;
79
76
  }
80
77
 
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
78
  // src/clone/error.ts
90
79
  function cloneError(error, settings, refs) {
91
80
  const result = createErrorInstance(error, settings, refs);
@@ -103,7 +92,7 @@ function cloneError(error, settings, refs) {
103
92
  if ("cause" in error && cause !== void 0) {
104
93
  result.cause = clone(cause, settings, refs);
105
94
  }
106
- for (const key of OWN_ENUM_STRING_KEYS(error)) {
95
+ for (const key of Object.keys(error)) {
107
96
  Reflect.set(result, key, clone(Reflect.get(error, key), settings, refs));
108
97
  }
109
98
  return result;
@@ -156,18 +145,53 @@ function cloneMap(map, settings, refs) {
156
145
  return result;
157
146
  }
158
147
 
148
+ // src/own.ts
149
+ var OWN_DESCS = Object.getOwnPropertyDescriptors;
150
+ var 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
+ var 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
+ var 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
+
159
182
  // src/clone/plain-object.ts
160
183
  function clonePlainObject(object, settings, refs) {
161
184
  const result = Object.create(Object.getPrototypeOf(object));
162
185
  refs.set(object, result);
186
+ function copy(key) {
187
+ result[key] = clone(object[key], settings, refs);
188
+ }
163
189
  for (const key of OWN_ENUM_STRING_KEYS(object)) {
164
- if (!isUnsafeKey(key)) {
165
- result[key] = clone(object[key], settings, refs);
166
- }
190
+ copy(key);
167
191
  }
168
192
  if (settings.preserveSymbolKeys) {
169
193
  for (const key of OWN_ENUM_SYMBOL_KEYS(object)) {
170
- result[key] = clone(object[key], settings, refs);
194
+ copy(key);
171
195
  }
172
196
  }
173
197
  return result;
@@ -224,10 +248,7 @@ function cloneWithDescriptors(object, settings, refs) {
224
248
  const result = Object.create(Object.getPrototypeOf(object));
225
249
  refs.set(object, result);
226
250
  const descs = OWN_DESCS(object);
227
- for (const key of OWN_STRING_KEYS(object)) {
228
- if (isUnsafeKey(key)) {
229
- continue;
230
- }
251
+ function copy(key) {
231
252
  const desc = { ...descs[key] };
232
253
  if ("value" in desc) {
233
254
  desc.value = clone(desc.value, settings, refs);
@@ -240,19 +261,12 @@ function cloneWithDescriptors(object, settings, refs) {
240
261
  }
241
262
  }
242
263
  }
264
+ for (const key of OWN_STRING_KEYS(object)) {
265
+ copy(key);
266
+ }
243
267
  if (settings.preserveSymbolKeys) {
244
268
  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
- }
269
+ copy(key);
256
270
  }
257
271
  }
258
272
  return result;
@@ -350,4 +364,4 @@ function resolveOptions(options) {
350
364
  };
351
365
  }
352
366
 
353
- export { OWN_DESCS, OWN_ENUM_STRING_KEYS, OWN_ENUM_SYMBOL_KEYS, OWN_STRING_KEYS, OWN_SYMBOL_KEYS, bunshinClone, clone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
367
+ export { OWN_DESCS, OWN_ENUM_STRING_KEYS, OWN_ENUM_SYMBOL_KEYS, OWN_STRING_KEYS, OWN_SYMBOL_KEYS, 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": "1.9.3",
4
4
  "description": "High-performance deep clone utility with descriptor support",
5
5
  "keywords": [
6
6
  "array",