bunshin-clone 1.7.2 → 1.8.1
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 +30 -20
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +30 -20
- package/package.json +4 -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
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
-
|
|
5
|
-
var { propertyIsEnumerable: IS_ENUMERABLE } = Object.prototype;
|
|
6
|
-
function bunshinClone(value, options = EMPTY_OPTIONS, refs = /* @__PURE__ */ new WeakMap()) {
|
|
4
|
+
function bunshinClone(value, options = {}, refs = /* @__PURE__ */ new WeakMap()) {
|
|
7
5
|
return clone(value, resolveOptions(options), refs);
|
|
8
6
|
}
|
|
9
7
|
function clone(value, settings, refs) {
|
|
10
8
|
if (!isObject(value)) {
|
|
11
9
|
return value;
|
|
12
10
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return ref;
|
|
11
|
+
if (refs.has(value)) {
|
|
12
|
+
return refs.get(value);
|
|
16
13
|
}
|
|
17
14
|
if (settings.preserveDescriptors && isPlainObject(value)) {
|
|
18
15
|
return cloneWithDescriptors(value, settings, refs);
|
|
@@ -34,11 +31,20 @@ function clone(value, settings, refs) {
|
|
|
34
31
|
if (isPlainObject(value)) {
|
|
35
32
|
const result = Object.create(Object.getPrototypeOf(value));
|
|
36
33
|
refs.set(value, result);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
if (!settings.preserveSymbolKeys) {
|
|
35
|
+
for (const key in value) {
|
|
36
|
+
if (isUnsafeKey(key) || !Object.hasOwn(value, key)) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
result[key] = clone(value[key], settings, refs);
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
43
|
+
if (isUnsafeKey(key) || !Object.propertyIsEnumerable.call(value, key)) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
result[key] = clone(value[key], settings, refs);
|
|
40
47
|
}
|
|
41
|
-
result[key] = clone(value[key], settings, refs);
|
|
42
48
|
}
|
|
43
49
|
return result;
|
|
44
50
|
}
|
|
@@ -131,8 +137,7 @@ function clone(value, settings, refs) {
|
|
|
131
137
|
return value;
|
|
132
138
|
}
|
|
133
139
|
function cloneBuffer(buffer, refs) {
|
|
134
|
-
|
|
135
|
-
if (ref !== void 0) {
|
|
140
|
+
if (refs.has(buffer)) {
|
|
136
141
|
return refs.get(buffer);
|
|
137
142
|
}
|
|
138
143
|
const result = buffer.slice(0);
|
|
@@ -228,12 +233,7 @@ function isPlainObject(value) {
|
|
|
228
233
|
return proto === Object.prototype || proto === null;
|
|
229
234
|
}
|
|
230
235
|
function isShallowArray(array) {
|
|
231
|
-
|
|
232
|
-
if (isObject(array[i])) {
|
|
233
|
-
return false;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return true;
|
|
236
|
+
return array.every((item) => !isObject(item));
|
|
237
237
|
}
|
|
238
238
|
function isUnsafeKey(key) {
|
|
239
239
|
return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
@@ -242,8 +242,13 @@ function resolveOptions(options) {
|
|
|
242
242
|
let {
|
|
243
243
|
preserveBufferSharing = false,
|
|
244
244
|
preserveDescriptors = false,
|
|
245
|
-
strictDescriptors = false
|
|
245
|
+
strictDescriptors = false,
|
|
246
|
+
preserveSymbolKeys = false
|
|
246
247
|
} = options;
|
|
248
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
249
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
250
|
+
preserveSymbolKeys = false;
|
|
251
|
+
}
|
|
247
252
|
if (typeof preserveBufferSharing !== "boolean") {
|
|
248
253
|
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
249
254
|
preserveBufferSharing = false;
|
|
@@ -256,7 +261,12 @@ function resolveOptions(options) {
|
|
|
256
261
|
console.warn("Invalid strictDescriptors option. Fallback: false.");
|
|
257
262
|
strictDescriptors = false;
|
|
258
263
|
}
|
|
259
|
-
return {
|
|
264
|
+
return {
|
|
265
|
+
preserveBufferSharing,
|
|
266
|
+
preserveDescriptors,
|
|
267
|
+
preserveSymbolKeys,
|
|
268
|
+
strictDescriptors
|
|
269
|
+
};
|
|
260
270
|
}
|
|
261
271
|
|
|
262
272
|
exports.bunshinClone = bunshinClone;
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
|
|
3
|
-
var { propertyIsEnumerable: IS_ENUMERABLE } = Object.prototype;
|
|
4
|
-
function bunshinClone(value, options = EMPTY_OPTIONS, refs = /* @__PURE__ */ new WeakMap()) {
|
|
2
|
+
function bunshinClone(value, options = {}, refs = /* @__PURE__ */ new WeakMap()) {
|
|
5
3
|
return clone(value, resolveOptions(options), refs);
|
|
6
4
|
}
|
|
7
5
|
function clone(value, settings, refs) {
|
|
8
6
|
if (!isObject(value)) {
|
|
9
7
|
return value;
|
|
10
8
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return ref;
|
|
9
|
+
if (refs.has(value)) {
|
|
10
|
+
return refs.get(value);
|
|
14
11
|
}
|
|
15
12
|
if (settings.preserveDescriptors && isPlainObject(value)) {
|
|
16
13
|
return cloneWithDescriptors(value, settings, refs);
|
|
@@ -32,11 +29,20 @@ function clone(value, settings, refs) {
|
|
|
32
29
|
if (isPlainObject(value)) {
|
|
33
30
|
const result = Object.create(Object.getPrototypeOf(value));
|
|
34
31
|
refs.set(value, result);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
if (!settings.preserveSymbolKeys) {
|
|
33
|
+
for (const key in value) {
|
|
34
|
+
if (isUnsafeKey(key) || !Object.hasOwn(value, key)) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
result[key] = clone(value[key], settings, refs);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
41
|
+
if (isUnsafeKey(key) || !Object.propertyIsEnumerable.call(value, key)) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
result[key] = clone(value[key], settings, refs);
|
|
38
45
|
}
|
|
39
|
-
result[key] = clone(value[key], settings, refs);
|
|
40
46
|
}
|
|
41
47
|
return result;
|
|
42
48
|
}
|
|
@@ -129,8 +135,7 @@ function clone(value, settings, refs) {
|
|
|
129
135
|
return value;
|
|
130
136
|
}
|
|
131
137
|
function cloneBuffer(buffer, refs) {
|
|
132
|
-
|
|
133
|
-
if (ref !== void 0) {
|
|
138
|
+
if (refs.has(buffer)) {
|
|
134
139
|
return refs.get(buffer);
|
|
135
140
|
}
|
|
136
141
|
const result = buffer.slice(0);
|
|
@@ -226,12 +231,7 @@ function isPlainObject(value) {
|
|
|
226
231
|
return proto === Object.prototype || proto === null;
|
|
227
232
|
}
|
|
228
233
|
function isShallowArray(array) {
|
|
229
|
-
|
|
230
|
-
if (isObject(array[i])) {
|
|
231
|
-
return false;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
return true;
|
|
234
|
+
return array.every((item) => !isObject(item));
|
|
235
235
|
}
|
|
236
236
|
function isUnsafeKey(key) {
|
|
237
237
|
return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
@@ -240,8 +240,13 @@ function resolveOptions(options) {
|
|
|
240
240
|
let {
|
|
241
241
|
preserveBufferSharing = false,
|
|
242
242
|
preserveDescriptors = false,
|
|
243
|
-
strictDescriptors = false
|
|
243
|
+
strictDescriptors = false,
|
|
244
|
+
preserveSymbolKeys = false
|
|
244
245
|
} = options;
|
|
246
|
+
if (typeof preserveSymbolKeys !== "boolean") {
|
|
247
|
+
console.warn("Invalid preserveSymbolKeys option. Fallback: false.");
|
|
248
|
+
preserveSymbolKeys = false;
|
|
249
|
+
}
|
|
245
250
|
if (typeof preserveBufferSharing !== "boolean") {
|
|
246
251
|
console.warn("Invalid preserveBufferSharing option. Fallback: false.");
|
|
247
252
|
preserveBufferSharing = false;
|
|
@@ -254,7 +259,12 @@ function resolveOptions(options) {
|
|
|
254
259
|
console.warn("Invalid strictDescriptors option. Fallback: false.");
|
|
255
260
|
strictDescriptors = false;
|
|
256
261
|
}
|
|
257
|
-
return {
|
|
262
|
+
return {
|
|
263
|
+
preserveBufferSharing,
|
|
264
|
+
preserveDescriptors,
|
|
265
|
+
preserveSymbolKeys,
|
|
266
|
+
strictDescriptors
|
|
267
|
+
};
|
|
258
268
|
}
|
|
259
269
|
|
|
260
270
|
export { bunshinClone, forEachOwnKey, isObject, isPlainObject, isShallowArray, isUnsafeKey };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bunshin-clone",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "High-performance deep clone utility with descriptor support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"array",
|
|
@@ -73,5 +73,8 @@
|
|
|
73
73
|
},
|
|
74
74
|
"allowScripts": {
|
|
75
75
|
"esbuild": true
|
|
76
|
+
},
|
|
77
|
+
"dependencies": {
|
|
78
|
+
"bunshin-clone-npm": "npm:bunshin-clone@^1.8.0"
|
|
76
79
|
}
|
|
77
80
|
}
|