Skip to content

Commit 532c173

Browse files
jasnelltargos
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 7a91f4a commit 532c173

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
@@ -2408,6 +2408,9 @@ added:
24082408
- v21.7.0
24092409
- v20.12.0
24102410
changes:
2411+
- version: REPLACEME
2412+
pr-url: https://github.com/nodejs/node/pull/58437
2413+
description: Added the `'none'` format as a non-op format.
24112414
- version:
24122415
- v23.5.0
24132416
- v22.13.0
@@ -2483,6 +2486,8 @@ console.log(
24832486
);
24842487
```
24852488
2489+
The special format value `none` applies no additional styling to the text.
2490+
24862491
The full list of formats can be found in [modifiers][].
24872492
24882493
## Class: `util.TextDecoder`

lib/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ function lazyAbortController() {
100100
let internalDeepEqual;
101101

102102
/**
103-
* @param {string} code
103+
* @param {string} [code]
104104
* @returns {string}
105105
*/
106106
function escapeStyleCode(code) {
107+
if (code === undefined) return '';
107108
return `\u001b[${code}m`;
108109
}
109110

@@ -139,6 +140,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
139140
let left = '';
140141
let right = '';
141142
for (const key of formatArray) {
143+
if (key === 'none') continue;
142144
const formatCodes = inspect.colors[key];
143145
// If the format is not a valid style, throw an error
144146
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)