@augment-vir/core 31.37.0 → 31.38.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.
@@ -0,0 +1 @@
1
+ export declare function prettyDiff(actual: unknown, expected: unknown): string;
@@ -0,0 +1,84 @@
1
+ import { diffLines, diffWords } from 'diff';
2
+ import { stringify } from '../object/stringify.js';
3
+ import { isRuntimeEnv, RuntimeEnv } from '../runtime-env.js';
4
+ export function prettyDiff(actual, expected) {
5
+ const bothStrings = typeof expected === 'string' && typeof actual === 'string';
6
+ const useLines = typeof expected !== 'string' || typeof actual !== 'string';
7
+ const diffFunction = (useLines ? diffLines : diffWords);
8
+ const expectedString = [
9
+ bothStrings ? '' : '\n',
10
+ stringify(expected, 4),
11
+ '\n',
12
+ ].join('');
13
+ const actualString = [
14
+ bothStrings ? '' : '\n',
15
+ stringify(actual, 4),
16
+ '\n',
17
+ ].join('');
18
+ const changes = addDiffColors(useLines, diffFunction(expectedString, actualString));
19
+ const useColor = isRuntimeEnv(RuntimeEnv.Node);
20
+ /* node:coverage ignore next 7: currently only tested in node */
21
+ const explanationLine = [
22
+ useColor ? NodeColor.Green : '',
23
+ ' +added',
24
+ useColor ? NodeColor.Red : '',
25
+ ' -missing',
26
+ useColor ? NodeColor.Reset : '',
27
+ ].join('');
28
+ return [
29
+ explanationLine,
30
+ bothStrings ? '\n\n' : '\n',
31
+ changes,
32
+ ].join('');
33
+ }
34
+ var NodeColor;
35
+ (function (NodeColor) {
36
+ NodeColor["Green"] = "\u001B[32m";
37
+ NodeColor["Red"] = "\u001B[31m";
38
+ NodeColor["Reset"] = "\u001B[0m";
39
+ })(NodeColor || (NodeColor = {}));
40
+ var DiffPrefix;
41
+ (function (DiffPrefix) {
42
+ DiffPrefix["Added"] = "+";
43
+ DiffPrefix["Removed"] = "-";
44
+ })(DiffPrefix || (DiffPrefix = {}));
45
+ function addDiffColors(shouldUseLines, changes) {
46
+ const coloredDiff = shouldUseLines
47
+ ? changes
48
+ .flatMap((change) => {
49
+ return change.value
50
+ .split('\n')
51
+ .map((line) => {
52
+ return addColorToChange(line, change);
53
+ })
54
+ .join('\n');
55
+ })
56
+ .join('')
57
+ : changes
58
+ .map((change) => {
59
+ return addColorToChange(undefined, change);
60
+ })
61
+ .join('');
62
+ return coloredDiff;
63
+ }
64
+ function addColorToChange(line, change) {
65
+ if (line != undefined && !line) {
66
+ return '';
67
+ }
68
+ const useColor = isRuntimeEnv(RuntimeEnv.Node);
69
+ const prefix = change.added
70
+ ? DiffPrefix.Added
71
+ : change.removed
72
+ ? DiffPrefix.Removed
73
+ : line == undefined
74
+ ? ''
75
+ : ' ';
76
+ const color = change.added ? NodeColor.Green : change.removed ? NodeColor.Red : NodeColor.Reset;
77
+ return [
78
+ /* node:coverage ignore next 1 */
79
+ useColor ? color : '',
80
+ prefix,
81
+ line ?? change.value,
82
+ NodeColor.Reset,
83
+ ].join('');
84
+ }
@@ -0,0 +1,5 @@
1
+ import { type prettyDiff } from './pretty-diff.js';
2
+ export declare const mockPrettyDiffTestCases: {
3
+ it: string;
4
+ inputs: Parameters<typeof prettyDiff>;
5
+ }[];
@@ -0,0 +1,62 @@
1
+ export const mockPrettyDiffTestCases = [
2
+ {
3
+ it: 'handles strings',
4
+ inputs: [
5
+ 'hello there why',
6
+ 'hello what why',
7
+ ],
8
+ },
9
+ {
10
+ it: 'handles objects',
11
+ inputs: [
12
+ { a: 'hello there', b: 'goodbye now' },
13
+ { a: 'hello there' },
14
+ ],
15
+ },
16
+ {
17
+ it: 'handles numbers',
18
+ inputs: [
19
+ 52,
20
+ 40,
21
+ ],
22
+ },
23
+ {
24
+ it: 'expected object but got string',
25
+ inputs: [
26
+ 'hello there',
27
+ {
28
+ a: 'hello there',
29
+ },
30
+ ],
31
+ },
32
+ {
33
+ it: 'expected string but got object',
34
+ inputs: [
35
+ {
36
+ a: 'hello there',
37
+ },
38
+ 'hello there',
39
+ ],
40
+ },
41
+ {
42
+ it: 'expected number but got string',
43
+ inputs: [
44
+ 'hello there',
45
+ 42,
46
+ ],
47
+ },
48
+ {
49
+ it: 'expected string but got number',
50
+ inputs: [
51
+ 42,
52
+ 'hello there',
53
+ ],
54
+ },
55
+ {
56
+ it: 'got completely different strings',
57
+ inputs: [
58
+ 'nothing here',
59
+ 'hello there',
60
+ ],
61
+ },
62
+ ];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ /* node:coverage disable */
2
+ /** Run this script (without any args) to see what the pretty diff string printing looks like. */
3
+ import { prettyDiff } from './pretty-diff.js';
4
+ import { mockPrettyDiffTestCases } from './pretty-diff.mock.js';
5
+ mockPrettyDiffTestCases.forEach((testCase) => {
6
+ console.info('================================');
7
+ console.info(testCase.it);
8
+ console.info(prettyDiff(...testCase.inputs));
9
+ });
@@ -18,6 +18,9 @@ const undefinedSentinelStringRegExp = new RegExp(`['"]${undefinedSentinel}['"]`)
18
18
  export function stringify(input,
19
19
  /** Passed directly to the `space` parameter of `JSON.stringify`. */
20
20
  space) {
21
+ if (typeof input === 'string') {
22
+ return input;
23
+ }
21
24
  try {
22
25
  const json5String = JSON5.stringify(input,
23
26
  // Use a replacer to turn undefined into a unique string so the key is kept.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Indent all lines in a string by 4 spaces * count.
3
+ *
4
+ * @category String
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export declare function indent(value: string, count?: number): string;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Indent all lines in a string by 4 spaces * count.
3
+ *
4
+ * @category String
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export function indent(value, count = 1) {
9
+ return value
10
+ .split('\n')
11
+ .map((line) => [
12
+ ' '.repeat(Math.round(count)),
13
+ line,
14
+ ].join(''))
15
+ .join('\n');
16
+ }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './augments/array/array.js';
2
2
  export * from './augments/array/remove-duplicates.js';
3
3
  export * from './augments/array/tuple.js';
4
+ export * from './augments/diff/pretty-diff.js';
4
5
  export * from './augments/enum/enum-type.js';
5
6
  export * from './augments/enum/enum-values.js';
6
7
  export * from './augments/error/ensure-error.js';
@@ -27,6 +28,7 @@ export * from './augments/regexp/regexp-string.js';
27
28
  export * from './augments/runtime-env.js';
28
29
  export * from './augments/string/ansi.js';
29
30
  export * from './augments/string/casing.js';
31
+ export * from './augments/string/indent.js';
30
32
  export * from './augments/string/match.js';
31
33
  export * from './augments/string/punctuation.js';
32
34
  export * from './augments/string/remove-duplicate-characters.js';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './augments/array/array.js';
2
2
  export * from './augments/array/remove-duplicates.js';
3
3
  export * from './augments/array/tuple.js';
4
+ export * from './augments/diff/pretty-diff.js';
4
5
  export * from './augments/enum/enum-type.js';
5
6
  export * from './augments/enum/enum-values.js';
6
7
  export * from './augments/error/ensure-error.js';
@@ -27,6 +28,7 @@ export * from './augments/regexp/regexp-string.js';
27
28
  export * from './augments/runtime-env.js';
28
29
  export * from './augments/string/ansi.js';
29
30
  export * from './augments/string/casing.js';
31
+ export * from './augments/string/indent.js';
30
32
  export * from './augments/string/match.js';
31
33
  export * from './augments/string/punctuation.js';
32
34
  export * from './augments/string/remove-duplicate-characters.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/core",
3
- "version": "31.37.0",
3
+ "version": "31.38.0",
4
4
  "description": "Core augment-vir augments. Use @augment-vir/common instead.",
5
5
  "homepage": "https://github.com/electrovir/augment-vir",
6
6
  "bugs": {
@@ -25,16 +25,17 @@
25
25
  "start": "tsx src/index.ts",
26
26
  "test": "virmator test --no-deps node",
27
27
  "test:coverage": "npm run test coverage",
28
- "test:update": "npm test"
28
+ "test:update": "npm test update"
29
29
  },
30
30
  "dependencies": {
31
31
  "@date-vir/duration": "^7.4.2",
32
32
  "browser-or-node": "^3.0.0",
33
+ "diff": "^8.0.2",
33
34
  "json5": "^2.2.3",
34
- "type-fest": "^5.0.0"
35
+ "type-fest": "^5.0.1"
35
36
  },
36
37
  "devDependencies": {
37
- "@types/node": "^24.4.0",
38
+ "@types/node": "^24.5.2",
38
39
  "c8": "^10.1.3",
39
40
  "istanbul-smart-text-reporter": "^1.1.5",
40
41
  "typescript": "^5.9.2"