1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
126
127
128
129
130
131
132
133
134 |
 
 
 
x1256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1256
x1256
 
 
 
 
x1885
 
x1885
x1885
x2109
x1885
x2049
x1885
x2126
x1885
x1885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1256
x1874
x1874
x2092
x1874
x2033
x1874
x2115
x1874
x1874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1256
x1256
x1256
 
x1385
x1385
x1385
x1385
x1385
x1385
x1385
x1385
x1385
x1385
x1385
x1996
x1996
x1996
x1996
x1996
x1996
x1996
x1385
x4158
x1385
x1385 |
|
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import { bgGreen, bgRed, bold, gray, green, red, white } from "./styles.ts";
import type { DiffResult, DiffType } from "./types.ts";
/**
* Colors the output of assertion diffs.
*
* @param diffType Difference type, either added or removed.
* @param background If true, colors the background instead of the text.
*
* @returns A function that colors the input string.
*
* @example Usage
* ```ts
* import { createColor } from "@std/internal";
* import { assertEquals } from "@std/assert";
* import { bold, green, red, white } from "@std/fmt/colors";
*
* assertEquals(createColor("added")("foo"), green(bold("foo")));
* assertEquals(createColor("removed")("foo"), red(bold("foo")));
* assertEquals(createColor("common")("foo"), white("foo"));
* ```
*/
export function createColor(
diffType: DiffType,
/**
* TODO(@littledivy): Remove this when we can detect true color terminals. See
* https://github.com/denoland/deno_std/issues/2575.
*/
background = false,
): (s: string) => string {
switch (diffType) {
case "added":
return (s) => background ? bgGreen(white(s)) : green(bold(s));
case "removed":
return (s) => background ? bgRed(white(s)) : red(bold(s));
default:
return white;
}
}
/**
* Prefixes `+` or `-` in diff output.
*
* @param diffType Difference type, either added or removed
*
* @returns A string representing the sign.
*
* @example Usage
* ```ts
* import { createSign } from "@std/internal";
* import { assertEquals } from "@std/assert";
*
* assertEquals(createSign("added"), "+ ");
* assertEquals(createSign("removed"), "- ");
* assertEquals(createSign("common"), " ");
* ```
*/
export function createSign(diffType: DiffType): string {
switch (diffType) {
case "added":
return "+ ";
case "removed":
return "- ";
default:
return " ";
}
}
/** Options for {@linkcode buildMessage}. */
export interface BuildMessageOptions {
/**
* Whether to output the diff as a single string.
*
* @default {false}
*/
stringDiff?: boolean;
}
/**
* Builds a message based on the provided diff result.
*
* @param diffResult The diff result array.
* @param options Optional parameters for customizing the message.
*
* @returns An array of strings representing the built message.
*
* @example Usage
* ```ts no-assert
* import { diffStr, buildMessage } from "@std/internal";
*
* const diffResult = diffStr("Hello, world!", "Hello, world");
*
* console.log(buildMessage(diffResult));
* // [
* // "",
* // "",
* // " [Diff] Actual / Expected",
* // "",
* // "",
* // "- Hello, world!",
* // "+ Hello, world",
* // "",
* // ]
* ```
*/
export function buildMessage(
diffResult: ReadonlyArray<DiffResult<string>>,
options: BuildMessageOptions = {},
): string[] {
const { stringDiff = false } = options;
const messages = [
"",
"",
` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${
green(bold("Expected"))
}`,
"",
"",
];
const diffMessages = diffResult.map((result) => {
const color = createColor(result.type);
const line = result.details?.map((detail) =>
detail.type !== "common"
? createColor(detail.type, true)(detail.value)
: detail.value
).join("") ?? result.value;
return color(`${createSign(result.type)}${line}`);
});
messages.push(...(stringDiff ? [diffMessages.join("")] : diffMessages), "");
return messages;
}
|