@vitest/utils 0.27.2 → 0.28.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/diff.d.ts +4 -1
- package/dist/diff.js +4 -5
- package/dist/helpers.d.ts +16 -1
- package/dist/helpers.js +77 -1
- package/dist/index.d.ts +23 -3
- package/dist/index.js +127 -9
- package/package.json +2 -1
package/dist/diff.d.ts
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
declare function formatLine(line: string, outputTruncateLength?: number): string;
|
2
|
+
type Color = (str: string) => string;
|
2
3
|
interface DiffOptions {
|
3
|
-
noColor?: boolean;
|
4
4
|
outputDiffMaxLines?: number;
|
5
5
|
outputTruncateLength?: number;
|
6
6
|
outputDiffLines?: number;
|
7
7
|
showLegend?: boolean;
|
8
|
+
colorSuccess?: Color;
|
9
|
+
colorError?: Color;
|
10
|
+
colorDim?: Color;
|
8
11
|
}
|
9
12
|
/**
|
10
13
|
* Returns unified diff between two strings with coloured ANSI output.
|
package/dist/diff.js
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
import c from 'picocolors';
|
2
1
|
import * as diff from 'diff';
|
3
2
|
import cliTruncate from 'cli-truncate';
|
4
3
|
|
@@ -9,7 +8,7 @@ function formatLine(line, outputTruncateLength) {
|
|
9
8
|
function unifiedDiff(actual, expected, options = {}) {
|
10
9
|
if (actual === expected)
|
11
10
|
return "";
|
12
|
-
const { outputTruncateLength, outputDiffLines, outputDiffMaxLines,
|
11
|
+
const { outputTruncateLength, outputDiffLines, outputDiffMaxLines, showLegend = true } = options;
|
13
12
|
const indent = " ";
|
14
13
|
const diffLimit = outputDiffLines || 15;
|
15
14
|
const diffMaxLines = outputDiffMaxLines || 50;
|
@@ -20,9 +19,9 @@ function unifiedDiff(actual, expected, options = {}) {
|
|
20
19
|
let previousState = null;
|
21
20
|
let previousCount = 0;
|
22
21
|
const str = (str2) => str2;
|
23
|
-
const dim =
|
24
|
-
const green =
|
25
|
-
const red =
|
22
|
+
const dim = options.colorDim || str;
|
23
|
+
const green = options.colorSuccess || str;
|
24
|
+
const red = options.colorError || str;
|
26
25
|
function preprocess(line) {
|
27
26
|
if (!line || line.match(/\\ No newline/))
|
28
27
|
return;
|
package/dist/helpers.d.ts
CHANGED
@@ -1,4 +1,19 @@
|
|
1
|
+
import { Nullable, Arrayable } from './types.js';
|
2
|
+
|
1
3
|
declare function assertTypes(value: unknown, name: string, types: string[]): void;
|
4
|
+
declare function slash(path: string): string;
|
5
|
+
declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
|
2
6
|
declare function isObject(item: unknown): boolean;
|
7
|
+
declare function getType(value: unknown): string;
|
8
|
+
declare function getOwnProperties(obj: any): (string | symbol)[];
|
9
|
+
declare function deepClone<T>(val: T): T;
|
10
|
+
declare function clone<T>(val: T, seen: WeakMap<any, any>): T;
|
11
|
+
declare function noop(): void;
|
12
|
+
declare function objectAttr(source: any, path: string, defaultValue?: undefined): any;
|
13
|
+
type DeferPromise<T> = Promise<T> & {
|
14
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
15
|
+
reject: (reason?: any) => void;
|
16
|
+
};
|
17
|
+
declare function createDefer<T>(): DeferPromise<T>;
|
3
18
|
|
4
|
-
export { assertTypes, isObject };
|
19
|
+
export { assertTypes, clone, createDefer, deepClone, getOwnProperties, getType, isObject, noop, objectAttr, slash, toArray };
|
package/dist/helpers.js
CHANGED
@@ -4,8 +4,84 @@ function assertTypes(value, name, types) {
|
|
4
4
|
if (!pass)
|
5
5
|
throw new TypeError(`${name} value must be ${types.join(" or ")}, received "${receivedType}"`);
|
6
6
|
}
|
7
|
+
function slash(path) {
|
8
|
+
return path.replace(/\\/g, "/");
|
9
|
+
}
|
10
|
+
function toArray(array) {
|
11
|
+
if (array === null || array === void 0)
|
12
|
+
array = [];
|
13
|
+
if (Array.isArray(array))
|
14
|
+
return array;
|
15
|
+
return [array];
|
16
|
+
}
|
7
17
|
function isObject(item) {
|
8
18
|
return item != null && typeof item === "object" && !Array.isArray(item);
|
9
19
|
}
|
20
|
+
function isFinalObj(obj) {
|
21
|
+
return obj === Object.prototype || obj === Function.prototype || obj === RegExp.prototype;
|
22
|
+
}
|
23
|
+
function getType(value) {
|
24
|
+
return Object.prototype.toString.apply(value).slice(8, -1);
|
25
|
+
}
|
26
|
+
function collectOwnProperties(obj, collector) {
|
27
|
+
const collect = typeof collector === "function" ? collector : (key) => collector.add(key);
|
28
|
+
Object.getOwnPropertyNames(obj).forEach(collect);
|
29
|
+
Object.getOwnPropertySymbols(obj).forEach(collect);
|
30
|
+
}
|
31
|
+
function getOwnProperties(obj) {
|
32
|
+
const ownProps = /* @__PURE__ */ new Set();
|
33
|
+
if (isFinalObj(obj))
|
34
|
+
return [];
|
35
|
+
collectOwnProperties(obj, ownProps);
|
36
|
+
return Array.from(ownProps);
|
37
|
+
}
|
38
|
+
function deepClone(val) {
|
39
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
40
|
+
return clone(val, seen);
|
41
|
+
}
|
42
|
+
function clone(val, seen) {
|
43
|
+
let k, out;
|
44
|
+
if (seen.has(val))
|
45
|
+
return seen.get(val);
|
46
|
+
if (Array.isArray(val)) {
|
47
|
+
out = Array(k = val.length);
|
48
|
+
seen.set(val, out);
|
49
|
+
while (k--)
|
50
|
+
out[k] = clone(val[k], seen);
|
51
|
+
return out;
|
52
|
+
}
|
53
|
+
if (Object.prototype.toString.call(val) === "[object Object]") {
|
54
|
+
out = Object.create(Object.getPrototypeOf(val));
|
55
|
+
seen.set(val, out);
|
56
|
+
const props = getOwnProperties(val);
|
57
|
+
for (const k2 of props)
|
58
|
+
out[k2] = clone(val[k2], seen);
|
59
|
+
return out;
|
60
|
+
}
|
61
|
+
return val;
|
62
|
+
}
|
63
|
+
function noop() {
|
64
|
+
}
|
65
|
+
function objectAttr(source, path, defaultValue = void 0) {
|
66
|
+
const paths = path.replace(/\[(\d+)\]/g, ".$1").split(".");
|
67
|
+
let result = source;
|
68
|
+
for (const p of paths) {
|
69
|
+
result = Object(result)[p];
|
70
|
+
if (result === void 0)
|
71
|
+
return defaultValue;
|
72
|
+
}
|
73
|
+
return result;
|
74
|
+
}
|
75
|
+
function createDefer() {
|
76
|
+
let resolve = null;
|
77
|
+
let reject = null;
|
78
|
+
const p = new Promise((_resolve, _reject) => {
|
79
|
+
resolve = _resolve;
|
80
|
+
reject = _reject;
|
81
|
+
});
|
82
|
+
p.resolve = resolve;
|
83
|
+
p.reject = reject;
|
84
|
+
return p;
|
85
|
+
}
|
10
86
|
|
11
|
-
export { assertTypes, isObject };
|
87
|
+
export { assertTypes, clone, createDefer, deepClone, getOwnProperties, getType, isObject, noop, objectAttr, slash, toArray };
|
package/dist/index.d.ts
CHANGED
@@ -1,10 +1,30 @@
|
|
1
|
-
export {
|
2
|
-
export { assertTypes, isObject } from './helpers.js';
|
1
|
+
export { assertTypes, clone, createDefer, deepClone, getOwnProperties, getType, isObject, noop, objectAttr, slash, toArray } from './helpers.js';
|
3
2
|
export { ArgumentsType, Arrayable, Awaitable, Constructable, DeepMerge, MergeInsertions, MutableArray, Nullable } from './types.js';
|
4
3
|
import { PrettyFormatOptions } from 'pretty-format';
|
4
|
+
import p from 'picocolors';
|
5
5
|
|
6
6
|
declare function stringify(object: unknown, maxDepth?: number, { maxLength, ...options }?: PrettyFormatOptions & {
|
7
7
|
maxLength?: number;
|
8
8
|
}): string;
|
9
9
|
|
10
|
-
|
10
|
+
declare function getSafeTimers(): {
|
11
|
+
setTimeout: any;
|
12
|
+
setInterval: any;
|
13
|
+
clearInterval: any;
|
14
|
+
clearTimeout: any;
|
15
|
+
};
|
16
|
+
declare function setSafeTimers(): void;
|
17
|
+
|
18
|
+
declare function shuffle<T>(array: T[], seed?: number): T[];
|
19
|
+
|
20
|
+
declare function format(...args: any[]): string;
|
21
|
+
declare function inspect(obj: unknown): string;
|
22
|
+
declare function objDisplay(obj: unknown): string;
|
23
|
+
|
24
|
+
declare const SAFE_TIMERS_SYMBOL: unique symbol;
|
25
|
+
declare const SAFE_COLORS_SYMBOL: unique symbol;
|
26
|
+
|
27
|
+
declare function getColors(): typeof p;
|
28
|
+
declare function setColors(colors: typeof p): void;
|
29
|
+
|
30
|
+
export { SAFE_COLORS_SYMBOL, SAFE_TIMERS_SYMBOL, format, getColors, getSafeTimers, inspect, objDisplay, setColors, setSafeTimers, shuffle, stringify };
|
package/dist/index.js
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
export {
|
2
|
-
|
3
|
-
import
|
4
|
-
import '
|
5
|
-
import 'diff';
|
6
|
-
import 'cli-truncate';
|
1
|
+
export { assertTypes, clone, createDefer, deepClone, getOwnProperties, getType, isObject, noop, objectAttr, slash, toArray } from './helpers.js';
|
2
|
+
import { format as format$1, plugins } from 'pretty-format';
|
3
|
+
import util from 'util';
|
4
|
+
import loupeImport from 'loupe';
|
7
5
|
|
8
6
|
const {
|
9
7
|
AsymmetricMatcher,
|
@@ -25,14 +23,14 @@ function stringify(object, maxDepth = 10, { maxLength, ...options } = {}) {
|
|
25
23
|
const MAX_LENGTH = maxLength ?? 1e4;
|
26
24
|
let result;
|
27
25
|
try {
|
28
|
-
result = format(object, {
|
26
|
+
result = format$1(object, {
|
29
27
|
maxDepth,
|
30
28
|
escapeString: false,
|
31
29
|
plugins: PLUGINS,
|
32
30
|
...options
|
33
31
|
});
|
34
32
|
} catch {
|
35
|
-
result = format(object, {
|
33
|
+
result = format$1(object, {
|
36
34
|
callToJSON: false,
|
37
35
|
maxDepth,
|
38
36
|
escapeString: false,
|
@@ -43,4 +41,124 @@ function stringify(object, maxDepth = 10, { maxLength, ...options } = {}) {
|
|
43
41
|
return result.length >= MAX_LENGTH && maxDepth > 1 ? stringify(object, Math.floor(maxDepth / 2)) : result;
|
44
42
|
}
|
45
43
|
|
46
|
-
|
44
|
+
const SAFE_TIMERS_SYMBOL = Symbol("vitest:SAFE_TIMERS");
|
45
|
+
const SAFE_COLORS_SYMBOL = Symbol("vitest:SAFE_COLORS");
|
46
|
+
|
47
|
+
function getSafeTimers() {
|
48
|
+
const {
|
49
|
+
setTimeout: safeSetTimeout,
|
50
|
+
setInterval: safeSetInterval,
|
51
|
+
clearInterval: safeClearInterval,
|
52
|
+
clearTimeout: safeClearTimeout
|
53
|
+
} = globalThis[SAFE_TIMERS_SYMBOL] || globalThis;
|
54
|
+
return {
|
55
|
+
setTimeout: safeSetTimeout,
|
56
|
+
setInterval: safeSetInterval,
|
57
|
+
clearInterval: safeClearInterval,
|
58
|
+
clearTimeout: safeClearTimeout
|
59
|
+
};
|
60
|
+
}
|
61
|
+
function setSafeTimers() {
|
62
|
+
const {
|
63
|
+
setTimeout: safeSetTimeout,
|
64
|
+
setInterval: safeSetInterval,
|
65
|
+
clearInterval: safeClearInterval,
|
66
|
+
clearTimeout: safeClearTimeout
|
67
|
+
} = globalThis;
|
68
|
+
const timers = {
|
69
|
+
setTimeout: safeSetTimeout,
|
70
|
+
setInterval: safeSetInterval,
|
71
|
+
clearInterval: safeClearInterval,
|
72
|
+
clearTimeout: safeClearTimeout
|
73
|
+
};
|
74
|
+
globalThis[SAFE_TIMERS_SYMBOL] = timers;
|
75
|
+
}
|
76
|
+
|
77
|
+
const RealDate = Date;
|
78
|
+
function random(seed) {
|
79
|
+
const x = Math.sin(seed++) * 1e4;
|
80
|
+
return x - Math.floor(x);
|
81
|
+
}
|
82
|
+
function shuffle(array, seed = RealDate.now()) {
|
83
|
+
let length = array.length;
|
84
|
+
while (length) {
|
85
|
+
const index = Math.floor(random(seed) * length--);
|
86
|
+
const previous = array[length];
|
87
|
+
array[length] = array[index];
|
88
|
+
array[index] = previous;
|
89
|
+
++seed;
|
90
|
+
}
|
91
|
+
return array;
|
92
|
+
}
|
93
|
+
|
94
|
+
const loupe = typeof loupeImport.default === "function" ? loupeImport.default : loupeImport;
|
95
|
+
function format(...args) {
|
96
|
+
return util.format(...args);
|
97
|
+
}
|
98
|
+
function inspect(obj) {
|
99
|
+
return loupe(obj, {
|
100
|
+
depth: 2,
|
101
|
+
truncate: 40
|
102
|
+
});
|
103
|
+
}
|
104
|
+
function objDisplay(obj) {
|
105
|
+
const truncateThreshold = 40;
|
106
|
+
const str = inspect(obj);
|
107
|
+
const type = Object.prototype.toString.call(obj);
|
108
|
+
if (str.length >= truncateThreshold) {
|
109
|
+
if (type === "[object Function]") {
|
110
|
+
const fn = obj;
|
111
|
+
return !fn.name || fn.name === "" ? "[Function]" : `[Function: ${fn.name}]`;
|
112
|
+
} else if (type === "[object Array]") {
|
113
|
+
return `[ Array(${obj.length}) ]`;
|
114
|
+
} else if (type === "[object Object]") {
|
115
|
+
const keys = Object.keys(obj);
|
116
|
+
const kstr = keys.length > 2 ? `${keys.splice(0, 2).join(", ")}, ...` : keys.join(", ");
|
117
|
+
return `{ Object (${kstr}) }`;
|
118
|
+
} else {
|
119
|
+
return str;
|
120
|
+
}
|
121
|
+
}
|
122
|
+
return str;
|
123
|
+
}
|
124
|
+
|
125
|
+
const colors = [
|
126
|
+
"reset",
|
127
|
+
"bold",
|
128
|
+
"dim",
|
129
|
+
"italic",
|
130
|
+
"underline",
|
131
|
+
"inverse",
|
132
|
+
"hidden",
|
133
|
+
"strikethrough",
|
134
|
+
"black",
|
135
|
+
"red",
|
136
|
+
"green",
|
137
|
+
"yellow",
|
138
|
+
"blue",
|
139
|
+
"magenta",
|
140
|
+
"cyan",
|
141
|
+
"white",
|
142
|
+
"gray",
|
143
|
+
"bgBlack",
|
144
|
+
"bgRed",
|
145
|
+
"bgGreen",
|
146
|
+
"bgYellow",
|
147
|
+
"bgBlue",
|
148
|
+
"bgMagenta",
|
149
|
+
"bgCyan",
|
150
|
+
"bgWhite"
|
151
|
+
];
|
152
|
+
const formatter = (str) => String(str);
|
153
|
+
const defaultColors = colors.reduce((acc, key) => {
|
154
|
+
acc[key] = formatter;
|
155
|
+
return acc;
|
156
|
+
}, { isColorSupported: false });
|
157
|
+
function getColors() {
|
158
|
+
return globalThis[SAFE_COLORS_SYMBOL] || defaultColors;
|
159
|
+
}
|
160
|
+
function setColors(colors2) {
|
161
|
+
globalThis[SAFE_COLORS_SYMBOL] = colors2;
|
162
|
+
}
|
163
|
+
|
164
|
+
export { SAFE_COLORS_SYMBOL, SAFE_TIMERS_SYMBOL, format, getColors, getSafeTimers, inspect, objDisplay, setColors, setSafeTimers, shuffle, stringify };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.28.0",
|
5
5
|
"description": "Shared Vitest utility functions",
|
6
6
|
"license": "MIT",
|
7
7
|
"repository": {
|
@@ -34,6 +34,7 @@
|
|
34
34
|
"dependencies": {
|
35
35
|
"cli-truncate": "^3.1.0",
|
36
36
|
"diff": "^5.1.0",
|
37
|
+
"loupe": "^2.3.6",
|
37
38
|
"picocolors": "^1.0.0",
|
38
39
|
"pretty-format": "^27.5.1"
|
39
40
|
},
|