bunshin-clone 1.7.2 → 1.8.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/README.md +6 -1
- package/dist/index.cjs +26 -7
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +26 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,6 +36,7 @@ bunshinClone<T>(value, options);
|
|
|
36
36
|
interface BunshinCloneOptions {
|
|
37
37
|
preserveBufferSharing: boolean; // default: false
|
|
38
38
|
preserveDescriptors: boolean; // default: false
|
|
39
|
+
preserveSymbolKeys: boolean; // default: false
|
|
39
40
|
strictDescriptors: boolean; // default: false
|
|
40
41
|
}
|
|
41
42
|
```
|
|
@@ -48,9 +49,13 @@ If `true`, preserves shared backing buffers between TypedArray and DataView inst
|
|
|
48
49
|
|
|
49
50
|
If `true`, preserves property descriptors (getters/setters, etc.).
|
|
50
51
|
|
|
52
|
+
### `preserveSymbolKeys`
|
|
53
|
+
|
|
54
|
+
If `true`, preserves symbol keys (slower).
|
|
55
|
+
|
|
51
56
|
### `strictDescriptors`
|
|
52
57
|
|
|
53
|
-
If `true`, throws if descriptor cannot be merged (e.g. non-configurable or non-writable)
|
|
58
|
+
If `true`, throws if descriptor cannot be merged (e.g. non-configurable or non-writable).
|
|
54
59
|
|
|
55
60
|
## 📖 Details
|
|
56
61
|
|
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
4
|
var EMPTY_OPTIONS = {};
|
|
5
|
-
var { propertyIsEnumerable: IS_ENUMERABLE } = Object.prototype;
|
|
5
|
+
var { hasOwnProperty: HAS_OWN, propertyIsEnumerable: IS_ENUMERABLE } = Object.prototype;
|
|
6
6
|
function bunshinClone(value, options = EMPTY_OPTIONS, refs = /* @__PURE__ */ new WeakMap()) {
|
|
7
7
|
return clone(value, resolveOptions(options), refs);
|
|
8
8
|
}
|
|
@@ -34,11 +34,20 @@ function clone(value, settings, refs) {
|
|
|
34
34
|
if (isPlainObject(value)) {
|
|
35
35
|
const result = Object.create(Object.getPrototypeOf(value));
|
|
36
36
|
refs.set(value, result);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
if (!settings.preserveSymbolKeys) {
|
|
38
|
+
for (const key in value) {
|
|
39
|
+
if (isUnsafeKey(key) || !HAS_OWN.call(value, key)) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
result[key] = clone(value[key], settings, refs);
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
46
|
+
if (isUnsafeKey(key) || !IS_ENUMERABLE.call(value, key)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
result[key] = clone(value[key], settings, refs);
|
|
40
50
|
}
|
|
41
|
-
result[key] = clone(value[key], settings, refs);
|
|
42
51
|
}
|
|
43
52
|
return result;
|
|
44
53
|
}
|
|
@@ -242,8 +251,13 @@ function resolveOptions(options) {
|
|
|
242
251
|
let {
|
|
243
252
|
preserveBufferSharing = false,
|
|
244
253
|
preserveDescriptors = false,
|
|
245
|
-
strictDescriptors = false
|
|
254
|
+
strictDescriptors = false,
|
|
255
|
+
preserveSymbolKeys = false
|
|
246
256
|
} = options;
|
|
257
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
258
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
259
|
+
preserveSymbolKeys = false;
|
|
260
|
+
}
|
|
247
261
|
if (typeof preserveBufferSharing !== "boolean") {
|
|
248
262
|
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
249
263
|
preserveBufferSharing = false;
|
|
@@ -256,7 +270,12 @@ function resolveOptions(options) {
|
|
|
256
270
|
console.warn("Invalid strictDescriptors option. Fallback: false.");
|
|
257
271
|
strictDescriptors = false;
|
|
258
272
|
}
|
|
259
|
-
return {
|
|
273
|
+
return {
|
|
274
|
+
preserveBufferSharing,
|
|
275
|
+
preserveDescriptors,
|
|
276
|
+
preserveSymbolKeys,
|
|
277
|
+
strictDescriptors
|
|
278
|
+
};
|
|
260
279
|
}
|
|
261
280
|
|
|
262
281
|
exports.bunshinClone = bunshinClone;
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
var EMPTY_OPTIONS = {};
|
|
3
|
-
var { propertyIsEnumerable: IS_ENUMERABLE } = Object.prototype;
|
|
3
|
+
var { hasOwnProperty: HAS_OWN, propertyIsEnumerable: IS_ENUMERABLE } = Object.prototype;
|
|
4
4
|
function bunshinClone(value, options = EMPTY_OPTIONS, refs = /* @__PURE__ */ new WeakMap()) {
|
|
5
5
|
return clone(value, resolveOptions(options), refs);
|
|
6
6
|
}
|
|
@@ -32,11 +32,20 @@ function clone(value, settings, refs) {
|
|
|
32
32
|
if (isPlainObject(value)) {
|
|
33
33
|
const result = Object.create(Object.getPrototypeOf(value));
|
|
34
34
|
refs.set(value, result);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
if (!settings.preserveSymbolKeys) {
|
|
36
|
+
for (const key in value) {
|
|
37
|
+
if (isUnsafeKey(key) || !HAS_OWN.call(value, key)) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
result[key] = clone(value[key], settings, refs);
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
44
|
+
if (isUnsafeKey(key) || !IS_ENUMERABLE.call(value, key)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
result[key] = clone(value[key], settings, refs);
|
|
38
48
|
}
|
|
39
|
-
result[key] = clone(value[key], settings, refs);
|
|
40
49
|
}
|
|
41
50
|
return result;
|
|
42
51
|
}
|
|
@@ -240,8 +249,13 @@ function resolveOptions(options) {
|
|
|
240
249
|
let {
|
|
241
250
|
preserveBufferSharing = false,
|
|
242
251
|
preserveDescriptors = false,
|
|
243
|
-
strictDescriptors = false
|
|
252
|
+
strictDescriptors = false,
|
|
253
|
+
preserveSymbolKeys = false
|
|
244
254
|
} = options;
|
|
255
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
256
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
257
|
+
preserveSymbolKeys = false;
|
|
258
|
+
}
|
|
245
259
|
if (typeof preserveBufferSharing !== "boolean") {
|
|
246
260
|
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
247
261
|
preserveBufferSharing = false;
|
|
@@ -254,7 +268,12 @@ function resolveOptions(options) {
|
|
|
254
268
|
console.warn("Invalid strictDescriptors option. Fallback: false.");
|
|
255
269
|
strictDescriptors = false;
|
|
256
270
|
}
|
|
257
|
-
return {
|
|
271
|
+
return {
|
|
272
|
+
preserveBufferSharing,
|
|
273
|
+
preserveDescriptors,
|
|
274
|
+
preserveSymbolKeys,
|
|
275
|
+
strictDescriptors
|
|
276
|
+
};
|
|
258
277
|
}
|
|
259
278
|
|
|
260
279
|
export { bunshinClone, forEachOwnKey, isObject, isPlainObject, isShallowArray, isUnsafeKey };
|