diff --git a/benchmark/util/style-text.js b/benchmark/util/style-text.js index 282a96150f0b94..2ca099da97ad33 100644 --- a/benchmark/util/style-text.js +++ b/benchmark/util/style-text.js @@ -5,14 +5,27 @@ const common = require('../common.js'); const { styleText } = require('node:util'); const assert = require('node:assert'); +const validFormats = ['red', 'italic', 'bold']; +const invalidFormats = 'invalidFormat'; + const bench = common.createBenchmark(main, { - messageType: ['string', 'number', 'boolean', 'invalid'], - format: ['red', 'italic', 'invalid'], - validateStream: [1, 0], - n: [1e3], -}); + withColor: { + messageType: ['string', 'number', 'boolean', 'invalid'], + isValidFormat: [1, 0], + validateStream: [1, 0], + noColor: [''], + n: [1e3], + }, + withoutColor: { + messageType: ['string', 'number', 'boolean', 'invalid'], + isValidFormat: [1, 0], + validateStream: [1, 0], + noColor: ['1'], + n: [1e3], + }, +}, { byGroups: true }); -function main({ messageType, format, validateStream, n }) { +function main({ messageType, isValidFormat, validateStream, noColor, n }) { let str; switch (messageType) { case 'string': @@ -29,6 +42,9 @@ function main({ messageType, format, validateStream, n }) { break; } + process.env.NO_COLOR = noColor; + const format = isValidFormat ? validFormats : invalidFormats; + bench.start(); for (let i = 0; i < n; i++) { let colored = ''; diff --git a/lib/util.js b/lib/util.js index 233da10e83c48d..ccfe124f175b68 100644 --- a/lib/util.js +++ b/lib/util.js @@ -119,7 +119,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou validateString(text, 'text'); validateBoolean(validateStream, 'options.validateStream'); - let skipColorize; + let shouldColorize = true; if (validateStream) { if ( !isReadableStream(stream) && @@ -130,26 +130,29 @@ function styleText(format, text, { validateStream = true, stream = process.stdou } // If the stream is falsy or should not be colorized, set skipColorize to true - skipColorize = !lazyUtilColors().shouldColorize(stream); + shouldColorize = lazyUtilColors().shouldColorize(stream); } // If the format is not an array, convert it to an array const formatArray = ArrayIsArray(format) ? format : [format]; + const { colors } = inspect; - let left = ''; - let right = ''; - for (const key of formatArray) { - const formatCodes = inspect.colors[key]; + // We want to loop through the format array to check if the format is valid + // including if it's shouldn't be colorized + const { left, right } = formatArray.reduce((acc, key) => { + const formatCodes = colors[key]; // If the format is not a valid style, throw an error if (formatCodes == null) { - validateOneOf(key, 'format', ObjectKeys(inspect.colors)); + validateOneOf(key, 'format', ObjectKeys(colors)); } - if (skipColorize) continue; - left += escapeStyleCode(formatCodes[0]); - right = `${escapeStyleCode(formatCodes[1])}${right}`; - } + if (shouldColorize) { + acc.left += escapeStyleCode(formatCodes[0]); + acc.right = `${escapeStyleCode(formatCodes[1])}${acc.right}`; + } + return acc; + }, { left: '', right: '' }); - return skipColorize ? text : `${left}${text}${right}`; + return shouldColorize ? `${left}${text}${right}` : text; } /**