Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 88e08bb

Browse filesBrowse files
RafaelGSShemanth
authored andcommitted
util: add styleText API to text formatting
Co-Authored-By: Hemanth HM <hemanth.hm@gmail.com> PR-URL: #51850 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Adrian Estrada <edsadr@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 8dc3f91 commit 88e08bb
Copy full SHA for 88e08bb

File tree

Expand file treeCollapse file tree

3 files changed

+88
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+88
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/util.md‎

Copy file name to clipboardExpand all lines: doc/api/util.md
+37Lines changed: 37 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+16Lines changed: 16 additions & 0 deletions
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

@@ -393,6 +408,7 @@ module.exports = {
393408
debuglog,
394409
deprecate,
395410
format,
411+
styleText,
396412
formatWithOptions,
397413
getSystemErrorMap,
398414
getSystemErrorName,
Collapse file
+35Lines changed: 35 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.