1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(node/util): support array formats in styleText (#26507)

We missed adding support for an array of formats being passed to
`util.styleText`.

Fixes https://github.com/denoland/deno/issues/26496
This commit is contained in:
Marvin Hagemeister 2024-10-24 11:46:51 +02:00 committed by GitHub
parent 79a3ad2b95
commit ef53ce3ac4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View file

@ -565,6 +565,19 @@ export function stripVTControlCharacters(str) {
export function styleText(format, text) {
validateString(text, "text");
if (Array.isArray(format)) {
for (let i = 0; i < format.length; i++) {
const item = format[i];
const formatCodes = inspect.colors[item];
if (formatCodes == null) {
validateOneOf(item, "format", Object.keys(inspect.colors));
}
text = `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`;
}
return text;
}
const formatCodes = inspect.colors[format];
if (formatCodes == null) {
validateOneOf(format, "format", Object.keys(inspect.colors));

View file

@ -353,3 +353,8 @@ Deno.test("[util] styleText()", () => {
const redText = util.styleText("red", "error");
assertEquals(redText, "\x1B[31merror\x1B[39m");
});
Deno.test("[util] styleText() with array of formats", () => {
const colored = util.styleText(["red", "green"], "error");
assertEquals(colored, "\x1b[32m\x1b[31merror\x1b[39m\x1b[39m");
});