Skip to content

Commit

Permalink
fix(node/util): support array formats in styleText (#26507)
Browse files Browse the repository at this point in the history
We missed adding support for an array of formats being passed to
`util.styleText`.

Fixes #26496
  • Loading branch information
marvinhagemeister authored Oct 24, 2024
1 parent 79a3ad2 commit ef53ce3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ext/node/polyfills/internal/util/inspect.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions tests/unit_node/util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

0 comments on commit ef53ce3

Please sign in to comment.