mirror of
https://github.com/denoland/deno.git
synced 2024-12-21 23:04:45 -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:
parent
79a3ad2b95
commit
ef53ce3ac4
2 changed files with 18 additions and 0 deletions
|
@ -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));
|
||||
|
|
|
@ -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");
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue