bunshin-clone 1.8.10 → 1.8.12
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 +31 -23
- package/dist/index.js +31 -23
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -81,11 +81,15 @@ function clone(value, settings, refs) {
|
|
|
81
81
|
refs.set(value, value);
|
|
82
82
|
return value;
|
|
83
83
|
}
|
|
84
|
+
var IS_ENUMERABLE = Object.prototype.propertyIsEnumerable;
|
|
85
|
+
var OWN_DESCS = Object.getOwnPropertyDescriptors;
|
|
86
|
+
var OWN_NAMES = Object.getOwnPropertyNames;
|
|
87
|
+
var OWN_SYMBOLS = Object.getOwnPropertySymbols;
|
|
84
88
|
function cloneWithDescriptors(object, settings, refs) {
|
|
85
89
|
const result = Object.create(Object.getPrototypeOf(object));
|
|
86
90
|
refs.set(object, result);
|
|
87
|
-
const descs =
|
|
88
|
-
for (const key of
|
|
91
|
+
const descs = OWN_DESCS(object);
|
|
92
|
+
for (const key of OWN_NAMES(object)) {
|
|
89
93
|
if (isUnsafeKey(key)) {
|
|
90
94
|
continue;
|
|
91
95
|
}
|
|
@@ -101,38 +105,42 @@ function cloneWithDescriptors(object, settings, refs) {
|
|
|
101
105
|
}
|
|
102
106
|
}
|
|
103
107
|
}
|
|
108
|
+
if (settings.preserveSymbolKeys) {
|
|
109
|
+
for (const symbol of OWN_SYMBOLS(object)) {
|
|
110
|
+
const desc = { ...descs[symbol] };
|
|
111
|
+
if ("value" in desc) {
|
|
112
|
+
desc.value = clone(desc.value, settings, refs);
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
Object.defineProperty(result, symbol, desc);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (settings.strictDescriptors) {
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
104
123
|
return result;
|
|
105
124
|
}
|
|
106
125
|
function cloneArray(array, settings, refs) {
|
|
107
|
-
|
|
108
|
-
const result2 = array.slice();
|
|
109
|
-
refs.set(array, result2);
|
|
110
|
-
return result2;
|
|
111
|
-
}
|
|
112
|
-
const { length } = array;
|
|
113
|
-
const result = new Array(length);
|
|
126
|
+
const result = isPrimitiveArray(array) ? array.slice() : array.map((item) => clone(item, settings, refs));
|
|
114
127
|
refs.set(array, result);
|
|
115
|
-
for (let i = 0; i < length; i++) {
|
|
116
|
-
result[i] = clone(array[i], settings, refs);
|
|
117
|
-
}
|
|
118
128
|
return result;
|
|
119
129
|
}
|
|
120
130
|
function clonePlainObject(object, settings, refs) {
|
|
121
131
|
const result = Object.create(Object.getPrototypeOf(object));
|
|
122
132
|
refs.set(object, result);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
result[key] = clone(object[key], settings, refs);
|
|
133
|
+
for (const key of Object.keys(object)) {
|
|
134
|
+
if (isUnsafeKey(key)) {
|
|
135
|
+
continue;
|
|
129
136
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
137
|
+
result[key] = clone(object[key], settings, refs);
|
|
138
|
+
}
|
|
139
|
+
if (settings.preserveSymbolKeys) {
|
|
140
|
+
for (const symbol of OWN_SYMBOLS(object)) {
|
|
141
|
+
if (IS_ENUMERABLE.call(object, symbol)) {
|
|
142
|
+
result[symbol] = clone(object[symbol], settings, refs);
|
|
134
143
|
}
|
|
135
|
-
result[key] = clone(object[key], settings, refs);
|
|
136
144
|
}
|
|
137
145
|
}
|
|
138
146
|
return result;
|
package/dist/index.js
CHANGED
|
@@ -79,11 +79,15 @@ function clone(value, settings, refs) {
|
|
|
79
79
|
refs.set(value, value);
|
|
80
80
|
return value;
|
|
81
81
|
}
|
|
82
|
+
var IS_ENUMERABLE = Object.prototype.propertyIsEnumerable;
|
|
83
|
+
var OWN_DESCS = Object.getOwnPropertyDescriptors;
|
|
84
|
+
var OWN_NAMES = Object.getOwnPropertyNames;
|
|
85
|
+
var OWN_SYMBOLS = Object.getOwnPropertySymbols;
|
|
82
86
|
function cloneWithDescriptors(object, settings, refs) {
|
|
83
87
|
const result = Object.create(Object.getPrototypeOf(object));
|
|
84
88
|
refs.set(object, result);
|
|
85
|
-
const descs =
|
|
86
|
-
for (const key of
|
|
89
|
+
const descs = OWN_DESCS(object);
|
|
90
|
+
for (const key of OWN_NAMES(object)) {
|
|
87
91
|
if (isUnsafeKey(key)) {
|
|
88
92
|
continue;
|
|
89
93
|
}
|
|
@@ -99,38 +103,42 @@ function cloneWithDescriptors(object, settings, refs) {
|
|
|
99
103
|
}
|
|
100
104
|
}
|
|
101
105
|
}
|
|
106
|
+
if (settings.preserveSymbolKeys) {
|
|
107
|
+
for (const symbol of OWN_SYMBOLS(object)) {
|
|
108
|
+
const desc = { ...descs[symbol] };
|
|
109
|
+
if ("value" in desc) {
|
|
110
|
+
desc.value = clone(desc.value, settings, refs);
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
Object.defineProperty(result, symbol, desc);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (settings.strictDescriptors) {
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
102
121
|
return result;
|
|
103
122
|
}
|
|
104
123
|
function cloneArray(array, settings, refs) {
|
|
105
|
-
|
|
106
|
-
const result2 = array.slice();
|
|
107
|
-
refs.set(array, result2);
|
|
108
|
-
return result2;
|
|
109
|
-
}
|
|
110
|
-
const { length } = array;
|
|
111
|
-
const result = new Array(length);
|
|
124
|
+
const result = isPrimitiveArray(array) ? array.slice() : array.map((item) => clone(item, settings, refs));
|
|
112
125
|
refs.set(array, result);
|
|
113
|
-
for (let i = 0; i < length; i++) {
|
|
114
|
-
result[i] = clone(array[i], settings, refs);
|
|
115
|
-
}
|
|
116
126
|
return result;
|
|
117
127
|
}
|
|
118
128
|
function clonePlainObject(object, settings, refs) {
|
|
119
129
|
const result = Object.create(Object.getPrototypeOf(object));
|
|
120
130
|
refs.set(object, result);
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
result[key] = clone(object[key], settings, refs);
|
|
131
|
+
for (const key of Object.keys(object)) {
|
|
132
|
+
if (isUnsafeKey(key)) {
|
|
133
|
+
continue;
|
|
127
134
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
135
|
+
result[key] = clone(object[key], settings, refs);
|
|
136
|
+
}
|
|
137
|
+
if (settings.preserveSymbolKeys) {
|
|
138
|
+
for (const symbol of OWN_SYMBOLS(object)) {
|
|
139
|
+
if (IS_ENUMERABLE.call(object, symbol)) {
|
|
140
|
+
result[symbol] = clone(object[symbol], settings, refs);
|
|
132
141
|
}
|
|
133
|
-
result[key] = clone(object[key], settings, refs);
|
|
134
142
|
}
|
|
135
143
|
}
|
|
136
144
|
return result;
|