Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/util/style-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const bench = common.createBenchmark(main, {
// '#rotating' cycles through kHexColorCount distinct colors to simulate
// the high-miss-rate / large-cache scenario (e.g. user-randomised colors).
format: ['red', 'italic', 'invalid', '#ff0000', '#rotating'],
validateStream: [1, 0],
validateStream: [true, false],
n: [1e3],
});

Expand Down
4 changes: 2 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ function rgbToAnsi24Bit(r, g, b) {
function styleText(format, text, options) {
const validateStream = options?.validateStream ?? true;

// Fast path: single format string with validateStream=false
if (!validateStream && typeof format === 'string' && typeof text === 'string') {
// Fast path: single format string with validateStream === false
if (validateStream === false && typeof format === 'string' && typeof text === 'string') {
const cache = getStyleCache();
if (format === 'none') return text;
const style = cache[format];
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-util-styletext.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ assert.throws(() => {
code: 'ERR_INVALID_ARG_VALUE',
});

for (const validateStream of [0, 0n, '', NaN, 1, 'false', {}, []]) {
assert.throws(() => {
util.styleText('red', 'test', { validateStream });
}, {
code: 'ERR_INVALID_ARG_TYPE',
}, `validateStream=${String(validateStream)}`);
}

assert.strictEqual(
util.styleText('red', 'test', { validateStream: false, stream: {} }),
'\u001b[31mtest\u001b[39m',
);

assert.strictEqual(
util.styleText('red', 'test', { validateStream: false }),
'\u001b[31mtest\u001b[39m',
Expand Down