Skip to content

Commit 0fb67ce

Browse files
authored
feat(node/util): styleText (#22758)
Implements nodejs/node#51850
1 parent 9df917c commit 0fb67ce

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

ext/node/polyfills/internal/util/inspect.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import {
2727
validateObject,
28+
validateOneOf,
2829
validateString,
2930
} from "ext:deno_node/internal/validators.mjs";
3031
import { codes } from "ext:deno_node/internal/error_codes.ts";
@@ -562,10 +563,20 @@ export function stripVTControlCharacters(str) {
562563
return str.replace(ansi, "");
563564
}
564565

566+
export function styleText(format, text) {
567+
validateString(text, "text");
568+
const formatCodes = inspect.colors[format];
569+
if (formatCodes == null) {
570+
validateOneOf(format, "format", Object.keys(inspect.colors));
571+
}
572+
return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`;
573+
}
574+
565575
export default {
566576
format,
567577
getStringWidth,
568578
inspect,
569579
stripVTControlCharacters,
570580
formatWithOptions,
581+
styleText,
571582
};

0 commit comments

Comments
 (0)