bunshin-clone 1.2.13 → 1.2.15
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 +4 -4
- package/dist/index.cjs +27 -14
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +27 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,11 +13,11 @@ npm i bunshin-clone
|
|
|
13
13
|
import { bunshinClone } from 'bunshin-clone';
|
|
14
14
|
|
|
15
15
|
// CDNs
|
|
16
|
-
import { bunshinClone } from 'https://esm.sh/bunshin-clone@1.2.
|
|
16
|
+
import { bunshinClone } from 'https://esm.sh/bunshin-clone@1.2.15';
|
|
17
17
|
// or
|
|
18
|
-
import { bunshinClone } from 'https://cdn.jsdelivr.net/npm/bunshin-clone@1.2.
|
|
18
|
+
import { bunshinClone } from 'https://cdn.jsdelivr.net/npm/bunshin-clone@1.2.15/+esm';
|
|
19
19
|
// or
|
|
20
|
-
import { bunshinClone } from 'https://esm.unpkg.com/bunshin-clone@1.2.
|
|
20
|
+
import { bunshinClone } from 'https://esm.unpkg.com/bunshin-clone@1.2.15';
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
## 📦 APIs
|
|
@@ -79,7 +79,7 @@ bunshin-clone correctly handles:
|
|
|
79
79
|
- ArrayBuffer
|
|
80
80
|
- DataView
|
|
81
81
|
- TypedArray (Uint8Array, etc.)
|
|
82
|
-
-
|
|
82
|
+
- DOMException / Error
|
|
83
83
|
- Blob
|
|
84
84
|
- ImageData
|
|
85
85
|
- URL
|
package/dist/index.cjs
CHANGED
|
@@ -14,14 +14,15 @@ function clone(node, options, refs) {
|
|
|
14
14
|
if (ref !== void 0) {
|
|
15
15
|
return ref;
|
|
16
16
|
}
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
const settings = resolveOptions(options);
|
|
18
|
+
if (settings.preserveDescriptors && isPlainObject(node)) {
|
|
19
|
+
return cloneWithDescriptors(node, settings, refs);
|
|
19
20
|
}
|
|
20
21
|
if (Array.isArray(node)) {
|
|
21
22
|
const result = [];
|
|
22
23
|
refs.set(node, result);
|
|
23
24
|
for (let i = 0, l = node.length; i < l; i++) {
|
|
24
|
-
result[i] = clone(node[i],
|
|
25
|
+
result[i] = clone(node[i], settings, refs);
|
|
25
26
|
}
|
|
26
27
|
return result;
|
|
27
28
|
}
|
|
@@ -32,7 +33,7 @@ function clone(node, options, refs) {
|
|
|
32
33
|
if (!HAS_OWN.call(node, key) || isUnsafeKey(key)) {
|
|
33
34
|
continue;
|
|
34
35
|
}
|
|
35
|
-
result[key] = clone(node[key],
|
|
36
|
+
result[key] = clone(node[key], settings, refs);
|
|
36
37
|
}
|
|
37
38
|
return result;
|
|
38
39
|
}
|
|
@@ -40,7 +41,7 @@ function clone(node, options, refs) {
|
|
|
40
41
|
const result = /* @__PURE__ */ new Map();
|
|
41
42
|
refs.set(node, result);
|
|
42
43
|
for (const [key, value] of node) {
|
|
43
|
-
result.set(clone(key,
|
|
44
|
+
result.set(clone(key, settings, refs), clone(value, settings, refs));
|
|
44
45
|
}
|
|
45
46
|
return result;
|
|
46
47
|
}
|
|
@@ -48,7 +49,7 @@ function clone(node, options, refs) {
|
|
|
48
49
|
const result = /* @__PURE__ */ new Set();
|
|
49
50
|
refs.set(node, result);
|
|
50
51
|
for (const item of node) {
|
|
51
|
-
result.add(clone(item,
|
|
52
|
+
result.add(clone(item, settings, refs));
|
|
52
53
|
}
|
|
53
54
|
return result;
|
|
54
55
|
}
|
|
@@ -81,7 +82,7 @@ function clone(node, options, refs) {
|
|
|
81
82
|
return result;
|
|
82
83
|
}
|
|
83
84
|
if (node instanceof DOMException || node instanceof Error) {
|
|
84
|
-
return cloneError(node,
|
|
85
|
+
return cloneError(node, settings, refs);
|
|
85
86
|
}
|
|
86
87
|
if (node instanceof Blob) {
|
|
87
88
|
const result = node.slice(0, node.size, node.type);
|
|
@@ -112,7 +113,7 @@ function clone(node, options, refs) {
|
|
|
112
113
|
refs.set(node, node);
|
|
113
114
|
return node;
|
|
114
115
|
}
|
|
115
|
-
function cloneError(value,
|
|
116
|
+
function cloneError(value, settings, refs) {
|
|
116
117
|
const { name, message, cause, stack } = value;
|
|
117
118
|
if (value instanceof DOMException) {
|
|
118
119
|
const result2 = new DOMException(message, name);
|
|
@@ -151,14 +152,14 @@ function cloneError(value, options, refs) {
|
|
|
151
152
|
}
|
|
152
153
|
}
|
|
153
154
|
if ("cause" in value && cause !== void 0) {
|
|
154
|
-
result.cause = clone(cause,
|
|
155
|
+
result.cause = clone(cause, settings, refs);
|
|
155
156
|
}
|
|
156
157
|
for (const key of Object.keys(value)) {
|
|
157
|
-
Reflect.set(result, key, clone(Reflect.get(value, key),
|
|
158
|
+
Reflect.set(result, key, clone(Reflect.get(value, key), settings, refs));
|
|
158
159
|
}
|
|
159
160
|
return result;
|
|
160
161
|
}
|
|
161
|
-
function cloneWithDescriptors(node,
|
|
162
|
+
function cloneWithDescriptors(node, settings, refs) {
|
|
162
163
|
const result = Object.create(Object.getPrototypeOf(node));
|
|
163
164
|
refs.set(node, result);
|
|
164
165
|
const descs = Object.getOwnPropertyDescriptors(node);
|
|
@@ -168,12 +169,12 @@ function cloneWithDescriptors(node, options, refs) {
|
|
|
168
169
|
}
|
|
169
170
|
const desc = { ...descs[key] };
|
|
170
171
|
if ("value" in desc) {
|
|
171
|
-
desc.value = clone(desc.value,
|
|
172
|
+
desc.value = clone(desc.value, settings, refs);
|
|
172
173
|
}
|
|
173
174
|
try {
|
|
174
175
|
Object.defineProperty(result, key, desc);
|
|
175
176
|
} catch (error) {
|
|
176
|
-
if (
|
|
177
|
+
if (settings.strictDescriptors) {
|
|
177
178
|
throw error;
|
|
178
179
|
}
|
|
179
180
|
}
|
|
@@ -201,12 +202,24 @@ function isPlainObject(value) {
|
|
|
201
202
|
function isUnsafeKey(key) {
|
|
202
203
|
return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
203
204
|
}
|
|
205
|
+
function resolveOptions(options) {
|
|
206
|
+
let { preserveDescriptors = false, strictDescriptors = false } = options;
|
|
207
|
+
if (typeof preserveDescriptors !== "boolean") {
|
|
208
|
+
console.warn("Invalid preserveDescriptors option. Fallback: false.");
|
|
209
|
+
preserveDescriptors = false;
|
|
210
|
+
}
|
|
211
|
+
if (typeof strictDescriptors !== "boolean") {
|
|
212
|
+
console.warn("Invalid strictDescriptors option. Fallback: false.");
|
|
213
|
+
strictDescriptors = false;
|
|
214
|
+
}
|
|
215
|
+
return { preserveDescriptors, strictDescriptors };
|
|
216
|
+
}
|
|
204
217
|
/**
|
|
205
218
|
* Bunshin Clone
|
|
206
219
|
* High-performance deep clone utility with descriptor support.
|
|
207
220
|
* Handles circular ref and complex built-in types.
|
|
208
221
|
*
|
|
209
|
-
* @version 1.2.
|
|
222
|
+
* @version 1.2.15
|
|
210
223
|
* @author Yusuke Kamiyamane
|
|
211
224
|
* @license MIT
|
|
212
225
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -12,14 +12,15 @@ function clone(node, options, refs) {
|
|
|
12
12
|
if (ref !== void 0) {
|
|
13
13
|
return ref;
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
const settings = resolveOptions(options);
|
|
16
|
+
if (settings.preserveDescriptors && isPlainObject(node)) {
|
|
17
|
+
return cloneWithDescriptors(node, settings, refs);
|
|
17
18
|
}
|
|
18
19
|
if (Array.isArray(node)) {
|
|
19
20
|
const result = [];
|
|
20
21
|
refs.set(node, result);
|
|
21
22
|
for (let i = 0, l = node.length; i < l; i++) {
|
|
22
|
-
result[i] = clone(node[i],
|
|
23
|
+
result[i] = clone(node[i], settings, refs);
|
|
23
24
|
}
|
|
24
25
|
return result;
|
|
25
26
|
}
|
|
@@ -30,7 +31,7 @@ function clone(node, options, refs) {
|
|
|
30
31
|
if (!HAS_OWN.call(node, key) || isUnsafeKey(key)) {
|
|
31
32
|
continue;
|
|
32
33
|
}
|
|
33
|
-
result[key] = clone(node[key],
|
|
34
|
+
result[key] = clone(node[key], settings, refs);
|
|
34
35
|
}
|
|
35
36
|
return result;
|
|
36
37
|
}
|
|
@@ -38,7 +39,7 @@ function clone(node, options, refs) {
|
|
|
38
39
|
const result = /* @__PURE__ */ new Map();
|
|
39
40
|
refs.set(node, result);
|
|
40
41
|
for (const [key, value] of node) {
|
|
41
|
-
result.set(clone(key,
|
|
42
|
+
result.set(clone(key, settings, refs), clone(value, settings, refs));
|
|
42
43
|
}
|
|
43
44
|
return result;
|
|
44
45
|
}
|
|
@@ -46,7 +47,7 @@ function clone(node, options, refs) {
|
|
|
46
47
|
const result = /* @__PURE__ */ new Set();
|
|
47
48
|
refs.set(node, result);
|
|
48
49
|
for (const item of node) {
|
|
49
|
-
result.add(clone(item,
|
|
50
|
+
result.add(clone(item, settings, refs));
|
|
50
51
|
}
|
|
51
52
|
return result;
|
|
52
53
|
}
|
|
@@ -79,7 +80,7 @@ function clone(node, options, refs) {
|
|
|
79
80
|
return result;
|
|
80
81
|
}
|
|
81
82
|
if (node instanceof DOMException || node instanceof Error) {
|
|
82
|
-
return cloneError(node,
|
|
83
|
+
return cloneError(node, settings, refs);
|
|
83
84
|
}
|
|
84
85
|
if (node instanceof Blob) {
|
|
85
86
|
const result = node.slice(0, node.size, node.type);
|
|
@@ -110,7 +111,7 @@ function clone(node, options, refs) {
|
|
|
110
111
|
refs.set(node, node);
|
|
111
112
|
return node;
|
|
112
113
|
}
|
|
113
|
-
function cloneError(value,
|
|
114
|
+
function cloneError(value, settings, refs) {
|
|
114
115
|
const { name, message, cause, stack } = value;
|
|
115
116
|
if (value instanceof DOMException) {
|
|
116
117
|
const result2 = new DOMException(message, name);
|
|
@@ -149,14 +150,14 @@ function cloneError(value, options, refs) {
|
|
|
149
150
|
}
|
|
150
151
|
}
|
|
151
152
|
if ("cause" in value && cause !== void 0) {
|
|
152
|
-
result.cause = clone(cause,
|
|
153
|
+
result.cause = clone(cause, settings, refs);
|
|
153
154
|
}
|
|
154
155
|
for (const key of Object.keys(value)) {
|
|
155
|
-
Reflect.set(result, key, clone(Reflect.get(value, key),
|
|
156
|
+
Reflect.set(result, key, clone(Reflect.get(value, key), settings, refs));
|
|
156
157
|
}
|
|
157
158
|
return result;
|
|
158
159
|
}
|
|
159
|
-
function cloneWithDescriptors(node,
|
|
160
|
+
function cloneWithDescriptors(node, settings, refs) {
|
|
160
161
|
const result = Object.create(Object.getPrototypeOf(node));
|
|
161
162
|
refs.set(node, result);
|
|
162
163
|
const descs = Object.getOwnPropertyDescriptors(node);
|
|
@@ -166,12 +167,12 @@ function cloneWithDescriptors(node, options, refs) {
|
|
|
166
167
|
}
|
|
167
168
|
const desc = { ...descs[key] };
|
|
168
169
|
if ("value" in desc) {
|
|
169
|
-
desc.value = clone(desc.value,
|
|
170
|
+
desc.value = clone(desc.value, settings, refs);
|
|
170
171
|
}
|
|
171
172
|
try {
|
|
172
173
|
Object.defineProperty(result, key, desc);
|
|
173
174
|
} catch (error) {
|
|
174
|
-
if (
|
|
175
|
+
if (settings.strictDescriptors) {
|
|
175
176
|
throw error;
|
|
176
177
|
}
|
|
177
178
|
}
|
|
@@ -199,12 +200,24 @@ function isPlainObject(value) {
|
|
|
199
200
|
function isUnsafeKey(key) {
|
|
200
201
|
return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
201
202
|
}
|
|
203
|
+
function resolveOptions(options) {
|
|
204
|
+
let { preserveDescriptors = false, strictDescriptors = false } = options;
|
|
205
|
+
if (typeof preserveDescriptors !== "boolean") {
|
|
206
|
+
console.warn("Invalid preserveDescriptors option. Fallback: false.");
|
|
207
|
+
preserveDescriptors = false;
|
|
208
|
+
}
|
|
209
|
+
if (typeof strictDescriptors !== "boolean") {
|
|
210
|
+
console.warn("Invalid strictDescriptors option. Fallback: false.");
|
|
211
|
+
strictDescriptors = false;
|
|
212
|
+
}
|
|
213
|
+
return { preserveDescriptors, strictDescriptors };
|
|
214
|
+
}
|
|
202
215
|
/**
|
|
203
216
|
* Bunshin Clone
|
|
204
217
|
* High-performance deep clone utility with descriptor support.
|
|
205
218
|
* Handles circular ref and complex built-in types.
|
|
206
219
|
*
|
|
207
|
-
* @version 1.2.
|
|
220
|
+
* @version 1.2.15
|
|
208
221
|
* @author Yusuke Kamiyamane
|
|
209
222
|
* @license MIT
|
|
210
223
|
* @copyright Copyright (c) Yusuke Kamiyamane
|