bunshin-clone 1.0.13 → 1.2.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 CHANGED
@@ -1,11 +1,35 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  // src/index.ts
4
6
  var EMPTY_OPTIONS = {};
5
7
  var { hasOwnProperty: HAS_OWN } = Object.prototype;
6
8
  function bunshinClone(source, options) {
7
9
  return clone(source, options ?? EMPTY_OPTIONS, /* @__PURE__ */ new WeakMap());
8
10
  }
11
+ function cloneWithDescriptors(node, options, refs) {
12
+ const result = Object.create(Object.getPrototypeOf(node));
13
+ refs.set(node, result);
14
+ const descs = Object.getOwnPropertyDescriptors(node);
15
+ forEachOwnKey(descs, (key) => {
16
+ if (isUnsafeKey(key)) {
17
+ return;
18
+ }
19
+ const desc = { ...descs[key] };
20
+ if ("value" in desc) {
21
+ desc.value = clone(desc.value, options, refs);
22
+ }
23
+ try {
24
+ Object.defineProperty(result, key, desc);
25
+ } catch (error) {
26
+ if (options.strictDescriptors) {
27
+ throw error;
28
+ }
29
+ }
30
+ });
31
+ return result;
32
+ }
9
33
  function clone(node, options, refs) {
10
34
  if (!isObject(node)) {
11
35
  return node;
@@ -71,17 +95,14 @@ function clone(node, options, refs) {
71
95
  if (ArrayBuffer.isView(node)) {
72
96
  const { buffer, byteOffset, byteLength } = node;
73
97
  if (node instanceof DataView) {
74
- const result = new DataView(buffer.slice(0), byteOffset, byteLength);
75
- refs.set(node, result);
76
- return result;
77
- } else {
78
- const Ctor = node.constructor;
79
- const result = new Ctor(
80
- buffer.slice(byteOffset, byteOffset + byteLength)
81
- );
82
- refs.set(node, result);
83
- return result;
98
+ const result2 = new DataView(buffer.slice(0), byteOffset, byteLength);
99
+ refs.set(node, result2);
100
+ return result2;
84
101
  }
102
+ const Ctor = node.constructor;
103
+ const result = new Ctor(buffer.slice(byteOffset, byteOffset + byteLength));
104
+ refs.set(node, result);
105
+ return result;
85
106
  }
86
107
  if (node instanceof Error || typeof DOMException !== "undefined" && node instanceof DOMException) {
87
108
  return cloneError(node, options, refs);
@@ -163,28 +184,6 @@ function cloneError(value, options, refs) {
163
184
  }
164
185
  return result;
165
186
  }
166
- function cloneWithDescriptors(node, options, refs) {
167
- const result = Object.create(Object.getPrototypeOf(node));
168
- refs.set(node, result);
169
- const descs = Object.getOwnPropertyDescriptors(node);
170
- forEachOwnKey(descs, (key) => {
171
- if (isUnsafeKey(key)) {
172
- return;
173
- }
174
- const desc = { ...descs[key] };
175
- if ("value" in desc) {
176
- desc.value = clone(desc.value, options, refs);
177
- }
178
- try {
179
- Object.defineProperty(result, key, desc);
180
- } catch (error) {
181
- if (options.strictDescriptors) {
182
- throw error;
183
- }
184
- }
185
- });
186
- return result;
187
- }
188
187
  function forEachOwnKey(object, fn) {
189
188
  for (const key of Object.keys(object)) {
190
189
  fn(key);
@@ -212,11 +211,12 @@ function isUnsafeKey(key) {
212
211
  * High-performance deep clone utility with descriptor support.
213
212
  * Handles circular ref and complex built-in types.
214
213
  *
215
- * @version 1.0.13
214
+ * @version 1.2.0
216
215
  * @author Yusuke Kamiyamane
217
216
  * @license MIT
218
- * @copyright Copyright (c) 2026 Yusuke Kamiyamane
217
+ * @copyright Copyright (c) Yusuke Kamiyamane
219
218
  * @see {@link https://github.com/y14e/bunshin-clone}
220
219
  */
221
220
 
222
- module.exports = bunshinClone;
221
+ exports.cloneWithDescriptors = cloneWithDescriptors;
222
+ exports.default = bunshinClone;
package/dist/index.d.cts CHANGED
@@ -3,16 +3,19 @@
3
3
  * High-performance deep clone utility with descriptor support.
4
4
  * Handles circular ref and complex built-in types.
5
5
  *
6
- * @version 1.0.13
6
+ * @version 1.2.0
7
7
  * @author Yusuke Kamiyamane
8
8
  * @license MIT
9
- * @copyright Copyright (c) 2026 Yusuke Kamiyamane
9
+ * @copyright Copyright (c) Yusuke Kamiyamane
10
10
  * @see {@link https://github.com/y14e/bunshin-clone}
11
11
  */
12
12
  interface BunshinCloneOptions {
13
13
  readonly preserveDescriptors?: boolean;
14
14
  readonly strictDescriptors?: boolean;
15
15
  }
16
+ type Object$1 = Record<PropertyKey, unknown>;
17
+ type Refs = WeakMap<object, unknown>;
16
18
  declare function bunshinClone<T>(source: T, options?: BunshinCloneOptions): T;
19
+ declare function cloneWithDescriptors<T extends Object$1>(node: T, options: BunshinCloneOptions, refs: Refs): T;
17
20
 
18
- export { type BunshinCloneOptions, bunshinClone as default };
21
+ export { type BunshinCloneOptions, cloneWithDescriptors, bunshinClone as default };
package/dist/index.d.ts CHANGED
@@ -3,16 +3,19 @@
3
3
  * High-performance deep clone utility with descriptor support.
4
4
  * Handles circular ref and complex built-in types.
5
5
  *
6
- * @version 1.0.13
6
+ * @version 1.2.0
7
7
  * @author Yusuke Kamiyamane
8
8
  * @license MIT
9
- * @copyright Copyright (c) 2026 Yusuke Kamiyamane
9
+ * @copyright Copyright (c) Yusuke Kamiyamane
10
10
  * @see {@link https://github.com/y14e/bunshin-clone}
11
11
  */
12
12
  interface BunshinCloneOptions {
13
13
  readonly preserveDescriptors?: boolean;
14
14
  readonly strictDescriptors?: boolean;
15
15
  }
16
+ type Object$1 = Record<PropertyKey, unknown>;
17
+ type Refs = WeakMap<object, unknown>;
16
18
  declare function bunshinClone<T>(source: T, options?: BunshinCloneOptions): T;
19
+ declare function cloneWithDescriptors<T extends Object$1>(node: T, options: BunshinCloneOptions, refs: Refs): T;
17
20
 
18
- export { type BunshinCloneOptions, bunshinClone as default };
21
+ export { type BunshinCloneOptions, cloneWithDescriptors, bunshinClone as default };
package/dist/index.js CHANGED
@@ -4,6 +4,28 @@ 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 cloneWithDescriptors(node, options, refs) {
8
+ const result = Object.create(Object.getPrototypeOf(node));
9
+ refs.set(node, result);
10
+ const descs = Object.getOwnPropertyDescriptors(node);
11
+ forEachOwnKey(descs, (key) => {
12
+ if (isUnsafeKey(key)) {
13
+ return;
14
+ }
15
+ const desc = { ...descs[key] };
16
+ if ("value" in desc) {
17
+ desc.value = clone(desc.value, options, refs);
18
+ }
19
+ try {
20
+ Object.defineProperty(result, key, desc);
21
+ } catch (error) {
22
+ if (options.strictDescriptors) {
23
+ throw error;
24
+ }
25
+ }
26
+ });
27
+ return result;
28
+ }
7
29
  function clone(node, options, refs) {
8
30
  if (!isObject(node)) {
9
31
  return node;
@@ -69,17 +91,14 @@ function clone(node, options, refs) {
69
91
  if (ArrayBuffer.isView(node)) {
70
92
  const { buffer, byteOffset, byteLength } = node;
71
93
  if (node instanceof DataView) {
72
- const result = new DataView(buffer.slice(0), byteOffset, byteLength);
73
- refs.set(node, result);
74
- return result;
75
- } else {
76
- const Ctor = node.constructor;
77
- const result = new Ctor(
78
- buffer.slice(byteOffset, byteOffset + byteLength)
79
- );
80
- refs.set(node, result);
81
- return result;
94
+ const result2 = new DataView(buffer.slice(0), byteOffset, byteLength);
95
+ refs.set(node, result2);
96
+ return result2;
82
97
  }
98
+ const Ctor = node.constructor;
99
+ const result = new Ctor(buffer.slice(byteOffset, byteOffset + byteLength));
100
+ refs.set(node, result);
101
+ return result;
83
102
  }
84
103
  if (node instanceof Error || typeof DOMException !== "undefined" && node instanceof DOMException) {
85
104
  return cloneError(node, options, refs);
@@ -161,28 +180,6 @@ function cloneError(value, options, refs) {
161
180
  }
162
181
  return result;
163
182
  }
164
- function cloneWithDescriptors(node, options, refs) {
165
- const result = Object.create(Object.getPrototypeOf(node));
166
- refs.set(node, result);
167
- const descs = Object.getOwnPropertyDescriptors(node);
168
- forEachOwnKey(descs, (key) => {
169
- if (isUnsafeKey(key)) {
170
- return;
171
- }
172
- const desc = { ...descs[key] };
173
- if ("value" in desc) {
174
- desc.value = clone(desc.value, options, refs);
175
- }
176
- try {
177
- Object.defineProperty(result, key, desc);
178
- } catch (error) {
179
- if (options.strictDescriptors) {
180
- throw error;
181
- }
182
- }
183
- });
184
- return result;
185
- }
186
183
  function forEachOwnKey(object, fn) {
187
184
  for (const key of Object.keys(object)) {
188
185
  fn(key);
@@ -210,11 +207,11 @@ 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.13
210
+ * @version 1.2.0
214
211
  * @author Yusuke Kamiyamane
215
212
  * @license MIT
216
- * @copyright Copyright (c) 2026 Yusuke Kamiyamane
213
+ * @copyright Copyright (c) Yusuke Kamiyamane
217
214
  * @see {@link https://github.com/y14e/bunshin-clone}
218
215
  */
219
216
 
220
- export { bunshinClone as default };
217
+ export { cloneWithDescriptors, bunshinClone as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunshin-clone",
3
- "version": "1.0.13",
3
+ "version": "1.2.0",
4
4
  "description": "High-performance deep clone utility with descriptor support",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",