bunshin-clone 1.0.12 → 1.1.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/dist/index.cjs +38 -41
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +38 -41
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -6,89 +6,86 @@ var { hasOwnProperty: HAS_OWN } = Object.prototype;
|
|
|
6
6
|
function bunshinClone(source, options) {
|
|
7
7
|
return clone(source, options ?? EMPTY_OPTIONS, /* @__PURE__ */ new WeakMap());
|
|
8
8
|
}
|
|
9
|
-
function clone(node, options,
|
|
9
|
+
function clone(node, options, refs) {
|
|
10
10
|
if (!isObject(node)) {
|
|
11
11
|
return node;
|
|
12
12
|
}
|
|
13
|
-
const
|
|
14
|
-
if (
|
|
15
|
-
return
|
|
13
|
+
const ref = refs.get(node);
|
|
14
|
+
if (ref !== void 0) {
|
|
15
|
+
return ref;
|
|
16
16
|
}
|
|
17
17
|
if (options.preserveDescriptors && isPlainObject(node)) {
|
|
18
|
-
return cloneWithDescriptors(node, options,
|
|
18
|
+
return cloneWithDescriptors(node, options, refs);
|
|
19
19
|
}
|
|
20
20
|
if (Array.isArray(node)) {
|
|
21
21
|
const result = [];
|
|
22
|
-
|
|
22
|
+
refs.set(node, result);
|
|
23
23
|
for (let i = 0, l = node.length; i < l; i++) {
|
|
24
|
-
result[i] = clone(node[i], options,
|
|
24
|
+
result[i] = clone(node[i], options, refs);
|
|
25
25
|
}
|
|
26
26
|
return result;
|
|
27
27
|
}
|
|
28
28
|
if (isPlainObject(node)) {
|
|
29
29
|
const result = Object.create(Object.getPrototypeOf(node));
|
|
30
|
-
|
|
30
|
+
refs.set(node, result);
|
|
31
31
|
for (const key in node) {
|
|
32
32
|
if (!HAS_OWN.call(node, key) || isUnsafeKey(key)) {
|
|
33
33
|
continue;
|
|
34
34
|
}
|
|
35
|
-
result[key] = clone(node[key], options,
|
|
35
|
+
result[key] = clone(node[key], options, refs);
|
|
36
36
|
}
|
|
37
37
|
return result;
|
|
38
38
|
}
|
|
39
39
|
if (node instanceof Map) {
|
|
40
40
|
const result = /* @__PURE__ */ new Map();
|
|
41
|
-
|
|
41
|
+
refs.set(node, result);
|
|
42
42
|
for (const [key, value] of node) {
|
|
43
|
-
result.set(clone(key, options,
|
|
43
|
+
result.set(clone(key, options, refs), clone(value, options, refs));
|
|
44
44
|
}
|
|
45
45
|
return result;
|
|
46
46
|
}
|
|
47
47
|
if (node instanceof Set) {
|
|
48
48
|
const result = /* @__PURE__ */ new Set();
|
|
49
|
-
|
|
49
|
+
refs.set(node, result);
|
|
50
50
|
for (const item of node) {
|
|
51
|
-
result.add(clone(item, options,
|
|
51
|
+
result.add(clone(item, options, refs));
|
|
52
52
|
}
|
|
53
53
|
return result;
|
|
54
54
|
}
|
|
55
55
|
if (node instanceof Date) {
|
|
56
56
|
const result = new Date(node.getTime());
|
|
57
|
-
|
|
57
|
+
refs.set(node, result);
|
|
58
58
|
return result;
|
|
59
59
|
}
|
|
60
60
|
if (node instanceof RegExp) {
|
|
61
61
|
const result = new RegExp(node.source, node.flags);
|
|
62
|
-
|
|
62
|
+
refs.set(node, result);
|
|
63
63
|
result.lastIndex = node.lastIndex;
|
|
64
64
|
return result;
|
|
65
65
|
}
|
|
66
66
|
if (node instanceof ArrayBuffer) {
|
|
67
67
|
const result = node.slice(0);
|
|
68
|
-
|
|
68
|
+
refs.set(node, result);
|
|
69
69
|
return result;
|
|
70
70
|
}
|
|
71
71
|
if (ArrayBuffer.isView(node)) {
|
|
72
72
|
const { buffer, byteOffset, byteLength } = node;
|
|
73
73
|
if (node instanceof DataView) {
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
return
|
|
77
|
-
} else {
|
|
78
|
-
const Ctor = node.constructor;
|
|
79
|
-
const result = new Ctor(
|
|
80
|
-
buffer.slice(byteOffset, byteOffset + byteLength)
|
|
81
|
-
);
|
|
82
|
-
ref.set(node, result);
|
|
83
|
-
return result;
|
|
74
|
+
const result2 = new DataView(buffer.slice(0), byteOffset, byteLength);
|
|
75
|
+
refs.set(node, result2);
|
|
76
|
+
return result2;
|
|
84
77
|
}
|
|
78
|
+
const Ctor = node.constructor;
|
|
79
|
+
const result = new Ctor(buffer.slice(byteOffset, byteOffset + byteLength));
|
|
80
|
+
refs.set(node, result);
|
|
81
|
+
return result;
|
|
85
82
|
}
|
|
86
83
|
if (node instanceof Error || typeof DOMException !== "undefined" && node instanceof DOMException) {
|
|
87
|
-
return cloneError(node, options,
|
|
84
|
+
return cloneError(node, options, refs);
|
|
88
85
|
}
|
|
89
86
|
if (typeof Blob !== "undefined" && node instanceof Blob) {
|
|
90
87
|
const result = node.slice(0, node.size, node.type);
|
|
91
|
-
|
|
88
|
+
refs.set(node, result);
|
|
92
89
|
return result;
|
|
93
90
|
}
|
|
94
91
|
if (typeof ImageData !== "undefined" && node instanceof ImageData) {
|
|
@@ -97,29 +94,29 @@ function clone(node, options, ref) {
|
|
|
97
94
|
node.width,
|
|
98
95
|
node.height
|
|
99
96
|
);
|
|
100
|
-
|
|
97
|
+
refs.set(node, result);
|
|
101
98
|
return result;
|
|
102
99
|
}
|
|
103
100
|
if (typeof URL !== "undefined" && node instanceof URL) {
|
|
104
101
|
const result = new URL(node.href);
|
|
105
|
-
|
|
102
|
+
refs.set(node, result);
|
|
106
103
|
return result;
|
|
107
104
|
}
|
|
108
105
|
if (typeof URLSearchParams !== "undefined" && node instanceof URLSearchParams) {
|
|
109
106
|
const result = new URLSearchParams();
|
|
110
|
-
|
|
107
|
+
refs.set(node, result);
|
|
111
108
|
for (const [key, value] of node) {
|
|
112
109
|
result.append(key, value);
|
|
113
110
|
}
|
|
114
111
|
return result;
|
|
115
112
|
}
|
|
116
|
-
|
|
113
|
+
refs.set(node, node);
|
|
117
114
|
return node;
|
|
118
115
|
}
|
|
119
|
-
function cloneError(value, options,
|
|
116
|
+
function cloneError(value, options, refs) {
|
|
120
117
|
if (value instanceof DOMException) {
|
|
121
118
|
const result2 = new DOMException(value.message, value.name);
|
|
122
|
-
|
|
119
|
+
refs.set(value, result2);
|
|
123
120
|
return result2;
|
|
124
121
|
}
|
|
125
122
|
const name = value.name || "Error";
|
|
@@ -148,7 +145,7 @@ function cloneError(value, options, ref) {
|
|
|
148
145
|
result = new Error(message);
|
|
149
146
|
result.name = name;
|
|
150
147
|
}
|
|
151
|
-
|
|
148
|
+
refs.set(value, result);
|
|
152
149
|
if (value.stack) {
|
|
153
150
|
try {
|
|
154
151
|
result.stack = value.stack;
|
|
@@ -156,16 +153,16 @@ function cloneError(value, options, ref) {
|
|
|
156
153
|
}
|
|
157
154
|
}
|
|
158
155
|
if ("cause" in value && value.cause !== void 0) {
|
|
159
|
-
result.cause = clone(value.cause, options,
|
|
156
|
+
result.cause = clone(value.cause, options, refs);
|
|
160
157
|
}
|
|
161
158
|
for (const key of Object.keys(value)) {
|
|
162
|
-
result[key] = clone(value[key], options,
|
|
159
|
+
result[key] = clone(value[key], options, refs);
|
|
163
160
|
}
|
|
164
161
|
return result;
|
|
165
162
|
}
|
|
166
|
-
function cloneWithDescriptors(node, options,
|
|
163
|
+
function cloneWithDescriptors(node, options, refs) {
|
|
167
164
|
const result = Object.create(Object.getPrototypeOf(node));
|
|
168
|
-
|
|
165
|
+
refs.set(node, result);
|
|
169
166
|
const descs = Object.getOwnPropertyDescriptors(node);
|
|
170
167
|
forEachOwnKey(descs, (key) => {
|
|
171
168
|
if (isUnsafeKey(key)) {
|
|
@@ -173,7 +170,7 @@ function cloneWithDescriptors(node, options, ref) {
|
|
|
173
170
|
}
|
|
174
171
|
const desc = { ...descs[key] };
|
|
175
172
|
if ("value" in desc) {
|
|
176
|
-
desc.value = clone(desc.value, options,
|
|
173
|
+
desc.value = clone(desc.value, options, refs);
|
|
177
174
|
}
|
|
178
175
|
try {
|
|
179
176
|
Object.defineProperty(result, key, desc);
|
|
@@ -212,7 +209,7 @@ function isUnsafeKey(key) {
|
|
|
212
209
|
* High-performance deep clone utility with descriptor support.
|
|
213
210
|
* Handles circular ref and complex built-in types.
|
|
214
211
|
*
|
|
215
|
-
* @version 1.0
|
|
212
|
+
* @version 1.1.0
|
|
216
213
|
* @author Yusuke Kamiyamane
|
|
217
214
|
* @license MIT
|
|
218
215
|
* @copyright Copyright (c) 2026 Yusuke Kamiyamane
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -4,89 +4,86 @@ var { hasOwnProperty: HAS_OWN } = Object.prototype;
|
|
|
4
4
|
function bunshinClone(source, options) {
|
|
5
5
|
return clone(source, options ?? EMPTY_OPTIONS, /* @__PURE__ */ new WeakMap());
|
|
6
6
|
}
|
|
7
|
-
function clone(node, options,
|
|
7
|
+
function clone(node, options, refs) {
|
|
8
8
|
if (!isObject(node)) {
|
|
9
9
|
return node;
|
|
10
10
|
}
|
|
11
|
-
const
|
|
12
|
-
if (
|
|
13
|
-
return
|
|
11
|
+
const ref = refs.get(node);
|
|
12
|
+
if (ref !== void 0) {
|
|
13
|
+
return ref;
|
|
14
14
|
}
|
|
15
15
|
if (options.preserveDescriptors && isPlainObject(node)) {
|
|
16
|
-
return cloneWithDescriptors(node, options,
|
|
16
|
+
return cloneWithDescriptors(node, options, refs);
|
|
17
17
|
}
|
|
18
18
|
if (Array.isArray(node)) {
|
|
19
19
|
const result = [];
|
|
20
|
-
|
|
20
|
+
refs.set(node, result);
|
|
21
21
|
for (let i = 0, l = node.length; i < l; i++) {
|
|
22
|
-
result[i] = clone(node[i], options,
|
|
22
|
+
result[i] = clone(node[i], options, refs);
|
|
23
23
|
}
|
|
24
24
|
return result;
|
|
25
25
|
}
|
|
26
26
|
if (isPlainObject(node)) {
|
|
27
27
|
const result = Object.create(Object.getPrototypeOf(node));
|
|
28
|
-
|
|
28
|
+
refs.set(node, result);
|
|
29
29
|
for (const key in node) {
|
|
30
30
|
if (!HAS_OWN.call(node, key) || isUnsafeKey(key)) {
|
|
31
31
|
continue;
|
|
32
32
|
}
|
|
33
|
-
result[key] = clone(node[key], options,
|
|
33
|
+
result[key] = clone(node[key], options, refs);
|
|
34
34
|
}
|
|
35
35
|
return result;
|
|
36
36
|
}
|
|
37
37
|
if (node instanceof Map) {
|
|
38
38
|
const result = /* @__PURE__ */ new Map();
|
|
39
|
-
|
|
39
|
+
refs.set(node, result);
|
|
40
40
|
for (const [key, value] of node) {
|
|
41
|
-
result.set(clone(key, options,
|
|
41
|
+
result.set(clone(key, options, refs), clone(value, options, refs));
|
|
42
42
|
}
|
|
43
43
|
return result;
|
|
44
44
|
}
|
|
45
45
|
if (node instanceof Set) {
|
|
46
46
|
const result = /* @__PURE__ */ new Set();
|
|
47
|
-
|
|
47
|
+
refs.set(node, result);
|
|
48
48
|
for (const item of node) {
|
|
49
|
-
result.add(clone(item, options,
|
|
49
|
+
result.add(clone(item, options, refs));
|
|
50
50
|
}
|
|
51
51
|
return result;
|
|
52
52
|
}
|
|
53
53
|
if (node instanceof Date) {
|
|
54
54
|
const result = new Date(node.getTime());
|
|
55
|
-
|
|
55
|
+
refs.set(node, result);
|
|
56
56
|
return result;
|
|
57
57
|
}
|
|
58
58
|
if (node instanceof RegExp) {
|
|
59
59
|
const result = new RegExp(node.source, node.flags);
|
|
60
|
-
|
|
60
|
+
refs.set(node, result);
|
|
61
61
|
result.lastIndex = node.lastIndex;
|
|
62
62
|
return result;
|
|
63
63
|
}
|
|
64
64
|
if (node instanceof ArrayBuffer) {
|
|
65
65
|
const result = node.slice(0);
|
|
66
|
-
|
|
66
|
+
refs.set(node, result);
|
|
67
67
|
return result;
|
|
68
68
|
}
|
|
69
69
|
if (ArrayBuffer.isView(node)) {
|
|
70
70
|
const { buffer, byteOffset, byteLength } = node;
|
|
71
71
|
if (node instanceof DataView) {
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
return
|
|
75
|
-
} else {
|
|
76
|
-
const Ctor = node.constructor;
|
|
77
|
-
const result = new Ctor(
|
|
78
|
-
buffer.slice(byteOffset, byteOffset + byteLength)
|
|
79
|
-
);
|
|
80
|
-
ref.set(node, result);
|
|
81
|
-
return result;
|
|
72
|
+
const result2 = new DataView(buffer.slice(0), byteOffset, byteLength);
|
|
73
|
+
refs.set(node, result2);
|
|
74
|
+
return result2;
|
|
82
75
|
}
|
|
76
|
+
const Ctor = node.constructor;
|
|
77
|
+
const result = new Ctor(buffer.slice(byteOffset, byteOffset + byteLength));
|
|
78
|
+
refs.set(node, result);
|
|
79
|
+
return result;
|
|
83
80
|
}
|
|
84
81
|
if (node instanceof Error || typeof DOMException !== "undefined" && node instanceof DOMException) {
|
|
85
|
-
return cloneError(node, options,
|
|
82
|
+
return cloneError(node, options, refs);
|
|
86
83
|
}
|
|
87
84
|
if (typeof Blob !== "undefined" && node instanceof Blob) {
|
|
88
85
|
const result = node.slice(0, node.size, node.type);
|
|
89
|
-
|
|
86
|
+
refs.set(node, result);
|
|
90
87
|
return result;
|
|
91
88
|
}
|
|
92
89
|
if (typeof ImageData !== "undefined" && node instanceof ImageData) {
|
|
@@ -95,29 +92,29 @@ function clone(node, options, ref) {
|
|
|
95
92
|
node.width,
|
|
96
93
|
node.height
|
|
97
94
|
);
|
|
98
|
-
|
|
95
|
+
refs.set(node, result);
|
|
99
96
|
return result;
|
|
100
97
|
}
|
|
101
98
|
if (typeof URL !== "undefined" && node instanceof URL) {
|
|
102
99
|
const result = new URL(node.href);
|
|
103
|
-
|
|
100
|
+
refs.set(node, result);
|
|
104
101
|
return result;
|
|
105
102
|
}
|
|
106
103
|
if (typeof URLSearchParams !== "undefined" && node instanceof URLSearchParams) {
|
|
107
104
|
const result = new URLSearchParams();
|
|
108
|
-
|
|
105
|
+
refs.set(node, result);
|
|
109
106
|
for (const [key, value] of node) {
|
|
110
107
|
result.append(key, value);
|
|
111
108
|
}
|
|
112
109
|
return result;
|
|
113
110
|
}
|
|
114
|
-
|
|
111
|
+
refs.set(node, node);
|
|
115
112
|
return node;
|
|
116
113
|
}
|
|
117
|
-
function cloneError(value, options,
|
|
114
|
+
function cloneError(value, options, refs) {
|
|
118
115
|
if (value instanceof DOMException) {
|
|
119
116
|
const result2 = new DOMException(value.message, value.name);
|
|
120
|
-
|
|
117
|
+
refs.set(value, result2);
|
|
121
118
|
return result2;
|
|
122
119
|
}
|
|
123
120
|
const name = value.name || "Error";
|
|
@@ -146,7 +143,7 @@ function cloneError(value, options, ref) {
|
|
|
146
143
|
result = new Error(message);
|
|
147
144
|
result.name = name;
|
|
148
145
|
}
|
|
149
|
-
|
|
146
|
+
refs.set(value, result);
|
|
150
147
|
if (value.stack) {
|
|
151
148
|
try {
|
|
152
149
|
result.stack = value.stack;
|
|
@@ -154,16 +151,16 @@ function cloneError(value, options, ref) {
|
|
|
154
151
|
}
|
|
155
152
|
}
|
|
156
153
|
if ("cause" in value && value.cause !== void 0) {
|
|
157
|
-
result.cause = clone(value.cause, options,
|
|
154
|
+
result.cause = clone(value.cause, options, refs);
|
|
158
155
|
}
|
|
159
156
|
for (const key of Object.keys(value)) {
|
|
160
|
-
result[key] = clone(value[key], options,
|
|
157
|
+
result[key] = clone(value[key], options, refs);
|
|
161
158
|
}
|
|
162
159
|
return result;
|
|
163
160
|
}
|
|
164
|
-
function cloneWithDescriptors(node, options,
|
|
161
|
+
function cloneWithDescriptors(node, options, refs) {
|
|
165
162
|
const result = Object.create(Object.getPrototypeOf(node));
|
|
166
|
-
|
|
163
|
+
refs.set(node, result);
|
|
167
164
|
const descs = Object.getOwnPropertyDescriptors(node);
|
|
168
165
|
forEachOwnKey(descs, (key) => {
|
|
169
166
|
if (isUnsafeKey(key)) {
|
|
@@ -171,7 +168,7 @@ function cloneWithDescriptors(node, options, ref) {
|
|
|
171
168
|
}
|
|
172
169
|
const desc = { ...descs[key] };
|
|
173
170
|
if ("value" in desc) {
|
|
174
|
-
desc.value = clone(desc.value, options,
|
|
171
|
+
desc.value = clone(desc.value, options, refs);
|
|
175
172
|
}
|
|
176
173
|
try {
|
|
177
174
|
Object.defineProperty(result, key, desc);
|
|
@@ -210,7 +207,7 @@ function isUnsafeKey(key) {
|
|
|
210
207
|
* High-performance deep clone utility with descriptor support.
|
|
211
208
|
* Handles circular ref and complex built-in types.
|
|
212
209
|
*
|
|
213
|
-
* @version 1.0
|
|
210
|
+
* @version 1.1.0
|
|
214
211
|
* @author Yusuke Kamiyamane
|
|
215
212
|
* @license MIT
|
|
216
213
|
* @copyright Copyright (c) 2026 Yusuke Kamiyamane
|