diff --git a/doc/api/util.md b/doc/api/util.md index 8e48ea78c60..528763796d0 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -2394,6 +2394,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: v22.13.0 pr-url: https://github.com/nodejs/node/pull/56265 description: styleText is now stable. @@ -2467,6 +2470,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 a8a2bea01a0..dba1bfba367 100644 --- a/lib/util.js +++ b/lib/util.js @@ -217,10 +217,11 @@ function pad(n) { } /** - * @param {string} code + * @param {string} [code] * @returns {string} */ function escapeStyleCode(code) { + if (code === undefined) return ''; return `\u001b[${code}m`; } @@ -256,6 +257,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);