diff --git a/examples/rainbow.js b/examples/rainbow.js index 5b2b1d2..d0a10eb 100644 --- a/examples/rainbow.js +++ b/examples/rainbow.js @@ -15,7 +15,7 @@ function rainbow(string, offset) { let hue = offset % 360; const characters = []; for (const character of string) { - if (ignoreChars.test(character)) { + if (character < '!' || character > '~') { characters.push(character); } else { characters.push(chalk.hex(convertColor.hsl.hex(hue, 100, 50))(character)); diff --git a/source/index.js b/source/index.js index 8bc993d..4361685 100644 --- a/source/index.js +++ b/source/index.js @@ -22,7 +22,7 @@ const levelMapping = [ const styles = Object.create(null); const applyOptions = (object, options = {}) => { - if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) { + if (options.level !== undefined && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) { throw new Error('The `level` option should be an integer from 0 to 3'); } @@ -151,8 +151,15 @@ const createStyler = (open, close, parent) => { const createBuilder = (self, _styler, _isEmpty) => { // Single argument is hot path, implicit coercion is faster than anything - // eslint-disable-next-line no-implicit-coercion - const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' ')); + const builder = (...arguments_) => { + // Tagged template literal: first argument is a strings array with a `.raw` property + if (arguments_.length > 0 && Array.isArray(arguments_[0]) && 'raw' in arguments_[0]) { + return applyStyle(builder, String.raw(arguments_[0], ...arguments_.slice(1))); + } + + // eslint-disable-next-line no-implicit-coercion + return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' ')); + }; // We alter the prototype because we must return a function, but there is // no way to create a function with a different prototype diff --git a/source/utilities.js b/source/utilities.js index 4366dee..44020d0 100644 --- a/source/utilities.js +++ b/source/utilities.js @@ -20,14 +20,14 @@ export function stringReplaceAll(string, substring, replacer) { export function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) { let endIndex = 0; - let returnValue = ''; + const parts = []; do { const gotCR = string[index - 1] === '\r'; - returnValue += string.slice(endIndex, (gotCR ? index - 1 : index)) + prefix + (gotCR ? '\r\n' : '\n') + postfix; + parts.push(string.slice(endIndex, gotCR ? index - 1 : index), prefix, gotCR ? '\r\n' : '\n', postfix); endIndex = index + 1; index = string.indexOf('\n', endIndex); } while (index !== -1); - returnValue += string.slice(endIndex); - return returnValue; + parts.push(string.slice(endIndex)); + return parts.join(''); } diff --git a/source/vendor/ansi-styles/index.js b/source/vendor/ansi-styles/index.js index eaa7bed..ecf548e 100644 --- a/source/vendor/ansi-styles/index.js +++ b/source/vendor/ansi-styles/index.js @@ -133,7 +133,8 @@ function assembleStyles() { }, hexToRgb: { value(hex) { - const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16)); + const hexString = typeof hex === 'number' ? hex.toString(16).padStart(6, '0') : String(hex); + const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hexString); if (!matches) { return [0, 0, 0]; } diff --git a/source/vendor/supports-color/browser.js b/source/vendor/supports-color/browser.js index fbb6ce0..08fd1bf 100644 --- a/source/vendor/supports-color/browser.js +++ b/source/vendor/supports-color/browser.js @@ -7,7 +7,7 @@ const level = (() => { if (globalThis.navigator.userAgentData) { const brand = navigator.userAgentData.brands.find(({brand}) => brand === 'Chromium'); - if (brand && brand.version > 93) { + if (brand && Number(brand.version) > 93) { return 3; } } diff --git a/source/vendor/supports-color/index.js b/source/vendor/supports-color/index.js index 265d7f8..f1951f6 100644 --- a/source/vendor/supports-color/index.js +++ b/source/vendor/supports-color/index.js @@ -40,7 +40,8 @@ function envForceColor() { return 0; } - return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3); + const parsed = Number.parseInt(env.FORCE_COLOR, 10); + return env.FORCE_COLOR.length === 0 ? 1 : (Number.isNaN(parsed) ? 1 : Math.min(parsed, 3)); } } @@ -58,12 +59,8 @@ function translateLevel(level) { } function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) { - const noFlagForceColor = envForceColor(); - if (noFlagForceColor !== undefined) { - flagForceColor = noFlagForceColor; - } - - const forceColor = sniffFlags ? flagForceColor : noFlagForceColor; + const envColor = envForceColor(); + const forceColor = sniffFlags ? (envColor === undefined ? flagForceColor : envColor) : envColor; if (forceColor === 0) { return 0; diff --git a/test/chalk.js b/test/chalk.js index 8d58e45..d1e81ec 100644 --- a/test/chalk.js +++ b/test/chalk.js @@ -124,3 +124,22 @@ test('keeps function prototype methods', t => { t.is(chalk.bind(chalk, 'foo')(), 'foo'); t.is(chalk.call(chalk, 'foo'), 'foo'); }); + +test('accept numeric hex values in .hex() (S2)', t => { + // Numeric values should behave identically to their string equivalents + t.is(new Chalk({level: 3}).hex(0xFF_00_00)('hello'), new Chalk({level: 3}).hex('#FF0000')('hello')); + t.is(new Chalk({level: 3}).bgHex(0xFF_00_FF)('hello'), new Chalk({level: 3}).bgHex('#FF00FF')('hello')); + // Leading zeros must be preserved: 0x0000FF is blue, not a garbled 3-char match + t.is(new Chalk({level: 3}).hex(0x00_00_FF)('hello'), new Chalk({level: 3}).hex('#0000FF')('hello')); + t.is(new Chalk({level: 3}).hex(0x00_FF_00)('hello'), new Chalk({level: 3}).hex('#00FF00')('hello')); +}); + +test('support tagged template literals with interpolations (P6)', t => { + const name = 'World'; + t.is(chalk.red`hello ${name}`, '\u001B[31mhello World\u001B[39m'); + t.is(chalk.bold`count: ${42}`, '\u001B[1mcount: 42\u001B[22m'); + // Without interpolations should still work + t.is(chalk.red`hello`, '\u001B[31mhello\u001B[39m'); + // Array arguments (not template literals) must be unaffected + t.is(chalk.bold(['foo', 'bar']), '\u001B[1mfoo,bar\u001B[22m'); +}); diff --git a/test/instance.js b/test/instance.js index c3cc70b..ac1a038 100644 --- a/test/instance.js +++ b/test/instance.js @@ -22,3 +22,15 @@ test('the `level` option should be a number from 0 to 3', t => { }, {message: /should be an integer from 0 to 3/}); /* eslint-enable no-new */ }); + +test('the `level` option rejects null and NaN (S5)', t => { + /* eslint-disable no-new */ + t.throws(() => { + new Chalk({level: null}); + }, {message: /should be an integer from 0 to 3/}); + + t.throws(() => { + new Chalk({level: Number.NaN}); + }, {message: /should be an integer from 0 to 3/}); + /* eslint-enable no-new */ +});