Skip to content

Commit c969cf8

Browse files
committed
fixup! util: add formatText API to text formatting
1 parent 913a0c9 commit c969cf8

File tree

4 files changed

+64
-75
lines changed

4 files changed

+64
-75
lines changed

doc/api/util.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -345,31 +345,6 @@ util.format('%% %s');
345345
Some input values can have a significant performance overhead that can block the
346346
event loop. Use this function with care and never in a hot code path.
347347

348-
## `util.formatText(format, text)`
349-
350-
> Stability: 1 - Experimental
351-
352-
<!-- YAML
353-
added: REPLACEME
354-
-->
355-
356-
* `format` {string} A text format defined in `util.inspect.colors`.
357-
* `text` {string} The text to to be formatted.
358-
359-
This function returns a formatted text considering the `format` passed
360-
361-
```js
362-
const textRedCollor = util.formatText('red', 'My red message');
363-
console.log(textRedCollor);
364-
```
365-
366-
`util.inspect.colors` also provides text formats such as `italic`, and
367-
`underline`:
368-
369-
```js
370-
console.log(util.formatText('italic', 'My italic message'));
371-
```
372-
373348
## `util.formatWithOptions(inspectOptions, format[, ...args])`
374349

375350
<!-- YAML
@@ -1817,6 +1792,31 @@ console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m'));
18171792
// Prints "value"
18181793
```
18191794
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+
```js
1809+
const textRedCollr = util.styleText('red', 'My red message');
1810+
console.log(textRedColor);
1811+
```
1812+
1813+
`util.inspect.colors` also provides text formats such as `italic`, and
1814+
`underline` and you can combine both:
1815+
1816+
```js
1817+
console.log(util.styleText('underline', util.styleText('italic', 'My italic underlined message')));
1818+
```
1819+
18201820
## Class: `util.TextDecoder`
18211821
18221822
<!-- YAML

lib/util.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const {
5151
codes: {
5252
ERR_FALSY_VALUE_REJECTION,
5353
ERR_INVALID_ARG_TYPE,
54-
ERR_INVALID_ARG_VALUE,
5554
ERR_OUT_OF_RANGE,
5655
},
5756
isErrorStackTraceLimitWritable,
@@ -69,6 +68,7 @@ const {
6968
validateFunction,
7069
validateNumber,
7170
validateString,
71+
validateOneOf,
7272
} = require('internal/validators');
7373
const { isBuffer } = require('buffer').Buffer;
7474
const types = require('internal/util/types');
@@ -203,17 +203,10 @@ function pad(n) {
203203
* @param {string} text
204204
* @returns {string}
205205
*/
206-
function formatText(format, text) {
207-
validateString(format, 'format');
206+
function styleText(format, text) {
207+
validateOneOf(format, 'format', ObjectKeys(inspect.colors));
208208
validateString(text, 'text');
209209
const formatCodes = inspect.colors[format];
210-
if (formatCodes === undefined) {
211-
throw new ERR_INVALID_ARG_VALUE(
212-
'format',
213-
`${format}`,
214-
`must be one of ${ObjectKeys(inspect.colors).join(',')}`,
215-
);
216-
}
217210
return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`;
218211
}
219212

@@ -415,7 +408,7 @@ module.exports = {
415408
debuglog,
416409
deprecate,
417410
format,
418-
formatText,
411+
styleText,
419412
formatWithOptions,
420413
getSystemErrorMap,
421414
getSystemErrorName,

test/parallel/test-util-formattext.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

test/parallel/test-util-styletext.js

Lines 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)