Skip to content

Commit 24faff7

Browse files
RafaelGSShemanth
andcommitted
util: add styleText API to text formatting
Co-Authored-By: Hemanth HM <hemanth.hm@gmail.com>
1 parent 7fb80e5 commit 24faff7

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

doc/api/util.md

+37
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,42 @@ console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m'));
17921792
// Prints "value"
17931793
```
17941794
1795+
## `util.styleText(format, text)`
1796+
1797+
> Stability: 1.1 - Active development
1798+
1799+
<!-- YAML
1800+
added: REPLACEME
1801+
-->
1802+
1803+
* `format` {string} A text format defined in `util.inspect.colors`.
1804+
* `text` {string} The text to to be formatted.
1805+
1806+
This function returns a formatted text considering the `format` passed.
1807+
1808+
```mjs
1809+
import { styleText } from 'node:util';
1810+
const errorMessage = styleText('red', 'Error! Error!');
1811+
console.log(errorMessage);
1812+
```
1813+
1814+
```cjs
1815+
const { styleText } = require('node:util');
1816+
const errorMessage = styleText('red', 'Error! Error!');
1817+
console.log(errorMessage);
1818+
```
1819+
1820+
`util.inspect.colors` also provides text formats such as `italic`, and
1821+
`underline` and you can combine both:
1822+
1823+
```cjs
1824+
console.log(
1825+
util.styleText('underline', util.styleText('italic', 'My italic underlined message')),
1826+
);
1827+
```
1828+
1829+
The full list of formats can be found in [modifiers][].
1830+
17951831
## Class: `util.TextDecoder`
17961832
17971833
<!-- YAML
@@ -3395,6 +3431,7 @@ util.log('Timestamped message.');
33953431
[default sort]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
33963432
[global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
33973433
[list of deprecated APIS]: deprecations.md#list-of-deprecated-apis
3434+
[modifiers]: #modifiers
33983435
[realm]: https://tc39.es/ecma262/#realm
33993436
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
34003437
[util.inspect.custom]: #utilinspectcustom

lib/util.js

+16
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const {
6868
validateFunction,
6969
validateNumber,
7070
validateString,
71+
validateOneOf,
7172
} = require('internal/validators');
7273
const { isBuffer } = require('buffer').Buffer;
7374
const types = require('internal/util/types');
@@ -197,6 +198,20 @@ function pad(n) {
197198
return StringPrototypePadStart(n.toString(), 2, '0');
198199
}
199200

201+
/**
202+
* @param {string} format
203+
* @param {string} text
204+
* @returns {string}
205+
*/
206+
function styleText(format, text) {
207+
validateString(text, 'text');
208+
const formatCodes = inspect.colors[format];
209+
if (formatCodes == null) {
210+
validateOneOf(format, 'format', ObjectKeys(inspect.colors));
211+
}
212+
return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`;
213+
}
214+
200215
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
201216
'Oct', 'Nov', 'Dec'];
202217

@@ -395,6 +410,7 @@ module.exports = {
395410
debuglog,
396411
deprecate,
397412
format,
413+
styleText,
398414
formatWithOptions,
399415
getSystemErrorMap,
400416
getSystemErrorName,

test/parallel/test-util-styletext.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const util = require('util');
5+
6+
[
7+
undefined,
8+
null,
9+
false,
10+
5n,
11+
5,
12+
Symbol(),
13+
() => {},
14+
{},
15+
[],
16+
].forEach((invalidOption) => {
17+
assert.throws(() => {
18+
util.styleText(invalidOption, 'test');
19+
}, {
20+
code: 'ERR_INVALID_ARG_VALUE',
21+
});
22+
assert.throws(() => {
23+
util.styleText('red', invalidOption);
24+
}, {
25+
code: 'ERR_INVALID_ARG_TYPE'
26+
});
27+
});
28+
29+
assert.throws(() => {
30+
util.styleText('invalid', 'text');
31+
}, {
32+
code: 'ERR_INVALID_ARG_VALUE',
33+
});
34+
35+
assert.strictEqual(util.styleText('red', 'test'), '\u001b[31mtest\u001b[39m');

0 commit comments

Comments
 (0)