bunshin-clone 2.0.1 → 2.0.2
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/README.md +0 -153
- package/dist/index.bundle.cjs +5 -17
- package/dist/index.bundle.js +5 -17
- package/dist/index.cjs +5 -17
- package/dist/index.d.cts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +5 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,7 +37,6 @@ interface BunshinCloneOptions {
|
|
|
37
37
|
preserveBufferSharing: boolean; // default: false
|
|
38
38
|
preserveDescriptors: boolean; // default: false
|
|
39
39
|
preserveSymbolKeys: boolean; // default: false
|
|
40
|
-
strictDescriptors: boolean; // default: false
|
|
41
40
|
}
|
|
42
41
|
```
|
|
43
42
|
|
|
@@ -52,155 +51,3 @@ If `true`, preserves property descriptors, including non-enumerable properties.
|
|
|
52
51
|
### `preserveSymbolKeys`
|
|
53
52
|
|
|
54
53
|
If `true`, preserves symbol keys (slower).
|
|
55
|
-
|
|
56
|
-
### `strictDescriptors`
|
|
57
|
-
|
|
58
|
-
If `true`, throws if property descriptor cannot be copied (e.g. non-configurable or non-writable).
|
|
59
|
-
|
|
60
|
-
## 📖 Details
|
|
61
|
-
|
|
62
|
-
<details>
|
|
63
|
-
<summary>Read more</summary>
|
|
64
|
-
|
|
65
|
-
### Example
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
const source = { foo: 1, nested: { x: 1 } };
|
|
69
|
-
|
|
70
|
-
const result = bunshinClone(source);
|
|
71
|
-
|
|
72
|
-
console.log(result);
|
|
73
|
-
// { foo: 1, nested: { x: 1 } }
|
|
74
|
-
|
|
75
|
-
console.log(result === source); // false
|
|
76
|
-
console.log(result.nested === source.nested); // false
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### Supported Types
|
|
80
|
-
|
|
81
|
-
bunshin-clone correctly handles:
|
|
82
|
-
|
|
83
|
-
- Object (plain + prototype preserved)
|
|
84
|
-
- Array
|
|
85
|
-
- Map
|
|
86
|
-
- Set
|
|
87
|
-
- Date
|
|
88
|
-
- RegExp
|
|
89
|
-
- ArrayBuffer
|
|
90
|
-
- DataView
|
|
91
|
-
- TypedArray (Uint8Array, etc.)
|
|
92
|
-
- DOMException / Error
|
|
93
|
-
- Blob
|
|
94
|
-
- ImageData
|
|
95
|
-
- URL
|
|
96
|
-
- URLSearchParams
|
|
97
|
-
|
|
98
|
-
### Circular ref
|
|
99
|
-
|
|
100
|
-
```ts
|
|
101
|
-
const a: any = { x: 1 };
|
|
102
|
-
a.self = a;
|
|
103
|
-
|
|
104
|
-
const result = bunshinClone(a);
|
|
105
|
-
|
|
106
|
-
result.self === result; // true
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### Descriptor Behavior
|
|
110
|
-
|
|
111
|
-
#### Default (fast path)
|
|
112
|
-
|
|
113
|
-
```ts
|
|
114
|
-
const source = {
|
|
115
|
-
get x() {
|
|
116
|
-
return 42;
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const result = bunshinClone(source);
|
|
121
|
-
|
|
122
|
-
result.x; // 42
|
|
123
|
-
// getter is NOT preserved
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
#### preserveDescriptors: true
|
|
127
|
-
|
|
128
|
-
```ts
|
|
129
|
-
const source = {};
|
|
130
|
-
Object.defineProperty(source, 'x', {
|
|
131
|
-
get: () => 42,
|
|
132
|
-
enumerable: true,
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
const result = bunshinClone(source, {
|
|
136
|
-
preserveDescriptors: true,
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
Object.getOwnPropertyDescriptor(result, 'x')?.get;
|
|
140
|
-
// => preserved
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
#### Unsupported / Pass-through Types
|
|
144
|
-
|
|
145
|
-
Some values are returned as-is:
|
|
146
|
-
|
|
147
|
-
- Function
|
|
148
|
-
- WeakMap / WeakSet
|
|
149
|
-
- Proxy (not cloned)
|
|
150
|
-
- Other non-cloneable host objects
|
|
151
|
-
|
|
152
|
-
```ts
|
|
153
|
-
const fn = () => {};
|
|
154
|
-
|
|
155
|
-
bunshinClone(fn) === fn; // true
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### Design Notes
|
|
159
|
-
|
|
160
|
-
#### Deep clone (no structural sharing)
|
|
161
|
-
|
|
162
|
-
Unlike merge utilities, bunshin-clone always produces a new structure:
|
|
163
|
-
|
|
164
|
-
```ts
|
|
165
|
-
const source = { a: { b: 1 } };
|
|
166
|
-
|
|
167
|
-
const result = bunshinClone(source);
|
|
168
|
-
|
|
169
|
-
result !== source; // true
|
|
170
|
-
result.a !== source.a; // true
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
#### Getter / Setter behavior
|
|
174
|
-
|
|
175
|
-
- Default: evaluated and converted to value
|
|
176
|
-
- preserveDescriptors: preserved as-is
|
|
177
|
-
|
|
178
|
-
#### Descriptor safety
|
|
179
|
-
|
|
180
|
-
When `preserveDescriptors` is enabled:
|
|
181
|
-
|
|
182
|
-
- descriptors are cloned safely
|
|
183
|
-
- original object is never mutated
|
|
184
|
-
- errors are controlled via `strictDescriptors`
|
|
185
|
-
|
|
186
|
-
### Performance
|
|
187
|
-
|
|
188
|
-
- No proxy / no diffing
|
|
189
|
-
- Minimal branching
|
|
190
|
-
- Fast path for plain objects and arrays
|
|
191
|
-
- Competitive with structuredClone in many cases
|
|
192
|
-
|
|
193
|
-
### Comparison
|
|
194
|
-
|
|
195
|
-
| Feature | Bunshin Clone | structuredClone | lodash.clonedeep |
|
|
196
|
-
| ------------------- | ------------- | --------------- | ---------------- |
|
|
197
|
-
| Circular refs | ✅ | ✅ | ✅ |
|
|
198
|
-
| Map / Set | ✅ | ✅ | ⚠️ (partial) |
|
|
199
|
-
| TypedArray | ✅ | ✅ | ⚠️ (shallow) |
|
|
200
|
-
| Descriptor support | ✅ | ❌ | ❌ |
|
|
201
|
-
| Functions | pass-through | ❌ (throws) | pass-through |
|
|
202
|
-
| Prototype preserved | ✅ | ❌ | ⚠️ |
|
|
203
|
-
| Custom control | ✅ | ❌ | ❌ |
|
|
204
|
-
| Performance | ⚡ fast | ⚡ fast | 🐢 slower |
|
|
205
|
-
|
|
206
|
-
</details>
|
package/dist/index.bundle.cjs
CHANGED
|
@@ -253,13 +253,7 @@ function cloneWithDescriptors(object, settings, refs) {
|
|
|
253
253
|
if ("value" in desc) {
|
|
254
254
|
desc.value = clone(desc.value, settings, refs);
|
|
255
255
|
}
|
|
256
|
-
|
|
257
|
-
Object.defineProperty(result, key, desc);
|
|
258
|
-
} catch (error) {
|
|
259
|
-
if (settings.strictDescriptors) {
|
|
260
|
-
throw error;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
256
|
+
Object.defineProperty(result, key, desc);
|
|
263
257
|
}
|
|
264
258
|
for (const key of OWN_STRING_KEYS(object)) {
|
|
265
259
|
copy(key);
|
|
@@ -337,13 +331,8 @@ function resolveOptions(options) {
|
|
|
337
331
|
let {
|
|
338
332
|
preserveBufferSharing = false,
|
|
339
333
|
preserveDescriptors = false,
|
|
340
|
-
strictDescriptors = false,
|
|
341
334
|
preserveSymbolKeys = false
|
|
342
335
|
} = options;
|
|
343
|
-
if (typeof preserveSymbolKeys !== "boolean") {
|
|
344
|
-
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
345
|
-
preserveSymbolKeys = false;
|
|
346
|
-
}
|
|
347
336
|
if (typeof preserveBufferSharing !== "boolean") {
|
|
348
337
|
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
349
338
|
preserveBufferSharing = false;
|
|
@@ -352,15 +341,14 @@ function resolveOptions(options) {
|
|
|
352
341
|
console.warn("Invalid preserveDescriptors option. Fallback: false.");
|
|
353
342
|
preserveDescriptors = false;
|
|
354
343
|
}
|
|
355
|
-
if (typeof
|
|
356
|
-
console.warn("Invalid
|
|
357
|
-
|
|
344
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
345
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
346
|
+
preserveSymbolKeys = false;
|
|
358
347
|
}
|
|
359
348
|
return {
|
|
360
349
|
preserveBufferSharing,
|
|
361
350
|
preserveDescriptors,
|
|
362
|
-
preserveSymbolKeys
|
|
363
|
-
strictDescriptors
|
|
351
|
+
preserveSymbolKeys
|
|
364
352
|
};
|
|
365
353
|
}
|
|
366
354
|
|
package/dist/index.bundle.js
CHANGED
|
@@ -251,13 +251,7 @@ function cloneWithDescriptors(object, settings, refs) {
|
|
|
251
251
|
if ("value" in desc) {
|
|
252
252
|
desc.value = clone(desc.value, settings, refs);
|
|
253
253
|
}
|
|
254
|
-
|
|
255
|
-
Object.defineProperty(result, key, desc);
|
|
256
|
-
} catch (error) {
|
|
257
|
-
if (settings.strictDescriptors) {
|
|
258
|
-
throw error;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
254
|
+
Object.defineProperty(result, key, desc);
|
|
261
255
|
}
|
|
262
256
|
for (const key of OWN_STRING_KEYS(object)) {
|
|
263
257
|
copy(key);
|
|
@@ -335,13 +329,8 @@ function resolveOptions(options) {
|
|
|
335
329
|
let {
|
|
336
330
|
preserveBufferSharing = false,
|
|
337
331
|
preserveDescriptors = false,
|
|
338
|
-
strictDescriptors = false,
|
|
339
332
|
preserveSymbolKeys = false
|
|
340
333
|
} = options;
|
|
341
|
-
if (typeof preserveSymbolKeys !== "boolean") {
|
|
342
|
-
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
343
|
-
preserveSymbolKeys = false;
|
|
344
|
-
}
|
|
345
334
|
if (typeof preserveBufferSharing !== "boolean") {
|
|
346
335
|
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
347
336
|
preserveBufferSharing = false;
|
|
@@ -350,15 +339,14 @@ function resolveOptions(options) {
|
|
|
350
339
|
console.warn("Invalid preserveDescriptors option. Fallback: false.");
|
|
351
340
|
preserveDescriptors = false;
|
|
352
341
|
}
|
|
353
|
-
if (typeof
|
|
354
|
-
console.warn("Invalid
|
|
355
|
-
|
|
342
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
343
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
344
|
+
preserveSymbolKeys = false;
|
|
356
345
|
}
|
|
357
346
|
return {
|
|
358
347
|
preserveBufferSharing,
|
|
359
348
|
preserveDescriptors,
|
|
360
|
-
preserveSymbolKeys
|
|
361
|
-
strictDescriptors
|
|
349
|
+
preserveSymbolKeys
|
|
362
350
|
};
|
|
363
351
|
}
|
|
364
352
|
|
package/dist/index.cjs
CHANGED
|
@@ -219,13 +219,7 @@ function cloneWithDescriptors(object, settings, refs) {
|
|
|
219
219
|
if ("value" in desc) {
|
|
220
220
|
desc.value = clone(desc.value, settings, refs);
|
|
221
221
|
}
|
|
222
|
-
|
|
223
|
-
Object.defineProperty(result, key, desc);
|
|
224
|
-
} catch (error) {
|
|
225
|
-
if (settings.strictDescriptors) {
|
|
226
|
-
throw error;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
222
|
+
Object.defineProperty(result, key, desc);
|
|
229
223
|
}
|
|
230
224
|
for (const key of own.OWN_STRING_KEYS(object)) {
|
|
231
225
|
copy(key);
|
|
@@ -303,13 +297,8 @@ function resolveOptions(options) {
|
|
|
303
297
|
let {
|
|
304
298
|
preserveBufferSharing = false,
|
|
305
299
|
preserveDescriptors = false,
|
|
306
|
-
strictDescriptors = false,
|
|
307
300
|
preserveSymbolKeys = false
|
|
308
301
|
} = options;
|
|
309
|
-
if (typeof preserveSymbolKeys !== "boolean") {
|
|
310
|
-
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
311
|
-
preserveSymbolKeys = false;
|
|
312
|
-
}
|
|
313
302
|
if (typeof preserveBufferSharing !== "boolean") {
|
|
314
303
|
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
315
304
|
preserveBufferSharing = false;
|
|
@@ -318,15 +307,14 @@ function resolveOptions(options) {
|
|
|
318
307
|
console.warn("Invalid preserveDescriptors option. Fallback: false.");
|
|
319
308
|
preserveDescriptors = false;
|
|
320
309
|
}
|
|
321
|
-
if (typeof
|
|
322
|
-
console.warn("Invalid
|
|
323
|
-
|
|
310
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
311
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
312
|
+
preserveSymbolKeys = false;
|
|
324
313
|
}
|
|
325
314
|
return {
|
|
326
315
|
preserveBufferSharing,
|
|
327
316
|
preserveDescriptors,
|
|
328
|
-
preserveSymbolKeys
|
|
329
|
-
strictDescriptors
|
|
317
|
+
preserveSymbolKeys
|
|
330
318
|
};
|
|
331
319
|
}
|
|
332
320
|
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -217,13 +217,7 @@ function cloneWithDescriptors(object, settings, refs) {
|
|
|
217
217
|
if ("value" in desc) {
|
|
218
218
|
desc.value = clone(desc.value, settings, refs);
|
|
219
219
|
}
|
|
220
|
-
|
|
221
|
-
Object.defineProperty(result, key, desc);
|
|
222
|
-
} catch (error) {
|
|
223
|
-
if (settings.strictDescriptors) {
|
|
224
|
-
throw error;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
220
|
+
Object.defineProperty(result, key, desc);
|
|
227
221
|
}
|
|
228
222
|
for (const key of OWN_STRING_KEYS(object)) {
|
|
229
223
|
copy(key);
|
|
@@ -301,13 +295,8 @@ function resolveOptions(options) {
|
|
|
301
295
|
let {
|
|
302
296
|
preserveBufferSharing = false,
|
|
303
297
|
preserveDescriptors = false,
|
|
304
|
-
strictDescriptors = false,
|
|
305
298
|
preserveSymbolKeys = false
|
|
306
299
|
} = options;
|
|
307
|
-
if (typeof preserveSymbolKeys !== "boolean") {
|
|
308
|
-
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
309
|
-
preserveSymbolKeys = false;
|
|
310
|
-
}
|
|
311
300
|
if (typeof preserveBufferSharing !== "boolean") {
|
|
312
301
|
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
313
302
|
preserveBufferSharing = false;
|
|
@@ -316,15 +305,14 @@ function resolveOptions(options) {
|
|
|
316
305
|
console.warn("Invalid preserveDescriptors option. Fallback: false.");
|
|
317
306
|
preserveDescriptors = false;
|
|
318
307
|
}
|
|
319
|
-
if (typeof
|
|
320
|
-
console.warn("Invalid
|
|
321
|
-
|
|
308
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
309
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
310
|
+
preserveSymbolKeys = false;
|
|
322
311
|
}
|
|
323
312
|
return {
|
|
324
313
|
preserveBufferSharing,
|
|
325
314
|
preserveDescriptors,
|
|
326
|
-
preserveSymbolKeys
|
|
327
|
-
strictDescriptors
|
|
315
|
+
preserveSymbolKeys
|
|
328
316
|
};
|
|
329
317
|
}
|
|
330
318
|
|