From 532c173cf2ea1fe56c8f0a7fc7dd4cbc03c1fad3 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 23 May 2025 09:07:10 -0700 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/58437 Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel --- doc/api/util.md | 5 +++++ lib/util.js | 4 +++- test/parallel/test-util-styletext.js | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index da3fa07c9e6..06d7024a1b7 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -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 @@ -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` diff --git a/lib/util.js b/lib/util.js index 233da10e83c..a422daa212b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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`; } @@ -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) { diff --git a/test/parallel/test-util-styletext.js b/test/parallel/test-util-styletext.js index 68c31eef2e0..df2334651cc 100644 --- a/test/parallel/test-util-styletext.js +++ b/test/parallel/test-util-styletext.js @@ -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);