Skip to content

Commit 54288bd

Browse files
jasnelladuh95
authored andcommitted
util: add 'none' style to styleText
For cases where the style is not needed but code still calls styleText unconditionally, this adds a `none` style that not not apply any styling to the text. PR-URL: #58437 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 20d978d commit 54288bd

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/api/util.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,6 +2394,9 @@ added:
23942394
- v21.7.0
23952395
- v20.12.0
23962396
changes:
2397+
- version: REPLACEME
2398+
pr-url: https://github.com/nodejs/node/pull/58437
2399+
description: Added the `'none'` format as a non-op format.
23972400
- version: v22.13.0
23982401
pr-url: https://github.com/nodejs/node/pull/56265
23992402
description: styleText is now stable.
@@ -2467,6 +2470,8 @@ console.log(
24672470
);
24682471
```
24692472
2473+
The special format value `none` applies no additional styling to the text.
2474+
24702475
The full list of formats can be found in [modifiers][].
24712476
24722477
## Class: `util.TextDecoder`

lib/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,11 @@ function pad(n) {
217217
}
218218

219219
/**
220-
* @param {string} code
220+
* @param {string} [code]
221221
* @returns {string}
222222
*/
223223
function escapeStyleCode(code) {
224+
if (code === undefined) return '';
224225
return `\u001b[${code}m`;
225226
}
226227

@@ -256,6 +257,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
256257
let left = '';
257258
let right = '';
258259
for (const key of formatArray) {
260+
if (key === 'none') continue;
259261
const formatCodes = inspect.colors[key];
260262
// If the format is not a valid style, throw an error
261263
if (formatCodes == null) {

test/parallel/test-util-styletext.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ assert.strictEqual(
7575
styled,
7676
);
7777

78+
assert.strictEqual(util.styleText('none', 'test'), 'test');
79+
7880
const fd = common.getTTYfd();
7981
if (fd !== -1) {
8082
const writeStream = new WriteStream(fd);

0 commit comments

Comments
 (0)
close