Skip to content

Commit

Permalink
fixup! util: add formatText API to text formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Feb 23, 2024
1 parent 913a0c9 commit c969cf8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 75 deletions.
50 changes: 25 additions & 25 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,31 +345,6 @@ util.format('%% %s');
Some input values can have a significant performance overhead that can block the
event loop. Use this function with care and never in a hot code path.

## `util.formatText(format, text)`

> Stability: 1 - Experimental
<!-- YAML
added: REPLACEME
-->

* `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

```js
const textRedCollor = util.formatText('red', 'My red message');
console.log(textRedCollor);
```

`util.inspect.colors` also provides text formats such as `italic`, and
`underline`:

```js
console.log(util.formatText('italic', 'My italic message'));
```

## `util.formatWithOptions(inspectOptions, format[, ...args])`

<!-- YAML
Expand Down Expand Up @@ -1817,6 +1792,31 @@ console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m'));
// Prints "value"
```
## `util.styleText(format, text)`
> Stability: 1.1 - Active development
<!-- YAML
added: REPLACEME
-->
* `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.
```js
const textRedCollr = util.styleText('red', 'My red message');
console.log(textRedColor);
```
`util.inspect.colors` also provides text formats such as `italic`, and
`underline` and you can combine both:
```js
console.log(util.styleText('underline', util.styleText('italic', 'My italic underlined message')));
```
## Class: `util.TextDecoder`
<!-- YAML
Expand Down
15 changes: 4 additions & 11 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const {
codes: {
ERR_FALSY_VALUE_REJECTION,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE,
},
isErrorStackTraceLimitWritable,
Expand All @@ -69,6 +68,7 @@ const {
validateFunction,
validateNumber,
validateString,
validateOneOf,
} = require('internal/validators');
const { isBuffer } = require('buffer').Buffer;
const types = require('internal/util/types');
Expand Down Expand Up @@ -203,17 +203,10 @@ function pad(n) {
* @param {string} text
* @returns {string}
*/
function formatText(format, text) {
validateString(format, 'format');
function styleText(format, text) {
validateOneOf(format, 'format', ObjectKeys(inspect.colors));
validateString(text, 'text');
const formatCodes = inspect.colors[format];
if (formatCodes === undefined) {
throw new ERR_INVALID_ARG_VALUE(
'format',
`${format}`,
`must be one of ${ObjectKeys(inspect.colors).join(',')}`,
);
}
return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`;
}

Expand Down Expand Up @@ -415,7 +408,7 @@ module.exports = {
debuglog,
deprecate,
format,
formatText,
styleText,
formatWithOptions,
getSystemErrorMap,
getSystemErrorName,
Expand Down
39 changes: 0 additions & 39 deletions test/parallel/test-util-formattext.js

This file was deleted.

35 changes: 35 additions & 0 deletions test/parallel/test-util-styletext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
require('../common');
const assert = require('assert');
const util = require('util');

[
undefined,
null,
false,
5n,
5,
Symbol(),
() => {},
{},
[],
].forEach((invalidOption) => {
assert.throws(() => {
util.styleText(invalidOption, 'test');
}, {
code: 'ERR_INVALID_ARG_VALUE',
});
assert.throws(() => {
util.styleText('red', invalidOption);
}, {
code: 'ERR_INVALID_ARG_TYPE'
});
});

assert.throws(() => {
util.styleText('invalid', 'text');
}, {
code: 'ERR_INVALID_ARG_VALUE',
});

assert.strictEqual(util.styleText('red', 'test'), '\u001b[31mtest\u001b[39m');

0 comments on commit c969cf8

Please sign in to comment.