@vitest/utils 3.1.0-beta.1 → 3.1.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/chunk-_commonjsHelpers.js +138 -143
- package/dist/diff.js +1062 -1129
- package/dist/error.js +110 -117
- package/dist/helpers.d.ts +2 -1
- package/dist/helpers.js +199 -181
- package/dist/index.d.ts +2 -1
- package/dist/index.js +94 -114
- package/dist/source-map.d.ts +2 -1
- package/dist/source-map.js +169 -166
- package/package.json +3 -3
package/dist/helpers.js
CHANGED
@@ -1,224 +1,242 @@
|
|
1
|
+
/**
|
2
|
+
* Get original stacktrace without source map support the most performant way.
|
3
|
+
* - Create only 1 stack frame.
|
4
|
+
* - Rewrite prepareStackTrace to bypass "support-stack-trace" (usually takes ~250ms).
|
5
|
+
*/
|
1
6
|
function createSimpleStackTrace(options) {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
const { message = "$$stack trace error", stackTraceLimit = 1 } = options || {};
|
8
|
+
const limit = Error.stackTraceLimit;
|
9
|
+
const prepareStackTrace = Error.prepareStackTrace;
|
10
|
+
Error.stackTraceLimit = stackTraceLimit;
|
11
|
+
Error.prepareStackTrace = (e) => e.stack;
|
12
|
+
const err = new Error(message);
|
13
|
+
const stackTrace = err.stack || "";
|
14
|
+
Error.prepareStackTrace = prepareStackTrace;
|
15
|
+
Error.stackTraceLimit = limit;
|
16
|
+
return stackTrace;
|
12
17
|
}
|
13
18
|
function notNullish(v) {
|
14
|
-
|
19
|
+
return v != null;
|
15
20
|
}
|
16
21
|
function assertTypes(value, name, types) {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
);
|
23
|
-
}
|
22
|
+
const receivedType = typeof value;
|
23
|
+
const pass = types.includes(receivedType);
|
24
|
+
if (!pass) {
|
25
|
+
throw new TypeError(`${name} value must be ${types.join(" or ")}, received "${receivedType}"`);
|
26
|
+
}
|
24
27
|
}
|
25
28
|
function isPrimitive(value) {
|
26
|
-
|
29
|
+
return value === null || typeof value !== "function" && typeof value !== "object";
|
27
30
|
}
|
28
31
|
function slash(path) {
|
29
|
-
|
32
|
+
return path.replace(/\\/g, "/");
|
30
33
|
}
|
31
34
|
function parseRegexp(input) {
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
const m = input.match(/(\/?)(.+)\1([a-z]*)/i);
|
36
|
+
if (!m) {
|
37
|
+
return /$^/;
|
38
|
+
}
|
39
|
+
if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) {
|
40
|
+
return new RegExp(input);
|
41
|
+
}
|
42
|
+
return new RegExp(m[2], m[3]);
|
40
43
|
}
|
41
44
|
function toArray(array) {
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
if (array === null || array === undefined) {
|
46
|
+
array = [];
|
47
|
+
}
|
48
|
+
if (Array.isArray(array)) {
|
49
|
+
return array;
|
50
|
+
}
|
51
|
+
return [array];
|
49
52
|
}
|
50
53
|
function isObject(item) {
|
51
|
-
|
54
|
+
return item != null && typeof item === "object" && !Array.isArray(item);
|
52
55
|
}
|
53
56
|
function isFinalObj(obj) {
|
54
|
-
|
57
|
+
return obj === Object.prototype || obj === Function.prototype || obj === RegExp.prototype;
|
55
58
|
}
|
56
59
|
function getType(value) {
|
57
|
-
|
60
|
+
return Object.prototype.toString.apply(value).slice(8, -1);
|
58
61
|
}
|
59
62
|
function collectOwnProperties(obj, collector) {
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
+
const collect = typeof collector === "function" ? collector : (key) => collector.add(key);
|
64
|
+
Object.getOwnPropertyNames(obj).forEach(collect);
|
65
|
+
Object.getOwnPropertySymbols(obj).forEach(collect);
|
63
66
|
}
|
64
67
|
function getOwnProperties(obj) {
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
68
|
+
const ownProps = new Set();
|
69
|
+
if (isFinalObj(obj)) {
|
70
|
+
return [];
|
71
|
+
}
|
72
|
+
collectOwnProperties(obj, ownProps);
|
73
|
+
return Array.from(ownProps);
|
71
74
|
}
|
72
75
|
const defaultCloneOptions = { forceWritable: false };
|
73
76
|
function deepClone(val, options = defaultCloneOptions) {
|
74
|
-
|
75
|
-
|
77
|
+
const seen = new WeakMap();
|
78
|
+
return clone(val, seen, options);
|
76
79
|
}
|
77
80
|
function clone(val, seen, options = defaultCloneOptions) {
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
}
|
125
|
-
function noop() {
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
return result;
|
81
|
+
let k, out;
|
82
|
+
if (seen.has(val)) {
|
83
|
+
return seen.get(val);
|
84
|
+
}
|
85
|
+
if (Array.isArray(val)) {
|
86
|
+
out = Array.from({ length: k = val.length });
|
87
|
+
seen.set(val, out);
|
88
|
+
while (k--) {
|
89
|
+
out[k] = clone(val[k], seen, options);
|
90
|
+
}
|
91
|
+
return out;
|
92
|
+
}
|
93
|
+
if (Object.prototype.toString.call(val) === "[object Object]") {
|
94
|
+
out = Object.create(Object.getPrototypeOf(val));
|
95
|
+
seen.set(val, out);
|
96
|
+
const props = getOwnProperties(val);
|
97
|
+
for (const k of props) {
|
98
|
+
const descriptor = Object.getOwnPropertyDescriptor(val, k);
|
99
|
+
if (!descriptor) {
|
100
|
+
continue;
|
101
|
+
}
|
102
|
+
const cloned = clone(val[k], seen, options);
|
103
|
+
if (options.forceWritable) {
|
104
|
+
Object.defineProperty(out, k, {
|
105
|
+
enumerable: descriptor.enumerable,
|
106
|
+
configurable: true,
|
107
|
+
writable: true,
|
108
|
+
value: cloned
|
109
|
+
});
|
110
|
+
} else if ("get" in descriptor) {
|
111
|
+
Object.defineProperty(out, k, {
|
112
|
+
...descriptor,
|
113
|
+
get() {
|
114
|
+
return cloned;
|
115
|
+
}
|
116
|
+
});
|
117
|
+
} else {
|
118
|
+
Object.defineProperty(out, k, {
|
119
|
+
...descriptor,
|
120
|
+
value: cloned
|
121
|
+
});
|
122
|
+
}
|
123
|
+
}
|
124
|
+
return out;
|
125
|
+
}
|
126
|
+
return val;
|
127
|
+
}
|
128
|
+
function noop() {}
|
129
|
+
function objectAttr(source, path, defaultValue = undefined) {
|
130
|
+
const paths = path.replace(/\[(\d+)\]/g, ".$1").split(".");
|
131
|
+
let result = source;
|
132
|
+
for (const p of paths) {
|
133
|
+
result = new Object(result)[p];
|
134
|
+
if (result === undefined) {
|
135
|
+
return defaultValue;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
return result;
|
137
139
|
}
|
138
140
|
function createDefer() {
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
}
|
141
|
+
let resolve = null;
|
142
|
+
let reject = null;
|
143
|
+
const p = new Promise((_resolve, _reject) => {
|
144
|
+
resolve = _resolve;
|
145
|
+
reject = _reject;
|
146
|
+
});
|
147
|
+
p.resolve = resolve;
|
148
|
+
p.reject = reject;
|
149
|
+
return p;
|
150
|
+
}
|
151
|
+
/**
|
152
|
+
* If code starts with a function call, will return its last index, respecting arguments.
|
153
|
+
* This will return 25 - last ending character of toMatch ")"
|
154
|
+
* Also works with callbacks
|
155
|
+
* ```
|
156
|
+
* toMatch({ test: '123' });
|
157
|
+
* toBeAliased('123')
|
158
|
+
* ```
|
159
|
+
*/
|
149
160
|
function getCallLastIndex(code) {
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
161
|
+
let charIndex = -1;
|
162
|
+
let inString = null;
|
163
|
+
let startedBracers = 0;
|
164
|
+
let endedBracers = 0;
|
165
|
+
let beforeChar = null;
|
166
|
+
while (charIndex <= code.length) {
|
167
|
+
beforeChar = code[charIndex];
|
168
|
+
charIndex++;
|
169
|
+
const char = code[charIndex];
|
170
|
+
const isCharString = char === "\"" || char === "'" || char === "`";
|
171
|
+
if (isCharString && beforeChar !== "\\") {
|
172
|
+
if (inString === char) {
|
173
|
+
inString = null;
|
174
|
+
} else if (!inString) {
|
175
|
+
inString = char;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
if (!inString) {
|
179
|
+
if (char === "(") {
|
180
|
+
startedBracers++;
|
181
|
+
}
|
182
|
+
if (char === ")") {
|
183
|
+
endedBracers++;
|
184
|
+
}
|
185
|
+
}
|
186
|
+
if (startedBracers && endedBracers && startedBracers === endedBracers) {
|
187
|
+
return charIndex;
|
188
|
+
}
|
189
|
+
}
|
190
|
+
return null;
|
180
191
|
}
|
181
192
|
function isNegativeNaN(val) {
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
193
|
+
if (!Number.isNaN(val)) {
|
194
|
+
return false;
|
195
|
+
}
|
196
|
+
const f64 = new Float64Array(1);
|
197
|
+
f64[0] = val;
|
198
|
+
const u32 = new Uint32Array(f64.buffer);
|
199
|
+
const isNegative = u32[1] >>> 31 === 1;
|
200
|
+
return isNegative;
|
190
201
|
}
|
191
202
|
function toString(v) {
|
192
|
-
|
203
|
+
return Object.prototype.toString.call(v);
|
193
204
|
}
|
194
205
|
function isPlainObject(val) {
|
195
|
-
|
206
|
+
return toString(val) === "[object Object]" && (!val.constructor || val.constructor.name === "Object");
|
196
207
|
}
|
197
208
|
function isMergeableObject(item) {
|
198
|
-
|
199
|
-
}
|
209
|
+
return isPlainObject(item) && !Array.isArray(item);
|
210
|
+
}
|
211
|
+
/**
|
212
|
+
* Deep merge :P
|
213
|
+
*
|
214
|
+
* Will merge objects only if they are plain
|
215
|
+
*
|
216
|
+
* Do not merge types - it is very expensive and usually it's better to case a type here
|
217
|
+
*/
|
200
218
|
function deepMerge(target, ...sources) {
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
219
|
+
if (!sources.length) {
|
220
|
+
return target;
|
221
|
+
}
|
222
|
+
const source = sources.shift();
|
223
|
+
if (source === undefined) {
|
224
|
+
return target;
|
225
|
+
}
|
226
|
+
if (isMergeableObject(target) && isMergeableObject(source)) {
|
227
|
+
Object.keys(source).forEach((key) => {
|
228
|
+
const _source = source;
|
229
|
+
if (isMergeableObject(_source[key])) {
|
230
|
+
if (!target[key]) {
|
231
|
+
target[key] = {};
|
232
|
+
}
|
233
|
+
deepMerge(target[key], _source[key]);
|
234
|
+
} else {
|
235
|
+
target[key] = _source[key];
|
236
|
+
}
|
237
|
+
});
|
238
|
+
}
|
239
|
+
return deepMerge(target, ...sources);
|
222
240
|
}
|
223
241
|
|
224
242
|
export { assertTypes, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, getCallLastIndex, getOwnProperties, getType, isNegativeNaN, isObject, isPrimitive, noop, notNullish, objectAttr, parseRegexp, slash, toArray };
|
package/dist/index.d.ts
CHANGED
@@ -53,4 +53,5 @@ interface SafeTimers {
|
|
53
53
|
declare function getSafeTimers(): SafeTimers;
|
54
54
|
declare function setSafeTimers(): void;
|
55
55
|
|
56
|
-
export {
|
56
|
+
export { format, getSafeTimers, highlight, inspect, lineSplitRE, nanoid, objDisplay, offsetToLineNumber, positionToOffset, setSafeTimers, shuffle, stringify };
|
57
|
+
export type { SafeTimers, StringifyOptions };
|