diff --git a/doc/api/util.md b/doc/api/util.md index ed05dfebaf5384..f937d7272a7436 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -1792,6 +1792,42 @@ console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m')); // Prints "value" ``` +## `util.styleText(format, text)` + +> Stability: 1.1 - Active development + + + +* `format` {string} A text format defined in `util.inspect.colors`. +* `text` {string} The text to to be formatted. + +This function returns a formatted text considering the `format` passed. + +```mjs +import { styleText } from 'node:util'; +const errorMessage = styleText('red', 'Error! Error!'); +console.log(errorMessage); +``` + +```cjs +const { styleText } = require('node:util'); +const errorMessage = styleText('red', 'Error! Error!'); +console.log(errorMessage); +``` + +`util.inspect.colors` also provides text formats such as `italic`, and +`underline` and you can combine both: + +```cjs +console.log( + util.styleText('underline', util.styleText('italic', 'My italic underlined message')), +); +``` + +The full list of formats can be found in [modifiers][]. + ## Class: `util.TextDecoder`