bunshin-clone 1.0.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/LICENSE +21 -0
- package/README.md +198 -0
- package/dist/index.cjs +203 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +201 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yusuke Kamiyamane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# bunshin-clone
|
|
2
|
+
|
|
3
|
+
High-performance deep clone utility with descriptor support. Supports circular references and complex built-in types.
|
|
4
|
+
|
|
5
|
+
* ⚡ Fast (no unnecessary overhead)
|
|
6
|
+
* ♻️ Deep clone (no structural sharing)
|
|
7
|
+
* 🔁 Supports circular references
|
|
8
|
+
* 🧠 Handles Map, Set, Array, TypedArray, Date, RegExp, etc.
|
|
9
|
+
* 🧩 Optional descriptor preservation
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import bunshinClone from 'bunshin-clone';
|
|
15
|
+
|
|
16
|
+
const source = { foo: 1, nested: { x: 1 } };
|
|
17
|
+
|
|
18
|
+
const result = bunshinClone(source);
|
|
19
|
+
|
|
20
|
+
console.log(result);
|
|
21
|
+
// { foo: 1, nested: { x: 1 } }
|
|
22
|
+
|
|
23
|
+
console.log(result === source); // false
|
|
24
|
+
console.log(result.nested === source.nested); // false
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
bunshinClone(target)
|
|
31
|
+
bunshinClone(target, options)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Options
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
interface BunshinCloneOptions {
|
|
38
|
+
preserveDescriptors?: boolean;
|
|
39
|
+
strictDescriptors?: boolean;
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### preserveDescriptors
|
|
44
|
+
|
|
45
|
+
* `false` (default): use standard merge (faster, ignores property descriptors)
|
|
46
|
+
* `true`: preserve property descriptors (getters/setters, etc.)
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const source = {};
|
|
50
|
+
Object.defineProperty(source, 'x', {
|
|
51
|
+
get: () => 42,
|
|
52
|
+
enumerable: true,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const result = bunshinClone(source, { preserveDescriptors: true });
|
|
56
|
+
|
|
57
|
+
Object.getOwnPropertyDescriptor(result, 'x')?.get;
|
|
58
|
+
// => function
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### strictDescriptors
|
|
62
|
+
|
|
63
|
+
* `false` (default): skip incompatible descriptors
|
|
64
|
+
* `true`: throw if descriptor cannot be merged (e.g. non-configurable or non-writable)
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
Object.freeze(obj);
|
|
68
|
+
|
|
69
|
+
bunshinClone(obj, { strictDescriptors: true });
|
|
70
|
+
// => may throw TypeError
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Supported Types
|
|
74
|
+
|
|
75
|
+
bunshin-clone correctly handles:
|
|
76
|
+
|
|
77
|
+
* Object (plain + prototype preserved)
|
|
78
|
+
* Array
|
|
79
|
+
* Map
|
|
80
|
+
* Set
|
|
81
|
+
* Date
|
|
82
|
+
* RegExp
|
|
83
|
+
* ArrayBuffer
|
|
84
|
+
* DataView
|
|
85
|
+
* TypedArray (Uint8Array, etc.)
|
|
86
|
+
* Error / DOMException
|
|
87
|
+
* Blob
|
|
88
|
+
* ImageData
|
|
89
|
+
* URL
|
|
90
|
+
* URLSearchParams
|
|
91
|
+
|
|
92
|
+
## Circular ref
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
const a: any = { x: 1 };
|
|
96
|
+
a.self = a;
|
|
97
|
+
|
|
98
|
+
const result = bunshinClone(a);
|
|
99
|
+
|
|
100
|
+
result.self === result; // true
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Descriptor Behavior
|
|
104
|
+
|
|
105
|
+
### Default (fast path)
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
const source = {
|
|
109
|
+
get x() {
|
|
110
|
+
return 42;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const result = bunshinClone(source);
|
|
115
|
+
|
|
116
|
+
result.x; // 42
|
|
117
|
+
// getter is NOT preserved
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### preserveDescriptors: true
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
const source = {};
|
|
124
|
+
Object.defineProperty(source, 'x', {
|
|
125
|
+
get: () => 42,
|
|
126
|
+
enumerable: true,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const result = bunshinClone(source, {
|
|
130
|
+
preserveDescriptors: true,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
Object.getOwnPropertyDescriptor(result, 'x')?.get;
|
|
134
|
+
// => preserved
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Unsupported / Pass-through Types
|
|
138
|
+
|
|
139
|
+
Some values are returned as-is:
|
|
140
|
+
|
|
141
|
+
* Function
|
|
142
|
+
* WeakMap / WeakSet
|
|
143
|
+
* Proxy (not cloned)
|
|
144
|
+
* Other non-cloneable host objects
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
const fn = () => {};
|
|
148
|
+
|
|
149
|
+
bunshinClone(fn) === fn; // true
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Design Notes
|
|
153
|
+
|
|
154
|
+
### Deep clone (no structural sharing)
|
|
155
|
+
|
|
156
|
+
Unlike merge utilities, bunshin-clone always produces a new structure:
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
const source = { a: { b: 1 } };
|
|
160
|
+
|
|
161
|
+
const result = bunshinClone(source);
|
|
162
|
+
|
|
163
|
+
result !== source; // true
|
|
164
|
+
result.a !== source.a; // true
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Getter / Setter behavior
|
|
168
|
+
|
|
169
|
+
* Default: evaluated and converted to value
|
|
170
|
+
* preserveDescriptors: preserved as-is
|
|
171
|
+
|
|
172
|
+
### Descriptor safety
|
|
173
|
+
|
|
174
|
+
When `preserveDescriptors` is enabled:
|
|
175
|
+
|
|
176
|
+
* descriptors are cloned safely
|
|
177
|
+
* original object is never mutated
|
|
178
|
+
* errors are controlled via `strictDescriptors`
|
|
179
|
+
|
|
180
|
+
## Performance
|
|
181
|
+
|
|
182
|
+
* No proxy / no diffing
|
|
183
|
+
* Minimal branching
|
|
184
|
+
* Fast path for plain objects and arrays
|
|
185
|
+
* Competitive with structuredClone in many cases
|
|
186
|
+
|
|
187
|
+
## Comparison
|
|
188
|
+
|
|
189
|
+
| Feature | bunshin-clone | structuredClone | lodash.clonedeep |
|
|
190
|
+
|---------------------|--------------|----------------|------------------|
|
|
191
|
+
| Circular refs | ✅ | ✅ | ✅ |
|
|
192
|
+
| Map / Set | ✅ | ✅ | ⚠️ (partial) |
|
|
193
|
+
| TypedArray | ✅ | ✅ | ⚠️ (shallow) |
|
|
194
|
+
| Descriptor support | ✅ | ❌ | ❌ |
|
|
195
|
+
| Functions | pass-through | ❌ (throws) | pass-through |
|
|
196
|
+
| Prototype preserved | ✅ | ❌ | ⚠️ |
|
|
197
|
+
| Custom control | ✅ | ❌ | ❌ |
|
|
198
|
+
| Performance | ⚡ fast | ⚡ fast | 🐢 slower |
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var EMPTY_OPTIONS = {};
|
|
5
|
+
var { hasOwnProperty: HAS_OWN } = Object.prototype;
|
|
6
|
+
var { toString: OBJECT_TO_STRING } = Object.prototype;
|
|
7
|
+
function bunshinClone(target, options) {
|
|
8
|
+
return clone(target, options ?? EMPTY_OPTIONS, /* @__PURE__ */ new WeakMap());
|
|
9
|
+
}
|
|
10
|
+
function clone(node, options, ref) {
|
|
11
|
+
if (!isObject(node)) {
|
|
12
|
+
return node;
|
|
13
|
+
}
|
|
14
|
+
const cached = ref.get(node);
|
|
15
|
+
if (cached !== void 0) {
|
|
16
|
+
return cached;
|
|
17
|
+
}
|
|
18
|
+
if (options.preserveDescriptors && isPlainObject(node)) {
|
|
19
|
+
return cloneWithDescriptors(node, options, ref);
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(node)) {
|
|
22
|
+
const result = [];
|
|
23
|
+
ref.set(node, result);
|
|
24
|
+
for (let i = 0, l = node.length; i < l; i++) {
|
|
25
|
+
result[i] = clone(node[i], options, ref);
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
if (isPlainObject(node)) {
|
|
30
|
+
const result = Object.create(Object.getPrototypeOf(node));
|
|
31
|
+
ref.set(node, result);
|
|
32
|
+
for (const key in node) {
|
|
33
|
+
if (!HAS_OWN.call(node, key) || isUnsafeKey(key)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
result[key] = clone(node[key], options, ref);
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
if (node instanceof Map) {
|
|
41
|
+
const result = /* @__PURE__ */ new Map();
|
|
42
|
+
ref.set(node, result);
|
|
43
|
+
for (const [key, value] of node) {
|
|
44
|
+
result.set(clone(key, options, ref), clone(value, options, ref));
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
if (node instanceof Set) {
|
|
49
|
+
const result = /* @__PURE__ */ new Set();
|
|
50
|
+
ref.set(node, result);
|
|
51
|
+
for (const item of node) {
|
|
52
|
+
result.add(clone(item, options, ref));
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
if (node instanceof Date) {
|
|
57
|
+
const result = new Date(node.getTime());
|
|
58
|
+
ref.set(node, result);
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
if (node instanceof RegExp) {
|
|
62
|
+
const result = new RegExp(node.source, node.flags);
|
|
63
|
+
ref.set(node, result);
|
|
64
|
+
result.lastIndex = node.lastIndex;
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
if (node instanceof ArrayBuffer) {
|
|
68
|
+
const result = node.slice(0);
|
|
69
|
+
ref.set(node, result);
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
if (ArrayBuffer.isView(node)) {
|
|
73
|
+
if (node instanceof DataView) {
|
|
74
|
+
const result = new DataView(
|
|
75
|
+
node.buffer.slice(0),
|
|
76
|
+
node.byteOffset,
|
|
77
|
+
node.byteLength
|
|
78
|
+
);
|
|
79
|
+
ref.set(node, result);
|
|
80
|
+
return result;
|
|
81
|
+
} else {
|
|
82
|
+
const Ctor = node.constructor;
|
|
83
|
+
const result = new Ctor(node);
|
|
84
|
+
ref.set(node, result);
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (node instanceof Error || typeof DOMException !== "undefined" && node instanceof DOMException) {
|
|
89
|
+
const result = cloneError(node, options, ref);
|
|
90
|
+
ref.set(node, result);
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
if (typeof Blob !== "undefined" && node instanceof Blob) {
|
|
94
|
+
const result = node.slice(0, node.size, node.type);
|
|
95
|
+
ref.set(node, result);
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
if (typeof ImageData !== "undefined" && node instanceof ImageData) {
|
|
99
|
+
const result = new ImageData(
|
|
100
|
+
new Uint8ClampedArray(node.data),
|
|
101
|
+
node.width,
|
|
102
|
+
node.height
|
|
103
|
+
);
|
|
104
|
+
ref.set(node, result);
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
if (typeof URL !== "undefined" && node instanceof URL) {
|
|
108
|
+
const result = new URL(node.href);
|
|
109
|
+
ref.set(node, result);
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
if (typeof URLSearchParams !== "undefined" && node instanceof URLSearchParams) {
|
|
113
|
+
const result = new URLSearchParams();
|
|
114
|
+
ref.set(node, result);
|
|
115
|
+
for (const [key, value] of node) {
|
|
116
|
+
result.append(key, value);
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
ref.set(node, node);
|
|
121
|
+
return node;
|
|
122
|
+
}
|
|
123
|
+
function cloneError(value, options, ref) {
|
|
124
|
+
const name = value.name || "Error";
|
|
125
|
+
const message = value.message || "";
|
|
126
|
+
let result;
|
|
127
|
+
switch (name) {
|
|
128
|
+
case "RangeError":
|
|
129
|
+
result = new RangeError(message);
|
|
130
|
+
break;
|
|
131
|
+
case "ReferenceError":
|
|
132
|
+
result = new ReferenceError(message);
|
|
133
|
+
break;
|
|
134
|
+
case "SyntaxError":
|
|
135
|
+
result = new SyntaxError(message);
|
|
136
|
+
break;
|
|
137
|
+
case "TypeError":
|
|
138
|
+
result = new TypeError(message);
|
|
139
|
+
break;
|
|
140
|
+
default:
|
|
141
|
+
result = new Error(message);
|
|
142
|
+
result.name = name;
|
|
143
|
+
}
|
|
144
|
+
if (!options.preserveDescriptors && "cause" in value) {
|
|
145
|
+
result.cause = clone(result.cause, options, ref);
|
|
146
|
+
}
|
|
147
|
+
if (value.stack) {
|
|
148
|
+
result.stack = value.stack;
|
|
149
|
+
}
|
|
150
|
+
for (const key of Object.keys(value)) {
|
|
151
|
+
if (key === "name" || key === "message" || key === "stack") {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
result[key] = value[key];
|
|
155
|
+
}
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
function cloneWithDescriptors(node, options, ref) {
|
|
159
|
+
const result = Object.create(Object.getPrototypeOf(node));
|
|
160
|
+
ref.set(node, result);
|
|
161
|
+
const descriptors = Object.getOwnPropertyDescriptors(node);
|
|
162
|
+
const keys = Reflect.ownKeys(descriptors);
|
|
163
|
+
for (let i = 0, l = keys.length; i < l; i++) {
|
|
164
|
+
const key = keys[i];
|
|
165
|
+
if (isUnsafeKey(key)) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const descriptor = { ...descriptors[key] };
|
|
169
|
+
if ("value" in descriptor) {
|
|
170
|
+
descriptor.value = clone(descriptor.value, options, ref);
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
Object.defineProperty(result, key, descriptor);
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (options.strictDescriptors) {
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
function isObject(value) {
|
|
183
|
+
return typeof value === "object" && value !== null;
|
|
184
|
+
}
|
|
185
|
+
function isPlainObject(value) {
|
|
186
|
+
return OBJECT_TO_STRING.call(value) === "[object Object]";
|
|
187
|
+
}
|
|
188
|
+
function isUnsafeKey(key) {
|
|
189
|
+
return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* bunshin-clone
|
|
193
|
+
* High-performance deep clone utility with descriptor support.
|
|
194
|
+
* Supports circular ref and complex built-in types.
|
|
195
|
+
*
|
|
196
|
+
* @version 1.0.0
|
|
197
|
+
* @author Yusuke Kamiyamane
|
|
198
|
+
* @license MIT
|
|
199
|
+
* @copyright Copyright (c) 2026 Yusuke Kamiyamane
|
|
200
|
+
* @see {@link https://github.com/y14e/bunshin-clone}
|
|
201
|
+
*/
|
|
202
|
+
|
|
203
|
+
module.exports = bunshinClone;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bunshin-clone
|
|
3
|
+
* High-performance deep clone utility with descriptor support.
|
|
4
|
+
* Supports circular ref and complex built-in types.
|
|
5
|
+
*
|
|
6
|
+
* @version 1.0.0
|
|
7
|
+
* @author Yusuke Kamiyamane
|
|
8
|
+
* @license MIT
|
|
9
|
+
* @copyright Copyright (c) 2026 Yusuke Kamiyamane
|
|
10
|
+
* @see {@link https://github.com/y14e/bunshin-clone}
|
|
11
|
+
*/
|
|
12
|
+
interface BunshinCloneOptions {
|
|
13
|
+
readonly preserveDescriptors?: boolean;
|
|
14
|
+
readonly strictDescriptors?: boolean;
|
|
15
|
+
}
|
|
16
|
+
declare function bunshinClone(target: unknown, options?: BunshinCloneOptions): unknown;
|
|
17
|
+
|
|
18
|
+
export { type BunshinCloneOptions, bunshinClone as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bunshin-clone
|
|
3
|
+
* High-performance deep clone utility with descriptor support.
|
|
4
|
+
* Supports circular ref and complex built-in types.
|
|
5
|
+
*
|
|
6
|
+
* @version 1.0.0
|
|
7
|
+
* @author Yusuke Kamiyamane
|
|
8
|
+
* @license MIT
|
|
9
|
+
* @copyright Copyright (c) 2026 Yusuke Kamiyamane
|
|
10
|
+
* @see {@link https://github.com/y14e/bunshin-clone}
|
|
11
|
+
*/
|
|
12
|
+
interface BunshinCloneOptions {
|
|
13
|
+
readonly preserveDescriptors?: boolean;
|
|
14
|
+
readonly strictDescriptors?: boolean;
|
|
15
|
+
}
|
|
16
|
+
declare function bunshinClone(target: unknown, options?: BunshinCloneOptions): unknown;
|
|
17
|
+
|
|
18
|
+
export { type BunshinCloneOptions, bunshinClone as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var EMPTY_OPTIONS = {};
|
|
3
|
+
var { hasOwnProperty: HAS_OWN } = Object.prototype;
|
|
4
|
+
var { toString: OBJECT_TO_STRING } = Object.prototype;
|
|
5
|
+
function bunshinClone(target, options) {
|
|
6
|
+
return clone(target, options ?? EMPTY_OPTIONS, /* @__PURE__ */ new WeakMap());
|
|
7
|
+
}
|
|
8
|
+
function clone(node, options, ref) {
|
|
9
|
+
if (!isObject(node)) {
|
|
10
|
+
return node;
|
|
11
|
+
}
|
|
12
|
+
const cached = ref.get(node);
|
|
13
|
+
if (cached !== void 0) {
|
|
14
|
+
return cached;
|
|
15
|
+
}
|
|
16
|
+
if (options.preserveDescriptors && isPlainObject(node)) {
|
|
17
|
+
return cloneWithDescriptors(node, options, ref);
|
|
18
|
+
}
|
|
19
|
+
if (Array.isArray(node)) {
|
|
20
|
+
const result = [];
|
|
21
|
+
ref.set(node, result);
|
|
22
|
+
for (let i = 0, l = node.length; i < l; i++) {
|
|
23
|
+
result[i] = clone(node[i], options, ref);
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
if (isPlainObject(node)) {
|
|
28
|
+
const result = Object.create(Object.getPrototypeOf(node));
|
|
29
|
+
ref.set(node, result);
|
|
30
|
+
for (const key in node) {
|
|
31
|
+
if (!HAS_OWN.call(node, key) || isUnsafeKey(key)) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
result[key] = clone(node[key], options, ref);
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
if (node instanceof Map) {
|
|
39
|
+
const result = /* @__PURE__ */ new Map();
|
|
40
|
+
ref.set(node, result);
|
|
41
|
+
for (const [key, value] of node) {
|
|
42
|
+
result.set(clone(key, options, ref), clone(value, options, ref));
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
if (node instanceof Set) {
|
|
47
|
+
const result = /* @__PURE__ */ new Set();
|
|
48
|
+
ref.set(node, result);
|
|
49
|
+
for (const item of node) {
|
|
50
|
+
result.add(clone(item, options, ref));
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
if (node instanceof Date) {
|
|
55
|
+
const result = new Date(node.getTime());
|
|
56
|
+
ref.set(node, result);
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
if (node instanceof RegExp) {
|
|
60
|
+
const result = new RegExp(node.source, node.flags);
|
|
61
|
+
ref.set(node, result);
|
|
62
|
+
result.lastIndex = node.lastIndex;
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
if (node instanceof ArrayBuffer) {
|
|
66
|
+
const result = node.slice(0);
|
|
67
|
+
ref.set(node, result);
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
if (ArrayBuffer.isView(node)) {
|
|
71
|
+
if (node instanceof DataView) {
|
|
72
|
+
const result = new DataView(
|
|
73
|
+
node.buffer.slice(0),
|
|
74
|
+
node.byteOffset,
|
|
75
|
+
node.byteLength
|
|
76
|
+
);
|
|
77
|
+
ref.set(node, result);
|
|
78
|
+
return result;
|
|
79
|
+
} else {
|
|
80
|
+
const Ctor = node.constructor;
|
|
81
|
+
const result = new Ctor(node);
|
|
82
|
+
ref.set(node, result);
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (node instanceof Error || typeof DOMException !== "undefined" && node instanceof DOMException) {
|
|
87
|
+
const result = cloneError(node, options, ref);
|
|
88
|
+
ref.set(node, result);
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
if (typeof Blob !== "undefined" && node instanceof Blob) {
|
|
92
|
+
const result = node.slice(0, node.size, node.type);
|
|
93
|
+
ref.set(node, result);
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
if (typeof ImageData !== "undefined" && node instanceof ImageData) {
|
|
97
|
+
const result = new ImageData(
|
|
98
|
+
new Uint8ClampedArray(node.data),
|
|
99
|
+
node.width,
|
|
100
|
+
node.height
|
|
101
|
+
);
|
|
102
|
+
ref.set(node, result);
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
if (typeof URL !== "undefined" && node instanceof URL) {
|
|
106
|
+
const result = new URL(node.href);
|
|
107
|
+
ref.set(node, result);
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
if (typeof URLSearchParams !== "undefined" && node instanceof URLSearchParams) {
|
|
111
|
+
const result = new URLSearchParams();
|
|
112
|
+
ref.set(node, result);
|
|
113
|
+
for (const [key, value] of node) {
|
|
114
|
+
result.append(key, value);
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
ref.set(node, node);
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
121
|
+
function cloneError(value, options, ref) {
|
|
122
|
+
const name = value.name || "Error";
|
|
123
|
+
const message = value.message || "";
|
|
124
|
+
let result;
|
|
125
|
+
switch (name) {
|
|
126
|
+
case "RangeError":
|
|
127
|
+
result = new RangeError(message);
|
|
128
|
+
break;
|
|
129
|
+
case "ReferenceError":
|
|
130
|
+
result = new ReferenceError(message);
|
|
131
|
+
break;
|
|
132
|
+
case "SyntaxError":
|
|
133
|
+
result = new SyntaxError(message);
|
|
134
|
+
break;
|
|
135
|
+
case "TypeError":
|
|
136
|
+
result = new TypeError(message);
|
|
137
|
+
break;
|
|
138
|
+
default:
|
|
139
|
+
result = new Error(message);
|
|
140
|
+
result.name = name;
|
|
141
|
+
}
|
|
142
|
+
if (!options.preserveDescriptors && "cause" in value) {
|
|
143
|
+
result.cause = clone(result.cause, options, ref);
|
|
144
|
+
}
|
|
145
|
+
if (value.stack) {
|
|
146
|
+
result.stack = value.stack;
|
|
147
|
+
}
|
|
148
|
+
for (const key of Object.keys(value)) {
|
|
149
|
+
if (key === "name" || key === "message" || key === "stack") {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
result[key] = value[key];
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
function cloneWithDescriptors(node, options, ref) {
|
|
157
|
+
const result = Object.create(Object.getPrototypeOf(node));
|
|
158
|
+
ref.set(node, result);
|
|
159
|
+
const descriptors = Object.getOwnPropertyDescriptors(node);
|
|
160
|
+
const keys = Reflect.ownKeys(descriptors);
|
|
161
|
+
for (let i = 0, l = keys.length; i < l; i++) {
|
|
162
|
+
const key = keys[i];
|
|
163
|
+
if (isUnsafeKey(key)) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const descriptor = { ...descriptors[key] };
|
|
167
|
+
if ("value" in descriptor) {
|
|
168
|
+
descriptor.value = clone(descriptor.value, options, ref);
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
Object.defineProperty(result, key, descriptor);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
if (options.strictDescriptors) {
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
function isObject(value) {
|
|
181
|
+
return typeof value === "object" && value !== null;
|
|
182
|
+
}
|
|
183
|
+
function isPlainObject(value) {
|
|
184
|
+
return OBJECT_TO_STRING.call(value) === "[object Object]";
|
|
185
|
+
}
|
|
186
|
+
function isUnsafeKey(key) {
|
|
187
|
+
return typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* bunshin-clone
|
|
191
|
+
* High-performance deep clone utility with descriptor support.
|
|
192
|
+
* Supports circular ref and complex built-in types.
|
|
193
|
+
*
|
|
194
|
+
* @version 1.0.0
|
|
195
|
+
* @author Yusuke Kamiyamane
|
|
196
|
+
* @license MIT
|
|
197
|
+
* @copyright Copyright (c) 2026 Yusuke Kamiyamane
|
|
198
|
+
* @see {@link https://github.com/y14e/bunshin-clone}
|
|
199
|
+
*/
|
|
200
|
+
|
|
201
|
+
export { bunshinClone as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bunshin-clone",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "High-performance deep clone utility with descriptor support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"test": "bun test",
|
|
24
|
+
"prepublishOnly": "npm run build && npm test",
|
|
25
|
+
"lint": "tsc --noEmit"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"clone",
|
|
29
|
+
"deep",
|
|
30
|
+
"typescript"
|
|
31
|
+
],
|
|
32
|
+
"author": "Yusuke Kamiyamane",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/y14e/bunshin-clone.git"
|
|
37
|
+
},
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/y14e/bunshin-clone/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/y14e/bunshin-clone#readme",
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"bun-types": "latest",
|
|
44
|
+
"tsup": "^8.0.0",
|
|
45
|
+
"typescript": "^5.6.0"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=18"
|
|
49
|
+
}
|
|
50
|
+
}
|