@vitest/utils 0.27.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021-Present Anthony Fu <https://github.com/antfu>
4
+ Copyright (c) 2021-Present Matias Capeletto <https://github.com/patak-dev>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/dist/diff.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ declare function formatLine(line: string, outputTruncateLength?: number): string;
2
+ interface DiffOptions {
3
+ noColor?: boolean;
4
+ outputDiffMaxLines?: number;
5
+ outputTruncateLength?: number;
6
+ outputDiffLines?: number;
7
+ showLegend?: boolean;
8
+ }
9
+ /**
10
+ * Returns unified diff between two strings with coloured ANSI output.
11
+ *
12
+ * @private
13
+ * @param {String} actual
14
+ * @param {String} expected
15
+ * @return {string} The diff.
16
+ */
17
+ declare function unifiedDiff(actual: string, expected: string, options?: DiffOptions): string;
18
+
19
+ export { DiffOptions, formatLine, unifiedDiff };
package/dist/diff.js ADDED
@@ -0,0 +1,98 @@
1
+ import c from 'picocolors';
2
+ import * as diff from 'diff';
3
+ import cliTruncate from 'cli-truncate';
4
+
5
+ function formatLine(line, outputTruncateLength) {
6
+ var _a;
7
+ return cliTruncate(line, (outputTruncateLength ?? (((_a = process.stdout) == null ? void 0 : _a.columns) || 80)) - 4);
8
+ }
9
+ function unifiedDiff(actual, expected, options = {}) {
10
+ if (actual === expected)
11
+ return "";
12
+ const { outputTruncateLength, outputDiffLines, outputDiffMaxLines, noColor, showLegend = true } = options;
13
+ const indent = " ";
14
+ const diffLimit = outputDiffLines || 15;
15
+ const diffMaxLines = outputDiffMaxLines || 50;
16
+ const counts = {
17
+ "+": 0,
18
+ "-": 0
19
+ };
20
+ let previousState = null;
21
+ let previousCount = 0;
22
+ const str = (str2) => str2;
23
+ const dim = noColor ? str : c.dim;
24
+ const green = noColor ? str : c.green;
25
+ const red = noColor ? str : c.red;
26
+ function preprocess(line) {
27
+ if (!line || line.match(/\\ No newline/))
28
+ return;
29
+ const char = line[0];
30
+ if ("-+".includes(char)) {
31
+ if (previousState !== char) {
32
+ previousState = char;
33
+ previousCount = 0;
34
+ }
35
+ previousCount++;
36
+ counts[char]++;
37
+ if (previousCount === diffLimit)
38
+ return dim(`${char} ...`);
39
+ else if (previousCount > diffLimit)
40
+ return;
41
+ }
42
+ return line;
43
+ }
44
+ const msg = diff.createPatch("string", expected, actual);
45
+ let lines = msg.split("\n").slice(5).map(preprocess).filter(Boolean);
46
+ let moreLines = 0;
47
+ const isCompact = counts["+"] === 1 && counts["-"] === 1 && lines.length === 2;
48
+ if (lines.length > diffMaxLines) {
49
+ const firstDiff = lines.findIndex((line) => line[0] === "-" || line[0] === "+");
50
+ const displayLines = lines.slice(firstDiff - 2, diffMaxLines);
51
+ const lastDisplayedIndex = firstDiff - 2 + diffMaxLines;
52
+ if (lastDisplayedIndex < lines.length)
53
+ moreLines = lines.length - lastDisplayedIndex;
54
+ lines = displayLines;
55
+ }
56
+ let formatted = lines.map((line) => {
57
+ line = line.replace(/\\"/g, '"');
58
+ if (line[0] === "-") {
59
+ line = formatLine(line.slice(1), outputTruncateLength);
60
+ if (isCompact)
61
+ return green(line);
62
+ return green(`- ${formatLine(line, outputTruncateLength)}`);
63
+ }
64
+ if (line[0] === "+") {
65
+ line = formatLine(line.slice(1), outputTruncateLength);
66
+ if (isCompact)
67
+ return red(line);
68
+ return red(`+ ${formatLine(line, outputTruncateLength)}`);
69
+ }
70
+ if (line.match(/@@/))
71
+ return "--";
72
+ return ` ${line}`;
73
+ });
74
+ if (moreLines)
75
+ formatted.push(dim(`... ${moreLines} more lines`));
76
+ if (showLegend) {
77
+ if (isCompact) {
78
+ formatted = [
79
+ `${green("- Expected")} ${formatted[0]}`,
80
+ `${red("+ Received")} ${formatted[1]}`
81
+ ];
82
+ } else {
83
+ if (formatted[0].includes('"'))
84
+ formatted[0] = formatted[0].replace('"', "");
85
+ const last = formatted.length - 1;
86
+ if (formatted[last].endsWith('"'))
87
+ formatted[last] = formatted[last].slice(0, formatted[last].length - 1);
88
+ formatted.unshift(
89
+ green(`- Expected - ${counts["-"]}`),
90
+ red(`+ Received + ${counts["+"]}`),
91
+ ""
92
+ );
93
+ }
94
+ }
95
+ return formatted.map((i) => i ? indent + i : i).join("\n");
96
+ }
97
+
98
+ export { formatLine, unifiedDiff };
@@ -0,0 +1,4 @@
1
+ declare function assertTypes(value: unknown, name: string, types: string[]): void;
2
+ declare function isObject(item: unknown): boolean;
3
+
4
+ export { assertTypes, isObject };
@@ -0,0 +1,11 @@
1
+ function assertTypes(value, name, types) {
2
+ const receivedType = typeof value;
3
+ const pass = types.includes(receivedType);
4
+ if (!pass)
5
+ throw new TypeError(`${name} value must be ${types.join(" or ")}, received "${receivedType}"`);
6
+ }
7
+ function isObject(item) {
8
+ return item != null && typeof item === "object" && !Array.isArray(item);
9
+ }
10
+
11
+ export { assertTypes, isObject };
@@ -0,0 +1,10 @@
1
+ export { DiffOptions, formatLine, unifiedDiff } from './diff.js';
2
+ export { assertTypes, isObject } from './helpers.js';
3
+ export { ArgumentsType, Arrayable, Awaitable, Constructable, DeepMerge, MergeInsertions, MutableArray, Nullable } from './types.js';
4
+ import { PrettyFormatOptions } from 'pretty-format';
5
+
6
+ declare function stringify(object: unknown, maxDepth?: number, { maxLength, ...options }?: PrettyFormatOptions & {
7
+ maxLength?: number;
8
+ }): string;
9
+
10
+ export { stringify };
package/dist/index.js ADDED
@@ -0,0 +1,46 @@
1
+ export { formatLine, unifiedDiff } from './diff.js';
2
+ export { assertTypes, isObject } from './helpers.js';
3
+ import { format, plugins } from 'pretty-format';
4
+ import 'picocolors';
5
+ import 'diff';
6
+ import 'cli-truncate';
7
+
8
+ const {
9
+ AsymmetricMatcher,
10
+ DOMCollection,
11
+ DOMElement,
12
+ Immutable,
13
+ ReactElement,
14
+ ReactTestComponent
15
+ } = plugins;
16
+ const PLUGINS = [
17
+ ReactTestComponent,
18
+ ReactElement,
19
+ DOMElement,
20
+ DOMCollection,
21
+ Immutable,
22
+ AsymmetricMatcher
23
+ ];
24
+ function stringify(object, maxDepth = 10, { maxLength, ...options } = {}) {
25
+ const MAX_LENGTH = maxLength ?? 1e4;
26
+ let result;
27
+ try {
28
+ result = format(object, {
29
+ maxDepth,
30
+ escapeString: false,
31
+ plugins: PLUGINS,
32
+ ...options
33
+ });
34
+ } catch {
35
+ result = format(object, {
36
+ callToJSON: false,
37
+ maxDepth,
38
+ escapeString: false,
39
+ plugins: PLUGINS,
40
+ ...options
41
+ });
42
+ }
43
+ return result.length >= MAX_LENGTH && maxDepth > 1 ? stringify(object, Math.floor(maxDepth / 2)) : result;
44
+ }
45
+
46
+ export { stringify };
@@ -0,0 +1,18 @@
1
+ type Awaitable<T> = T | PromiseLike<T>;
2
+ type Nullable<T> = T | null | undefined;
3
+ type Arrayable<T> = T | Array<T>;
4
+ type ArgumentsType<T> = T extends (...args: infer U) => any ? U : never;
5
+ type MergeInsertions<T> = T extends object ? {
6
+ [K in keyof T]: MergeInsertions<T[K]>;
7
+ } : T;
8
+ type DeepMerge<F, S> = MergeInsertions<{
9
+ [K in keyof F | keyof S]: K extends keyof S & keyof F ? DeepMerge<F[K], S[K]> : K extends keyof S ? S[K] : K extends keyof F ? F[K] : never;
10
+ }>;
11
+ type MutableArray<T extends readonly any[]> = {
12
+ -readonly [k in keyof T]: T[k];
13
+ };
14
+ interface Constructable {
15
+ new (...args: any[]): any;
16
+ }
17
+
18
+ export { ArgumentsType, Arrayable, Awaitable, Constructable, DeepMerge, MergeInsertions, MutableArray, Nullable };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@vitest/utils",
3
+ "type": "module",
4
+ "version": "0.27.0",
5
+ "description": "Shared Vitest utility functions",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/vitest-dev/vitest.git",
10
+ "directory": "packages/utils"
11
+ },
12
+ "sideEffects": false,
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ },
18
+ "./diff": {
19
+ "types": "./dist/diff.d.ts",
20
+ "import": "./dist/diff.js"
21
+ },
22
+ "./helpers": {
23
+ "types": "./dist/helpers.d.ts",
24
+ "import": "./dist/helpers.js"
25
+ },
26
+ "./*": "./*"
27
+ },
28
+ "main": "./dist/index.js",
29
+ "module": "./dist/index.js",
30
+ "types": "./dist/index.d.ts",
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "dependencies": {
35
+ "cli-truncate": "^3.1.0",
36
+ "diff": "^5.1.0",
37
+ "picocolors": "^1.0.0",
38
+ "pretty-format": "^27.5.1"
39
+ },
40
+ "devDependencies": {
41
+ "@types/diff": "^5.0.2"
42
+ },
43
+ "scripts": {
44
+ "build": "rimraf dist && rollup -c",
45
+ "dev": "rollup -c --watch"
46
+ }
47
+ }