refactor(internal): cleanup buildMessage() (#4705)

This commit is contained in:
Asher Gomez 2024-05-13 07:14:33 +10:00 committed by GitHub
parent ad5a2d93c8
commit ea70808fee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,31 +5,38 @@ import { bgGreen, bgRed, bold, gray, green, red, white } from "@std/fmt/colors";
import type { DiffResult, DiffType } from "./_types.ts";
/**
* Colors the output of assertion diffs
* @param diffType Difference type, either added or removed
* 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.
*/
function createColor(
diffType: DiffType,
{ background = false } = {},
background = false,
): (s: string) => string {
// TODO(@littledivy): Remove this when we can detect
// true color terminals.
// https://github.com/denoland/deno_std/issues/2575
/**
* TODO(@littledivy): Remove this when we can detect true color terminals. See
* https://github.com/denoland/deno_std/issues/2575.
*/
background = false;
switch (diffType) {
case "added":
return (s: string): string =>
background ? bgGreen(white(s)) : green(bold(s));
return (s) => background ? bgGreen(white(s)) : green(bold(s));
case "removed":
return (s: string): string => background ? bgRed(white(s)) : red(bold(s));
return (s) => background ? bgRed(white(s)) : red(bold(s));
default:
return white;
}
}
/**
* Prefixes `+` or `-` in diff output
* Prefixes `+` or `-` in diff output.
*
* @param diffType Difference type, either added or removed
*
* @returns A string representing the sign.
*/
function createSign(diffType: DiffType): string {
switch (diffType) {
@ -42,32 +49,37 @@ function createSign(diffType: DiffType): string {
}
}
/**
* Builds a message based on the provided diff result.
*
* @param diffResult The diff result array.
* @param options Optional parameters for customizing the message.
* @param options.stringDiff Whether to output the diff as a single string.
*
* @returns An array of strings representing the built message.
*/
export function buildMessage(
diffResult: ReadonlyArray<DiffResult<string>>,
{ stringDiff = false } = {},
): string[] {
const messages: string[] = [];
const diffMessages: string[] = [];
messages.push("");
messages.push("");
messages.push(
const messages = [
"",
"",
` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${
green(bold("Expected"))
}`,
);
messages.push("");
messages.push("");
diffResult.forEach((result) => {
const c = createColor(result.type);
"",
"",
];
const diffMessages = diffResult.map((result) => {
const color = createColor(result.type);
const line = result.details?.map((detail) =>
detail.type !== "common"
? createColor(detail.type, { background: true })(detail.value)
? createColor(detail.type, true)(detail.value)
: detail.value
).join("") ?? result.value;
diffMessages.push(c(`${createSign(result.type)}${line}`));
return color(`${createSign(result.type)}${line}`);
});
messages.push(...(stringDiff ? [diffMessages.join("")] : diffMessages));
messages.push("");
messages.push(...(stringDiff ? [diffMessages.join("")] : diffMessages), "");
return messages;
}