bunshin-clone 1.8.8 → 1.8.9

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
@@ -22,20 +22,10 @@ function clone(value, settings, refs) {
22
22
  return clonePlainObject(value, settings, refs);
23
23
  }
24
24
  if (value instanceof Map) {
25
- const result = /* @__PURE__ */ new Map();
26
- refs.set(value, result);
27
- for (const [key, v] of value) {
28
- result.set(clone(key, settings, refs), clone(v, settings, refs));
29
- }
30
- return result;
25
+ return cloneMap(value, settings, refs);
31
26
  }
32
27
  if (value instanceof Set) {
33
- const result = /* @__PURE__ */ new Set();
34
- refs.set(value, result);
35
- for (const item of value) {
36
- result.add(clone(item, settings, refs));
37
- }
38
- return result;
28
+ return cloneSet(value, settings, refs);
39
29
  }
40
30
  if (value instanceof Date) {
41
31
  const result = new Date(value.getTime());
@@ -84,11 +74,8 @@ function clone(value, settings, refs) {
84
74
  return result;
85
75
  }
86
76
  if (value instanceof URLSearchParams) {
87
- const result = new URLSearchParams();
77
+ const result = new URLSearchParams(value);
88
78
  refs.set(value, result);
89
- for (const [key, v] of value) {
90
- result.append(key, v);
91
- }
92
79
  return result;
93
80
  }
94
81
  refs.set(value, value);
@@ -98,9 +85,9 @@ function cloneWithDescriptors(object, settings, refs) {
98
85
  const result = Object.create(Object.getPrototypeOf(object));
99
86
  refs.set(object, result);
100
87
  const descs = Object.getOwnPropertyDescriptors(object);
101
- forEachOwnKey(descs, (key) => {
88
+ for (const key of Reflect.ownKeys(descs)) {
102
89
  if (isUnsafeKey(key)) {
103
- return;
90
+ continue;
104
91
  }
105
92
  const desc = { ...descs[key] };
106
93
  if ("value" in desc) {
@@ -113,7 +100,7 @@ function cloneWithDescriptors(object, settings, refs) {
113
100
  throw error;
114
101
  }
115
102
  }
116
- });
103
+ }
117
104
  return result;
118
105
  }
119
106
  function cloneArray(array, settings, refs) {
@@ -150,6 +137,27 @@ function clonePlainObject(object, settings, refs) {
150
137
  }
151
138
  return result;
152
139
  }
140
+ function cloneMap(map, settings, refs) {
141
+ const result = /* @__PURE__ */ new Map();
142
+ refs.set(map, result);
143
+ for (const [key, value] of map) {
144
+ result.set(clone(key, settings, refs), clone(value, settings, refs));
145
+ }
146
+ return result;
147
+ }
148
+ function cloneSet(set, settings, refs) {
149
+ if (isPrimitiveSet(set)) {
150
+ const result2 = new Set(set);
151
+ refs.set(set, result2);
152
+ return result2;
153
+ }
154
+ const result = /* @__PURE__ */ new Set();
155
+ refs.set(set, result);
156
+ for (const item of set) {
157
+ result.add(clone(item, settings, refs));
158
+ }
159
+ return result;
160
+ }
153
161
  function cloneArrayBufferView(view, settings, refs) {
154
162
  if (view instanceof DataView) {
155
163
  const { buffer: buffer2, byteOffset: byteOffset2, byteLength } = view;
@@ -229,14 +237,6 @@ function createErrorInstance(error, settings, refs) {
229
237
  result.name = name;
230
238
  return result;
231
239
  }
232
- function forEachOwnKey(object, callback) {
233
- for (const key of Object.keys(object)) {
234
- callback(key);
235
- }
236
- for (const symbol of Object.getOwnPropertySymbols(object)) {
237
- callback(symbol);
238
- }
239
- }
240
240
  function isObject(value) {
241
241
  return typeof value === "object" && value !== null;
242
242
  }
@@ -250,6 +250,14 @@ function isPlainObject(value) {
250
250
  function isPrimitiveArray(array) {
251
251
  return array.every((item) => !isObject(item));
252
252
  }
253
+ function isPrimitiveSet(set) {
254
+ for (const value of set) {
255
+ if (isObject(value)) {
256
+ return false;
257
+ }
258
+ }
259
+ return true;
260
+ }
253
261
  function isUnsafeKey(key) {
254
262
  return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
255
263
  }
@@ -285,7 +293,6 @@ function resolveOptions(options) {
285
293
  }
286
294
 
287
295
  exports.bunshinClone = bunshinClone;
288
- exports.forEachOwnKey = forEachOwnKey;
289
296
  exports.isObject = isObject;
290
297
  exports.isPlainObject = isPlainObject;
291
298
  exports.isPrimitiveArray = isPrimitiveArray;
package/dist/index.d.cts CHANGED
@@ -5,12 +5,11 @@ interface BunshinCloneOptions {
5
5
  strictDescriptors: boolean;
6
6
  }
7
7
  type PlainObject = Record<PropertyKey, unknown>;
8
- type Refs = WeakMap<object, unknown>;
8
+ type Refs = WeakMap<object, object>;
9
9
  declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>, refs?: Refs): T;
10
- declare function forEachOwnKey(object: PlainObject, callback: (key: string | symbol) => void): void;
11
10
  declare function isObject(value: unknown): value is object;
12
11
  declare function isPlainObject(value: unknown): value is PlainObject;
13
12
  declare function isPrimitiveArray(array: unknown[]): boolean;
14
13
  declare function isUnsafeKey(key: PropertyKey): boolean;
15
14
 
16
- export { type BunshinCloneOptions, type PlainObject, bunshinClone, forEachOwnKey, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
15
+ export { type BunshinCloneOptions, type PlainObject, bunshinClone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
package/dist/index.d.ts CHANGED
@@ -5,12 +5,11 @@ interface BunshinCloneOptions {
5
5
  strictDescriptors: boolean;
6
6
  }
7
7
  type PlainObject = Record<PropertyKey, unknown>;
8
- type Refs = WeakMap<object, unknown>;
8
+ type Refs = WeakMap<object, object>;
9
9
  declare function bunshinClone<T>(value: T, options?: Partial<BunshinCloneOptions>, refs?: Refs): T;
10
- declare function forEachOwnKey(object: PlainObject, callback: (key: string | symbol) => void): void;
11
10
  declare function isObject(value: unknown): value is object;
12
11
  declare function isPlainObject(value: unknown): value is PlainObject;
13
12
  declare function isPrimitiveArray(array: unknown[]): boolean;
14
13
  declare function isUnsafeKey(key: PropertyKey): boolean;
15
14
 
16
- export { type BunshinCloneOptions, type PlainObject, bunshinClone, forEachOwnKey, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
15
+ export { type BunshinCloneOptions, type PlainObject, bunshinClone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
package/dist/index.js CHANGED
@@ -20,20 +20,10 @@ function clone(value, settings, refs) {
20
20
  return clonePlainObject(value, settings, refs);
21
21
  }
22
22
  if (value instanceof Map) {
23
- const result = /* @__PURE__ */ new Map();
24
- refs.set(value, result);
25
- for (const [key, v] of value) {
26
- result.set(clone(key, settings, refs), clone(v, settings, refs));
27
- }
28
- return result;
23
+ return cloneMap(value, settings, refs);
29
24
  }
30
25
  if (value instanceof Set) {
31
- const result = /* @__PURE__ */ new Set();
32
- refs.set(value, result);
33
- for (const item of value) {
34
- result.add(clone(item, settings, refs));
35
- }
36
- return result;
26
+ return cloneSet(value, settings, refs);
37
27
  }
38
28
  if (value instanceof Date) {
39
29
  const result = new Date(value.getTime());
@@ -82,11 +72,8 @@ function clone(value, settings, refs) {
82
72
  return result;
83
73
  }
84
74
  if (value instanceof URLSearchParams) {
85
- const result = new URLSearchParams();
75
+ const result = new URLSearchParams(value);
86
76
  refs.set(value, result);
87
- for (const [key, v] of value) {
88
- result.append(key, v);
89
- }
90
77
  return result;
91
78
  }
92
79
  refs.set(value, value);
@@ -96,9 +83,9 @@ function cloneWithDescriptors(object, settings, refs) {
96
83
  const result = Object.create(Object.getPrototypeOf(object));
97
84
  refs.set(object, result);
98
85
  const descs = Object.getOwnPropertyDescriptors(object);
99
- forEachOwnKey(descs, (key) => {
86
+ for (const key of Reflect.ownKeys(descs)) {
100
87
  if (isUnsafeKey(key)) {
101
- return;
88
+ continue;
102
89
  }
103
90
  const desc = { ...descs[key] };
104
91
  if ("value" in desc) {
@@ -111,7 +98,7 @@ function cloneWithDescriptors(object, settings, refs) {
111
98
  throw error;
112
99
  }
113
100
  }
114
- });
101
+ }
115
102
  return result;
116
103
  }
117
104
  function cloneArray(array, settings, refs) {
@@ -148,6 +135,27 @@ function clonePlainObject(object, settings, refs) {
148
135
  }
149
136
  return result;
150
137
  }
138
+ function cloneMap(map, settings, refs) {
139
+ const result = /* @__PURE__ */ new Map();
140
+ refs.set(map, result);
141
+ for (const [key, value] of map) {
142
+ result.set(clone(key, settings, refs), clone(value, settings, refs));
143
+ }
144
+ return result;
145
+ }
146
+ function cloneSet(set, settings, refs) {
147
+ if (isPrimitiveSet(set)) {
148
+ const result2 = new Set(set);
149
+ refs.set(set, result2);
150
+ return result2;
151
+ }
152
+ const result = /* @__PURE__ */ new Set();
153
+ refs.set(set, result);
154
+ for (const item of set) {
155
+ result.add(clone(item, settings, refs));
156
+ }
157
+ return result;
158
+ }
151
159
  function cloneArrayBufferView(view, settings, refs) {
152
160
  if (view instanceof DataView) {
153
161
  const { buffer: buffer2, byteOffset: byteOffset2, byteLength } = view;
@@ -227,14 +235,6 @@ function createErrorInstance(error, settings, refs) {
227
235
  result.name = name;
228
236
  return result;
229
237
  }
230
- function forEachOwnKey(object, callback) {
231
- for (const key of Object.keys(object)) {
232
- callback(key);
233
- }
234
- for (const symbol of Object.getOwnPropertySymbols(object)) {
235
- callback(symbol);
236
- }
237
- }
238
238
  function isObject(value) {
239
239
  return typeof value === "object" && value !== null;
240
240
  }
@@ -248,6 +248,14 @@ function isPlainObject(value) {
248
248
  function isPrimitiveArray(array) {
249
249
  return array.every((item) => !isObject(item));
250
250
  }
251
+ function isPrimitiveSet(set) {
252
+ for (const value of set) {
253
+ if (isObject(value)) {
254
+ return false;
255
+ }
256
+ }
257
+ return true;
258
+ }
251
259
  function isUnsafeKey(key) {
252
260
  return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
253
261
  }
@@ -282,4 +290,4 @@ function resolveOptions(options) {
282
290
  };
283
291
  }
284
292
 
285
- export { bunshinClone, forEachOwnKey, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
293
+ export { bunshinClone, isObject, isPlainObject, isPrimitiveArray, isUnsafeKey };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunshin-clone",
3
- "version": "1.8.8",
3
+ "version": "1.8.9",
4
4
  "description": "High-performance deep clone utility with descriptor support",
5
5
  "keywords": [
6
6
  "array",