bunshin-clone 1.6.0 → 1.7.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 +22 -5
- package/dist/index.js +22 -5
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
4
|
var EMPTY_OPTIONS = {};
|
|
5
|
+
var { propertyIsEnumerable: IS_ENUMERABLE } = Object.prototype;
|
|
5
6
|
function bunshinClone(value, options = EMPTY_OPTIONS, refs = /* @__PURE__ */ new WeakMap()) {
|
|
6
7
|
return clone(value, resolveOptions(options), refs);
|
|
7
8
|
}
|
|
@@ -9,13 +10,19 @@ function clone(value, settings, refs) {
|
|
|
9
10
|
if (!isObject(value)) {
|
|
10
11
|
return value;
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
const ref = refs.get(value);
|
|
14
|
+
if (ref !== void 0) {
|
|
15
|
+
return ref;
|
|
14
16
|
}
|
|
15
17
|
if (settings.preserveDescriptors && isPlainObject(value)) {
|
|
16
18
|
return cloneWithDescriptors(value, settings, refs);
|
|
17
19
|
}
|
|
18
20
|
if (Array.isArray(value)) {
|
|
21
|
+
if (canShallowCloneArray(value)) {
|
|
22
|
+
const result2 = value.slice();
|
|
23
|
+
refs.set(value, result2);
|
|
24
|
+
return result2;
|
|
25
|
+
}
|
|
19
26
|
const { length } = value;
|
|
20
27
|
const result = new Array(length);
|
|
21
28
|
refs.set(value, result);
|
|
@@ -28,9 +35,10 @@ function clone(value, settings, refs) {
|
|
|
28
35
|
const result = Object.create(Object.getPrototypeOf(value));
|
|
29
36
|
refs.set(value, result);
|
|
30
37
|
for (const key of Reflect.ownKeys(value)) {
|
|
31
|
-
if (
|
|
32
|
-
|
|
38
|
+
if (isUnsafeKey(key) || !IS_ENUMERABLE.call(value, key)) {
|
|
39
|
+
continue;
|
|
33
40
|
}
|
|
41
|
+
result[key] = clone(value[key], settings, refs);
|
|
34
42
|
}
|
|
35
43
|
return result;
|
|
36
44
|
}
|
|
@@ -123,7 +131,8 @@ function clone(value, settings, refs) {
|
|
|
123
131
|
return value;
|
|
124
132
|
}
|
|
125
133
|
function cloneBuffer(buffer, refs) {
|
|
126
|
-
|
|
134
|
+
const ref = refs.get(buffer);
|
|
135
|
+
if (ref !== void 0) {
|
|
127
136
|
return refs.get(buffer);
|
|
128
137
|
}
|
|
129
138
|
const result = buffer.slice(0);
|
|
@@ -200,6 +209,14 @@ function createErrorInstance(error, settings, refs) {
|
|
|
200
209
|
result.name = name;
|
|
201
210
|
return result;
|
|
202
211
|
}
|
|
212
|
+
function canShallowCloneArray(array) {
|
|
213
|
+
for (let i = 0, l = array.length; i < l; i++) {
|
|
214
|
+
if (isObject(array[i])) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
203
220
|
function forEachOwnKey(object, callback) {
|
|
204
221
|
for (const key of Object.keys(object)) {
|
|
205
222
|
callback(key);
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
var EMPTY_OPTIONS = {};
|
|
3
|
+
var { propertyIsEnumerable: IS_ENUMERABLE } = Object.prototype;
|
|
3
4
|
function bunshinClone(value, options = EMPTY_OPTIONS, refs = /* @__PURE__ */ new WeakMap()) {
|
|
4
5
|
return clone(value, resolveOptions(options), refs);
|
|
5
6
|
}
|
|
@@ -7,13 +8,19 @@ function clone(value, settings, refs) {
|
|
|
7
8
|
if (!isObject(value)) {
|
|
8
9
|
return value;
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
const ref = refs.get(value);
|
|
12
|
+
if (ref !== void 0) {
|
|
13
|
+
return ref;
|
|
12
14
|
}
|
|
13
15
|
if (settings.preserveDescriptors && isPlainObject(value)) {
|
|
14
16
|
return cloneWithDescriptors(value, settings, refs);
|
|
15
17
|
}
|
|
16
18
|
if (Array.isArray(value)) {
|
|
19
|
+
if (canShallowCloneArray(value)) {
|
|
20
|
+
const result2 = value.slice();
|
|
21
|
+
refs.set(value, result2);
|
|
22
|
+
return result2;
|
|
23
|
+
}
|
|
17
24
|
const { length } = value;
|
|
18
25
|
const result = new Array(length);
|
|
19
26
|
refs.set(value, result);
|
|
@@ -26,9 +33,10 @@ function clone(value, settings, refs) {
|
|
|
26
33
|
const result = Object.create(Object.getPrototypeOf(value));
|
|
27
34
|
refs.set(value, result);
|
|
28
35
|
for (const key of Reflect.ownKeys(value)) {
|
|
29
|
-
if (
|
|
30
|
-
|
|
36
|
+
if (isUnsafeKey(key) || !IS_ENUMERABLE.call(value, key)) {
|
|
37
|
+
continue;
|
|
31
38
|
}
|
|
39
|
+
result[key] = clone(value[key], settings, refs);
|
|
32
40
|
}
|
|
33
41
|
return result;
|
|
34
42
|
}
|
|
@@ -121,7 +129,8 @@ function clone(value, settings, refs) {
|
|
|
121
129
|
return value;
|
|
122
130
|
}
|
|
123
131
|
function cloneBuffer(buffer, refs) {
|
|
124
|
-
|
|
132
|
+
const ref = refs.get(buffer);
|
|
133
|
+
if (ref !== void 0) {
|
|
125
134
|
return refs.get(buffer);
|
|
126
135
|
}
|
|
127
136
|
const result = buffer.slice(0);
|
|
@@ -198,6 +207,14 @@ function createErrorInstance(error, settings, refs) {
|
|
|
198
207
|
result.name = name;
|
|
199
208
|
return result;
|
|
200
209
|
}
|
|
210
|
+
function canShallowCloneArray(array) {
|
|
211
|
+
for (let i = 0, l = array.length; i < l; i++) {
|
|
212
|
+
if (isObject(array[i])) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
201
218
|
function forEachOwnKey(object, callback) {
|
|
202
219
|
for (const key of Object.keys(object)) {
|
|
203
220
|
callback(key);
|