Skip to content

Commit

Permalink
util: add styleText API to text formatting
Browse files Browse the repository at this point in the history
Co-Authored-By: Hemanth HM <hemanth.hm@gmail.com>
  • Loading branch information
RafaelGSS and hemanth committed Feb 23, 2024
1 parent 7fb80e5 commit 44b90fe
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
25 changes: 25 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1792,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
14 changes: 14 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,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 @@ -197,6 +198,18 @@ function pad(n) {
return StringPrototypePadStart(n.toString(), 2, '0');
}

/**
* @param {string} format
* @param {string} text
* @returns {string}
*/
function styleText(format, text) {
validateOneOf(format, 'format', ObjectKeys(inspect.colors));
validateString(text, 'text');
const formatCodes = inspect.colors[format];
return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`;
}

const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];

Expand Down Expand Up @@ -395,6 +408,7 @@ module.exports = {
debuglog,
deprecate,
format,
styleText,
formatWithOptions,
getSystemErrorMap,
getSystemErrorName,
Expand Down
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 44b90fe

Please sign in to comment.