bunshin-clone 1.4.1 → 1.4.2
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 +73 -73
- package/dist/index.js +73 -73
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -6,137 +6,137 @@ var { hasOwnProperty: HAS_OWN } = Object.prototype;
|
|
|
6
6
|
function bunshinClone(source, options = EMPTY_OPTIONS, refs = /* @__PURE__ */ new WeakMap()) {
|
|
7
7
|
return clone(source, options, refs);
|
|
8
8
|
}
|
|
9
|
-
function clone(
|
|
10
|
-
if (!isObject(
|
|
11
|
-
return
|
|
9
|
+
function clone(source, options, refs) {
|
|
10
|
+
if (!isObject(source)) {
|
|
11
|
+
return source;
|
|
12
12
|
}
|
|
13
|
-
const ref = refs.get(
|
|
13
|
+
const ref = refs.get(source);
|
|
14
14
|
if (ref !== void 0) {
|
|
15
15
|
return ref;
|
|
16
16
|
}
|
|
17
17
|
const settings = resolveOptions(options);
|
|
18
|
-
if (settings.preserveDescriptors && isPlainObject(
|
|
19
|
-
return cloneWithDescriptors(
|
|
18
|
+
if (settings.preserveDescriptors && isPlainObject(source)) {
|
|
19
|
+
return cloneWithDescriptors(source, settings, refs);
|
|
20
20
|
}
|
|
21
|
-
if (Array.isArray(
|
|
21
|
+
if (Array.isArray(source)) {
|
|
22
22
|
const result = [];
|
|
23
|
-
refs.set(
|
|
24
|
-
for (let i = 0, l =
|
|
25
|
-
result[i] = clone(
|
|
23
|
+
refs.set(source, result);
|
|
24
|
+
for (let i = 0, l = source.length; i < l; i++) {
|
|
25
|
+
result[i] = clone(source[i], settings, refs);
|
|
26
26
|
}
|
|
27
27
|
return result;
|
|
28
28
|
}
|
|
29
|
-
if (isPlainObject(
|
|
30
|
-
const result = Object.create(Object.getPrototypeOf(
|
|
31
|
-
refs.set(
|
|
32
|
-
for (const key in
|
|
33
|
-
if (!HAS_OWN.call(
|
|
29
|
+
if (isPlainObject(source)) {
|
|
30
|
+
const result = Object.create(Object.getPrototypeOf(source));
|
|
31
|
+
refs.set(source, result);
|
|
32
|
+
for (const key in source) {
|
|
33
|
+
if (!HAS_OWN.call(source, key) || isUnsafeKey(key)) {
|
|
34
34
|
continue;
|
|
35
35
|
}
|
|
36
|
-
result[key] = clone(
|
|
36
|
+
result[key] = clone(source[key], settings, refs);
|
|
37
37
|
}
|
|
38
38
|
return result;
|
|
39
39
|
}
|
|
40
|
-
if (
|
|
40
|
+
if (source instanceof Map) {
|
|
41
41
|
const result = /* @__PURE__ */ new Map();
|
|
42
|
-
refs.set(
|
|
43
|
-
for (const [key, value] of
|
|
42
|
+
refs.set(source, result);
|
|
43
|
+
for (const [key, value] of source) {
|
|
44
44
|
result.set(clone(key, settings, refs), clone(value, settings, refs));
|
|
45
45
|
}
|
|
46
46
|
return result;
|
|
47
47
|
}
|
|
48
|
-
if (
|
|
48
|
+
if (source instanceof Set) {
|
|
49
49
|
const result = /* @__PURE__ */ new Set();
|
|
50
|
-
refs.set(
|
|
51
|
-
for (const item of
|
|
50
|
+
refs.set(source, result);
|
|
51
|
+
for (const item of source) {
|
|
52
52
|
result.add(clone(item, settings, refs));
|
|
53
53
|
}
|
|
54
54
|
return result;
|
|
55
55
|
}
|
|
56
|
-
if (
|
|
57
|
-
const result = new Date(
|
|
58
|
-
refs.set(
|
|
56
|
+
if (source instanceof Date) {
|
|
57
|
+
const result = new Date(source.getTime());
|
|
58
|
+
refs.set(source, result);
|
|
59
59
|
return result;
|
|
60
60
|
}
|
|
61
|
-
if (
|
|
62
|
-
const result = new RegExp(
|
|
63
|
-
refs.set(
|
|
64
|
-
result.lastIndex =
|
|
61
|
+
if (source instanceof RegExp) {
|
|
62
|
+
const result = new RegExp(source.source, source.flags);
|
|
63
|
+
refs.set(source, result);
|
|
64
|
+
result.lastIndex = source.lastIndex;
|
|
65
65
|
return result;
|
|
66
66
|
}
|
|
67
|
-
if (
|
|
68
|
-
const result =
|
|
69
|
-
refs.set(
|
|
67
|
+
if (source instanceof ArrayBuffer) {
|
|
68
|
+
const result = source.slice(0);
|
|
69
|
+
refs.set(source, result);
|
|
70
70
|
return result;
|
|
71
71
|
}
|
|
72
|
-
if (ArrayBuffer.isView(
|
|
73
|
-
const { buffer, byteOffset, byteLength } =
|
|
74
|
-
if (
|
|
72
|
+
if (ArrayBuffer.isView(source)) {
|
|
73
|
+
const { buffer, byteOffset, byteLength } = source;
|
|
74
|
+
if (source instanceof DataView) {
|
|
75
75
|
const result2 = new DataView(buffer.slice(0), byteOffset, byteLength);
|
|
76
|
-
refs.set(
|
|
76
|
+
refs.set(source, result2);
|
|
77
77
|
return result2;
|
|
78
78
|
}
|
|
79
|
-
const Ctor =
|
|
79
|
+
const Ctor = source.constructor;
|
|
80
80
|
const result = new Ctor(buffer.slice(byteOffset, byteOffset + byteLength));
|
|
81
|
-
refs.set(
|
|
81
|
+
refs.set(source, result);
|
|
82
82
|
return result;
|
|
83
83
|
}
|
|
84
|
-
if (
|
|
85
|
-
return cloneError(
|
|
84
|
+
if (source instanceof DOMException || source instanceof Error) {
|
|
85
|
+
return cloneError(source, settings, refs);
|
|
86
86
|
}
|
|
87
|
-
if (
|
|
88
|
-
const result =
|
|
89
|
-
refs.set(
|
|
87
|
+
if (source instanceof Blob) {
|
|
88
|
+
const result = source.slice(0, source.size, source.type);
|
|
89
|
+
refs.set(source, result);
|
|
90
90
|
return result;
|
|
91
91
|
}
|
|
92
|
-
if (typeof ImageData !== "undefined" &&
|
|
93
|
-
const { data, width, height, colorSpace } =
|
|
92
|
+
if (typeof ImageData !== "undefined" && source instanceof ImageData) {
|
|
93
|
+
const { data, width, height, colorSpace } = source;
|
|
94
94
|
const result = new ImageData(new Uint8ClampedArray(data), width, height, {
|
|
95
95
|
colorSpace
|
|
96
96
|
});
|
|
97
|
-
refs.set(
|
|
97
|
+
refs.set(source, result);
|
|
98
98
|
return result;
|
|
99
99
|
}
|
|
100
|
-
if (
|
|
101
|
-
const result = new URL(
|
|
102
|
-
refs.set(
|
|
100
|
+
if (source instanceof URL) {
|
|
101
|
+
const result = new URL(source.href);
|
|
102
|
+
refs.set(source, result);
|
|
103
103
|
return result;
|
|
104
104
|
}
|
|
105
|
-
if (
|
|
105
|
+
if (source instanceof URLSearchParams) {
|
|
106
106
|
const result = new URLSearchParams();
|
|
107
|
-
refs.set(
|
|
108
|
-
for (const [key, value] of
|
|
107
|
+
refs.set(source, result);
|
|
108
|
+
for (const [key, value] of source) {
|
|
109
109
|
result.append(key, value);
|
|
110
110
|
}
|
|
111
111
|
return result;
|
|
112
112
|
}
|
|
113
|
-
refs.set(
|
|
114
|
-
return
|
|
113
|
+
refs.set(source, source);
|
|
114
|
+
return source;
|
|
115
115
|
}
|
|
116
|
-
function cloneError(
|
|
117
|
-
const result = createErrorInstance(
|
|
118
|
-
refs.set(
|
|
119
|
-
if (
|
|
116
|
+
function cloneError(source, settings, refs) {
|
|
117
|
+
const result = createErrorInstance(source, settings, refs);
|
|
118
|
+
refs.set(source, result);
|
|
119
|
+
if (source instanceof DOMException) {
|
|
120
120
|
return result;
|
|
121
121
|
}
|
|
122
|
-
if (
|
|
122
|
+
if (source.stack) {
|
|
123
123
|
try {
|
|
124
|
-
result.stack =
|
|
124
|
+
result.stack = source.stack;
|
|
125
125
|
} catch {
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
-
if ("cause" in
|
|
129
|
-
result.cause = clone(
|
|
128
|
+
if ("cause" in source && source.cause !== void 0) {
|
|
129
|
+
result.cause = clone(source.cause, settings, refs);
|
|
130
130
|
}
|
|
131
|
-
for (const key of Object.keys(
|
|
132
|
-
Reflect.set(result, key, clone(Reflect.get(
|
|
131
|
+
for (const key of Object.keys(source)) {
|
|
132
|
+
Reflect.set(result, key, clone(Reflect.get(source, key), settings, refs));
|
|
133
133
|
}
|
|
134
134
|
return result;
|
|
135
135
|
}
|
|
136
|
-
function cloneWithDescriptors(
|
|
137
|
-
const result = Object.create(Object.getPrototypeOf(
|
|
138
|
-
refs.set(
|
|
139
|
-
const descs = Object.getOwnPropertyDescriptors(
|
|
136
|
+
function cloneWithDescriptors(source, settings, refs) {
|
|
137
|
+
const result = Object.create(Object.getPrototypeOf(source));
|
|
138
|
+
refs.set(source, result);
|
|
139
|
+
const descs = Object.getOwnPropertyDescriptors(source);
|
|
140
140
|
forEachOwnKey(descs, (key) => {
|
|
141
141
|
if (isUnsafeKey(key)) {
|
|
142
142
|
return;
|
|
@@ -163,14 +163,14 @@ var ERROR_CTORS = {
|
|
|
163
163
|
TypeError,
|
|
164
164
|
URIError
|
|
165
165
|
};
|
|
166
|
-
function createErrorInstance(
|
|
167
|
-
const { message, name } =
|
|
168
|
-
if (
|
|
166
|
+
function createErrorInstance(source, settings, refs) {
|
|
167
|
+
const { message, name } = source;
|
|
168
|
+
if (source instanceof DOMException) {
|
|
169
169
|
return new DOMException(message, name);
|
|
170
170
|
}
|
|
171
|
-
if (
|
|
171
|
+
if (source instanceof AggregateError) {
|
|
172
172
|
return new AggregateError(
|
|
173
|
-
|
|
173
|
+
source.errors.map((e) => clone(e, settings, refs)),
|
|
174
174
|
message
|
|
175
175
|
);
|
|
176
176
|
}
|
package/dist/index.js
CHANGED
|
@@ -4,137 +4,137 @@ var { hasOwnProperty: HAS_OWN } = Object.prototype;
|
|
|
4
4
|
function bunshinClone(source, options = EMPTY_OPTIONS, refs = /* @__PURE__ */ new WeakMap()) {
|
|
5
5
|
return clone(source, options, refs);
|
|
6
6
|
}
|
|
7
|
-
function clone(
|
|
8
|
-
if (!isObject(
|
|
9
|
-
return
|
|
7
|
+
function clone(source, options, refs) {
|
|
8
|
+
if (!isObject(source)) {
|
|
9
|
+
return source;
|
|
10
10
|
}
|
|
11
|
-
const ref = refs.get(
|
|
11
|
+
const ref = refs.get(source);
|
|
12
12
|
if (ref !== void 0) {
|
|
13
13
|
return ref;
|
|
14
14
|
}
|
|
15
15
|
const settings = resolveOptions(options);
|
|
16
|
-
if (settings.preserveDescriptors && isPlainObject(
|
|
17
|
-
return cloneWithDescriptors(
|
|
16
|
+
if (settings.preserveDescriptors && isPlainObject(source)) {
|
|
17
|
+
return cloneWithDescriptors(source, settings, refs);
|
|
18
18
|
}
|
|
19
|
-
if (Array.isArray(
|
|
19
|
+
if (Array.isArray(source)) {
|
|
20
20
|
const result = [];
|
|
21
|
-
refs.set(
|
|
22
|
-
for (let i = 0, l =
|
|
23
|
-
result[i] = clone(
|
|
21
|
+
refs.set(source, result);
|
|
22
|
+
for (let i = 0, l = source.length; i < l; i++) {
|
|
23
|
+
result[i] = clone(source[i], settings, refs);
|
|
24
24
|
}
|
|
25
25
|
return result;
|
|
26
26
|
}
|
|
27
|
-
if (isPlainObject(
|
|
28
|
-
const result = Object.create(Object.getPrototypeOf(
|
|
29
|
-
refs.set(
|
|
30
|
-
for (const key in
|
|
31
|
-
if (!HAS_OWN.call(
|
|
27
|
+
if (isPlainObject(source)) {
|
|
28
|
+
const result = Object.create(Object.getPrototypeOf(source));
|
|
29
|
+
refs.set(source, result);
|
|
30
|
+
for (const key in source) {
|
|
31
|
+
if (!HAS_OWN.call(source, key) || isUnsafeKey(key)) {
|
|
32
32
|
continue;
|
|
33
33
|
}
|
|
34
|
-
result[key] = clone(
|
|
34
|
+
result[key] = clone(source[key], settings, refs);
|
|
35
35
|
}
|
|
36
36
|
return result;
|
|
37
37
|
}
|
|
38
|
-
if (
|
|
38
|
+
if (source instanceof Map) {
|
|
39
39
|
const result = /* @__PURE__ */ new Map();
|
|
40
|
-
refs.set(
|
|
41
|
-
for (const [key, value] of
|
|
40
|
+
refs.set(source, result);
|
|
41
|
+
for (const [key, value] of source) {
|
|
42
42
|
result.set(clone(key, settings, refs), clone(value, settings, refs));
|
|
43
43
|
}
|
|
44
44
|
return result;
|
|
45
45
|
}
|
|
46
|
-
if (
|
|
46
|
+
if (source instanceof Set) {
|
|
47
47
|
const result = /* @__PURE__ */ new Set();
|
|
48
|
-
refs.set(
|
|
49
|
-
for (const item of
|
|
48
|
+
refs.set(source, result);
|
|
49
|
+
for (const item of source) {
|
|
50
50
|
result.add(clone(item, settings, refs));
|
|
51
51
|
}
|
|
52
52
|
return result;
|
|
53
53
|
}
|
|
54
|
-
if (
|
|
55
|
-
const result = new Date(
|
|
56
|
-
refs.set(
|
|
54
|
+
if (source instanceof Date) {
|
|
55
|
+
const result = new Date(source.getTime());
|
|
56
|
+
refs.set(source, result);
|
|
57
57
|
return result;
|
|
58
58
|
}
|
|
59
|
-
if (
|
|
60
|
-
const result = new RegExp(
|
|
61
|
-
refs.set(
|
|
62
|
-
result.lastIndex =
|
|
59
|
+
if (source instanceof RegExp) {
|
|
60
|
+
const result = new RegExp(source.source, source.flags);
|
|
61
|
+
refs.set(source, result);
|
|
62
|
+
result.lastIndex = source.lastIndex;
|
|
63
63
|
return result;
|
|
64
64
|
}
|
|
65
|
-
if (
|
|
66
|
-
const result =
|
|
67
|
-
refs.set(
|
|
65
|
+
if (source instanceof ArrayBuffer) {
|
|
66
|
+
const result = source.slice(0);
|
|
67
|
+
refs.set(source, result);
|
|
68
68
|
return result;
|
|
69
69
|
}
|
|
70
|
-
if (ArrayBuffer.isView(
|
|
71
|
-
const { buffer, byteOffset, byteLength } =
|
|
72
|
-
if (
|
|
70
|
+
if (ArrayBuffer.isView(source)) {
|
|
71
|
+
const { buffer, byteOffset, byteLength } = source;
|
|
72
|
+
if (source instanceof DataView) {
|
|
73
73
|
const result2 = new DataView(buffer.slice(0), byteOffset, byteLength);
|
|
74
|
-
refs.set(
|
|
74
|
+
refs.set(source, result2);
|
|
75
75
|
return result2;
|
|
76
76
|
}
|
|
77
|
-
const Ctor =
|
|
77
|
+
const Ctor = source.constructor;
|
|
78
78
|
const result = new Ctor(buffer.slice(byteOffset, byteOffset + byteLength));
|
|
79
|
-
refs.set(
|
|
79
|
+
refs.set(source, result);
|
|
80
80
|
return result;
|
|
81
81
|
}
|
|
82
|
-
if (
|
|
83
|
-
return cloneError(
|
|
82
|
+
if (source instanceof DOMException || source instanceof Error) {
|
|
83
|
+
return cloneError(source, settings, refs);
|
|
84
84
|
}
|
|
85
|
-
if (
|
|
86
|
-
const result =
|
|
87
|
-
refs.set(
|
|
85
|
+
if (source instanceof Blob) {
|
|
86
|
+
const result = source.slice(0, source.size, source.type);
|
|
87
|
+
refs.set(source, result);
|
|
88
88
|
return result;
|
|
89
89
|
}
|
|
90
|
-
if (typeof ImageData !== "undefined" &&
|
|
91
|
-
const { data, width, height, colorSpace } =
|
|
90
|
+
if (typeof ImageData !== "undefined" && source instanceof ImageData) {
|
|
91
|
+
const { data, width, height, colorSpace } = source;
|
|
92
92
|
const result = new ImageData(new Uint8ClampedArray(data), width, height, {
|
|
93
93
|
colorSpace
|
|
94
94
|
});
|
|
95
|
-
refs.set(
|
|
95
|
+
refs.set(source, result);
|
|
96
96
|
return result;
|
|
97
97
|
}
|
|
98
|
-
if (
|
|
99
|
-
const result = new URL(
|
|
100
|
-
refs.set(
|
|
98
|
+
if (source instanceof URL) {
|
|
99
|
+
const result = new URL(source.href);
|
|
100
|
+
refs.set(source, result);
|
|
101
101
|
return result;
|
|
102
102
|
}
|
|
103
|
-
if (
|
|
103
|
+
if (source instanceof URLSearchParams) {
|
|
104
104
|
const result = new URLSearchParams();
|
|
105
|
-
refs.set(
|
|
106
|
-
for (const [key, value] of
|
|
105
|
+
refs.set(source, result);
|
|
106
|
+
for (const [key, value] of source) {
|
|
107
107
|
result.append(key, value);
|
|
108
108
|
}
|
|
109
109
|
return result;
|
|
110
110
|
}
|
|
111
|
-
refs.set(
|
|
112
|
-
return
|
|
111
|
+
refs.set(source, source);
|
|
112
|
+
return source;
|
|
113
113
|
}
|
|
114
|
-
function cloneError(
|
|
115
|
-
const result = createErrorInstance(
|
|
116
|
-
refs.set(
|
|
117
|
-
if (
|
|
114
|
+
function cloneError(source, settings, refs) {
|
|
115
|
+
const result = createErrorInstance(source, settings, refs);
|
|
116
|
+
refs.set(source, result);
|
|
117
|
+
if (source instanceof DOMException) {
|
|
118
118
|
return result;
|
|
119
119
|
}
|
|
120
|
-
if (
|
|
120
|
+
if (source.stack) {
|
|
121
121
|
try {
|
|
122
|
-
result.stack =
|
|
122
|
+
result.stack = source.stack;
|
|
123
123
|
} catch {
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
|
-
if ("cause" in
|
|
127
|
-
result.cause = clone(
|
|
126
|
+
if ("cause" in source && source.cause !== void 0) {
|
|
127
|
+
result.cause = clone(source.cause, settings, refs);
|
|
128
128
|
}
|
|
129
|
-
for (const key of Object.keys(
|
|
130
|
-
Reflect.set(result, key, clone(Reflect.get(
|
|
129
|
+
for (const key of Object.keys(source)) {
|
|
130
|
+
Reflect.set(result, key, clone(Reflect.get(source, key), settings, refs));
|
|
131
131
|
}
|
|
132
132
|
return result;
|
|
133
133
|
}
|
|
134
|
-
function cloneWithDescriptors(
|
|
135
|
-
const result = Object.create(Object.getPrototypeOf(
|
|
136
|
-
refs.set(
|
|
137
|
-
const descs = Object.getOwnPropertyDescriptors(
|
|
134
|
+
function cloneWithDescriptors(source, settings, refs) {
|
|
135
|
+
const result = Object.create(Object.getPrototypeOf(source));
|
|
136
|
+
refs.set(source, result);
|
|
137
|
+
const descs = Object.getOwnPropertyDescriptors(source);
|
|
138
138
|
forEachOwnKey(descs, (key) => {
|
|
139
139
|
if (isUnsafeKey(key)) {
|
|
140
140
|
return;
|
|
@@ -161,14 +161,14 @@ var ERROR_CTORS = {
|
|
|
161
161
|
TypeError,
|
|
162
162
|
URIError
|
|
163
163
|
};
|
|
164
|
-
function createErrorInstance(
|
|
165
|
-
const { message, name } =
|
|
166
|
-
if (
|
|
164
|
+
function createErrorInstance(source, settings, refs) {
|
|
165
|
+
const { message, name } = source;
|
|
166
|
+
if (source instanceof DOMException) {
|
|
167
167
|
return new DOMException(message, name);
|
|
168
168
|
}
|
|
169
|
-
if (
|
|
169
|
+
if (source instanceof AggregateError) {
|
|
170
170
|
return new AggregateError(
|
|
171
|
-
|
|
171
|
+
source.errors.map((e) => clone(e, settings, refs)),
|
|
172
172
|
message
|
|
173
173
|
);
|
|
174
174
|
}
|