Skip to content

util: add 'none' style to styleText #58437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -2408,6 +2408,9 @@ added:
- v21.7.0
- v20.12.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/58437
description: Added the `'none'` format as a non-op format.
- version:
- v23.5.0
- v22.13.0
Expand Down Expand Up @@ -2483,6 +2486,8 @@ console.log(
);
```

The special format value `none` applies no additional styling to the text.

The full list of formats can be found in [modifiers][].

## Class: `util.TextDecoder`
Expand Down
4 changes: 3 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ function lazyAbortController() {
let internalDeepEqual;

/**
* @param {string} code
* @param {string} [code]
* @returns {string}
*/
function escapeStyleCode(code) {
if (code === undefined) return '';
return `\u001b[${code}m`;
}

Expand Down Expand Up @@ -139,6 +140,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
let left = '';
let right = '';
for (const key of formatArray) {
if (key === 'none') continue;
const formatCodes = inspect.colors[key];
// If the format is not a valid style, throw an error
if (formatCodes == null) {
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-util-styletext.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ assert.strictEqual(
styled,
);

assert.strictEqual(util.styleText('none', 'test'), 'test');

const fd = common.getTTYfd();
if (fd !== -1) {
const writeStream = new WriteStream(fd);
Expand Down
Loading