Skip to content

Commit ae972e9

Browse files
committed
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.
1 parent 20c4b80 commit ae972e9

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

doc/api/util.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,6 +2483,8 @@ console.log(
24832483
);
24842484
```
24852485
2486+
The special format value `none` applies no additional styling to the text.
2487+
24862488
The full list of formats can be found in [modifiers][].
24872489
24882490
## Class: `util.TextDecoder`

lib/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ let internalDeepEqual;
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)