From d798222a5a3ff208782ce81f5fbd3babb286b3e3 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Sun, 18 Apr 2021 00:33:03 +1200 Subject: [PATCH] Add named exports (#432) --- index.d.ts | 145 +++++++++++++++++++-------------------- index.test-d.ts | 32 ++++----- readme.md | 10 +-- source/index.js | 25 ++++--- test/_fixture.js | 4 +- test/chalk.js | 24 +++---- test/constructor.js | 14 ---- test/instance.js | 8 +-- test/template-literal.js | 46 ++++++------- test/visible.js | 8 +-- 10 files changed, 149 insertions(+), 167 deletions(-) delete mode 100644 test/constructor.js diff --git a/index.d.ts b/index.d.ts index 268ab75..a1d2857 100644 --- a/index.d.ts +++ b/index.d.ts @@ -93,7 +93,7 @@ export interface Options { /** Return a new Chalk instance. */ -export type ChalkInstance = new (options?: Options) => Chalk; +export const Chalk: new (options?: Options) => ChalkInstance; /** Detect whether the terminal supports color. @@ -149,12 +149,7 @@ interface ChalkFunction { (...text: unknown[]): string; } -export interface Chalk extends ChalkFunction { - /** - Return a new Chalk instance. - */ - Instance: ChalkInstance; - +export interface ChalkInstance extends ChalkFunction { /** The color support for Chalk. @@ -180,7 +175,7 @@ export interface Chalk extends ChalkFunction { chalk.hex('#DEADED'); ``` */ - hex: (color: string) => Chalk; + hex: (color: string) => this; /** Use keyword color value to set text color. @@ -194,27 +189,27 @@ export interface Chalk extends ChalkFunction { chalk.keyword('orange'); ``` */ - keyword: (color: string) => Chalk; + keyword: (color: string) => this; /** Use RGB values to set text color. */ - rgb: (red: number, green: number, blue: number) => Chalk; + rgb: (red: number, green: number, blue: number) => this; /** Use HSL values to set text color. */ - hsl: (hue: number, saturation: number, lightness: number) => Chalk; + hsl: (hue: number, saturation: number, lightness: number) => this; /** Use HSV values to set text color. */ - hsv: (hue: number, saturation: number, value: number) => Chalk; + hsv: (hue: number, saturation: number, value: number) => this; /** Use HWB values to set text color. */ - hwb: (hue: number, whiteness: number, blackness: number) => Chalk; + hwb: (hue: number, whiteness: number, blackness: number) => this; /** Use a [Select/Set Graphic Rendition](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters) (SGR) [color code number](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit) to set text color. @@ -222,12 +217,12 @@ export interface Chalk extends ChalkFunction { 30 <= code && code < 38 || 90 <= code && code < 98 For example, 31 for red, 91 for redBright. */ - ansi: (code: number) => Chalk; + ansi: (code: number) => this; /** Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color. */ - ansi256: (index: number) => Chalk; + ansi256: (index: number) => this; /** Use HEX value to set background color. @@ -241,7 +236,7 @@ export interface Chalk extends ChalkFunction { chalk.bgHex('#DEADED'); ``` */ - bgHex: (color: string) => Chalk; + bgHex: (color: string) => this; /** Use keyword color value to set background color. @@ -255,27 +250,27 @@ export interface Chalk extends ChalkFunction { chalk.bgKeyword('orange'); ``` */ - bgKeyword: (color: string) => Chalk; + bgKeyword: (color: string) => this; /** Use RGB values to set background color. */ - bgRgb: (red: number, green: number, blue: number) => Chalk; + bgRgb: (red: number, green: number, blue: number) => this; /** Use HSL values to set background color. */ - bgHsl: (hue: number, saturation: number, lightness: number) => Chalk; + bgHsl: (hue: number, saturation: number, lightness: number) => this; /** Use HSV values to set background color. */ - bgHsv: (hue: number, saturation: number, value: number) => Chalk; + bgHsv: (hue: number, saturation: number, value: number) => this; /** Use HWB values to set background color. */ - bgHwb: (hue: number, whiteness: number, blackness: number) => Chalk; + bgHwb: (hue: number, whiteness: number, blackness: number) => this; /** Use a [Select/Set Graphic Rendition](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters) (SGR) [color code number](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit) to set background color. @@ -284,114 +279,114 @@ export interface Chalk extends ChalkFunction { For example, 31 for red, 91 for redBright. Use the foreground code, not the background code (for example, not 41, nor 101). */ - bgAnsi: (code: number) => Chalk; + bgAnsi: (code: number) => this; /** Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set background color. */ - bgAnsi256: (index: number) => Chalk; + bgAnsi256: (index: number) => this; /** Modifier: Resets the current color chain. */ - readonly reset: Chalk; + readonly reset: this; /** Modifier: Make text bold. */ - readonly bold: Chalk; + readonly bold: this; /** Modifier: Emitting only a small amount of light. */ - readonly dim: Chalk; + readonly dim: this; /** Modifier: Make text italic. (Not widely supported) */ - readonly italic: Chalk; + readonly italic: this; /** Modifier: Make text underline. (Not widely supported) */ - readonly underline: Chalk; + readonly underline: this; /** Modifier: Inverse background and foreground colors. */ - readonly inverse: Chalk; + readonly inverse: this; /** Modifier: Prints the text, but makes it invisible. */ - readonly hidden: Chalk; + readonly hidden: this; /** Modifier: Puts a horizontal line through the center of the text. (Not widely supported) */ - readonly strikethrough: Chalk; + readonly strikethrough: this; /** Modifier: Prints the text only when Chalk has a color support level > 0. Can be useful for things that are purely cosmetic. */ - readonly visible: Chalk; + readonly visible: this; - readonly black: Chalk; - readonly red: Chalk; - readonly green: Chalk; - readonly yellow: Chalk; - readonly blue: Chalk; - readonly magenta: Chalk; - readonly cyan: Chalk; - readonly white: Chalk; + readonly black: this; + readonly red: this; + readonly green: this; + readonly yellow: this; + readonly blue: this; + readonly magenta: this; + readonly cyan: this; + readonly white: this; /* Alias for `blackBright`. */ - readonly gray: Chalk; + readonly gray: this; /* Alias for `blackBright`. */ - readonly grey: Chalk; - - readonly blackBright: Chalk; - readonly redBright: Chalk; - readonly greenBright: Chalk; - readonly yellowBright: Chalk; - readonly blueBright: Chalk; - readonly magentaBright: Chalk; - readonly cyanBright: Chalk; - readonly whiteBright: Chalk; - - readonly bgBlack: Chalk; - readonly bgRed: Chalk; - readonly bgGreen: Chalk; - readonly bgYellow: Chalk; - readonly bgBlue: Chalk; - readonly bgMagenta: Chalk; - readonly bgCyan: Chalk; - readonly bgWhite: Chalk; + readonly grey: this; + + readonly blackBright: this; + readonly redBright: this; + readonly greenBright: this; + readonly yellowBright: this; + readonly blueBright: this; + readonly magentaBright: this; + readonly cyanBright: this; + readonly whiteBright: this; + + readonly bgBlack: this; + readonly bgRed: this; + readonly bgGreen: this; + readonly bgYellow: this; + readonly bgBlue: this; + readonly bgMagenta: this; + readonly bgCyan: this; + readonly bgWhite: this; /* Alias for `bgBlackBright`. */ - readonly bgGray: Chalk; + readonly bgGray: this; /* Alias for `bgBlackBright`. */ - readonly bgGrey: Chalk; + readonly bgGrey: this; - readonly bgBlackBright: Chalk; - readonly bgRedBright: Chalk; - readonly bgGreenBright: Chalk; - readonly bgYellowBright: Chalk; - readonly bgBlueBright: Chalk; - readonly bgMagentaBright: Chalk; - readonly bgCyanBright: Chalk; - readonly bgWhiteBright: Chalk; + readonly bgBlackBright: this; + readonly bgRedBright: this; + readonly bgGreenBright: this; + readonly bgYellowBright: this; + readonly bgBlueBright: this; + readonly bgMagentaBright: this; + readonly bgCyanBright: this; + readonly bgWhiteBright: this; } /** @@ -403,9 +398,11 @@ Order doesn't matter, and later styles take precedent in case of a conflict. This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`. */ -declare const chalk: Chalk & ChalkFunction & { - supportsColor: ColorSupport | false; - stderr: Chalk & {supportsColor: ColorSupport | false}; -}; +declare const chalk: ChalkInstance & ChalkFunction; + +export const supportsColor: ColorSupport | false; + +export const chalkStderr: typeof chalk; +export const supportsColorStderr: typeof supportsColor; export default chalk; diff --git a/index.test-d.ts b/index.test-d.ts index 292707e..3e7e2d6 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,35 +1,35 @@ import {expectType, expectAssignable, expectError} from 'tsd'; -import chalk, {Chalk, Color, ColorSupport, ColorSupportLevel} from './index.js'; +import chalk, {Chalk, ChalkInstance, Color, ColorSupport, ColorSupportLevel, chalkStderr, supportsColor, supportsColorStderr} from './index.js'; // - Helpers - -type colorReturn = Chalk & {supportsColor?: never}; +type colorReturn = ChalkInstance & {supportsColor?: never}; // - supportsColor - -expectType(chalk.supportsColor); -if (chalk.supportsColor) { - expectType(chalk.supportsColor.hasBasic); - expectType(chalk.supportsColor.has256); - expectType(chalk.supportsColor.has16m); +expectType(supportsColor); +if (supportsColor) { + expectType(supportsColor.hasBasic); + expectType(supportsColor.has256); + expectType(supportsColor.has16m); } // - stderr - -expectAssignable(chalk.stderr); -expectType(chalk.stderr.supportsColor); -if (chalk.stderr.supportsColor) { - expectType(chalk.stderr.supportsColor.hasBasic); - expectType(chalk.stderr.supportsColor.has256); - expectType(chalk.stderr.supportsColor.has16m); +expectAssignable(chalkStderr); +expectType(supportsColorStderr); +if (supportsColorStderr) { + expectType(supportsColorStderr.hasBasic); + expectType(supportsColorStderr.has256); + expectType(supportsColorStderr.has16m); } -// -- `stderr` is not a member of the Chalk interface -- -expectError(chalk.reset.stderr); +// -- `supportsColorStderr` is not a member of the Chalk interface -- +expectError(chalk.reset.supportsColorStderr); // -- `supportsColor` is not a member of the Chalk interface -- expectError(chalk.reset.supportsColor); // - Chalk - // -- Instance -- -expectType(new chalk.Instance({level: 1})); +expectType(new Chalk({level: 1})); // -- Properties -- expectType(chalk.level); diff --git a/readme.md b/readme.md index c48056c..45e4be7 100644 --- a/readme.md +++ b/readme.md @@ -171,9 +171,9 @@ Color support is automatically detected, but you can override it by setting the If you need to change this in a reusable module, create a new instance: ```js -import chalk from 'chalk'; +import {Chalk} from 'chalk'; -const customChalk = new chalk.Instance({level: 0}); +const customChalk = new Chalk({level: 0}); ``` | Level | Description | @@ -183,7 +183,7 @@ const customChalk = new chalk.Instance({level: 0}); | `2` | 256 color support | | `3` | Truecolor support (16 million colors) | -### chalk.supportsColor +### supportsColor Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience. @@ -191,9 +191,9 @@ Can be overridden by the user with the flags `--color` and `--no-color`. For sit Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively. -### chalk.stderr and chalk.stderr.supportsColor +### chalkStderr and supportsColorStderr -`chalk.stderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `chalk.supportsColor` apply to this too. `chalk.stderr.supportsColor` is exposed for convenience. +`chalkStderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `supportsColor` apply to this too. `supportsColorStderr` is exposed for convenience. ## Styles diff --git a/source/index.js b/source/index.js index 9a9986d..0f159d4 100644 --- a/source/index.js +++ b/source/index.js @@ -29,7 +29,7 @@ const applyOptions = (object, options = {}) => { object.level = options.level === undefined ? colorLevel : options.level; }; -class ChalkClass { +export class Chalk { constructor(options) { // eslint-disable-next-line no-constructor-return return chalkFactory(options); @@ -42,19 +42,15 @@ const chalkFactory = options => { chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_); - Object.setPrototypeOf(chalk, Chalk.prototype); + Object.setPrototypeOf(chalk, createChalk.prototype); Object.setPrototypeOf(chalk.template, chalk); - chalk.template.constructor = () => { - throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.'); - }; - - chalk.template.Instance = ChalkClass; + chalk.template.Chalk = Chalk; return chalk.template; }; -function Chalk(options) { +function createChalk(options) { return chalkFactory(options); } @@ -213,11 +209,14 @@ const chalkTag = (chalk, ...strings) => { return template(chalk, parts.join('')); }; -Object.defineProperties(Chalk.prototype, styles); +Object.defineProperties(createChalk.prototype, styles); -const chalk = Chalk(); // eslint-disable-line new-cap -chalk.supportsColor = stdoutColor; -chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap -chalk.stderr.supportsColor = stderrColor; +const chalk = createChalk(); +export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0}); + +export { + stdoutColor as supportsColor, + stderrColor as supportsColorStderr +}; export default chalk; diff --git a/test/_fixture.js b/test/_fixture.js index 1a8e8ba..c3e5b15 100644 --- a/test/_fixture.js +++ b/test/_fixture.js @@ -1,3 +1,3 @@ -import chalk from '../source/index.js'; +import chalk, {chalkStderr} from '../source/index.js'; -console.log(`${chalk.hex('#ff6159')('testout')} ${chalk.stderr.hex('#ff6159')('testerr')}`); +console.log(`${chalk.hex('#ff6159')('testout')} ${chalkStderr.hex('#ff6159')('testerr')}`); diff --git a/test/chalk.js b/test/chalk.js index 93e9c23..adc1aed 100644 --- a/test/chalk.js +++ b/test/chalk.js @@ -1,8 +1,8 @@ import test from 'ava'; -import chalk from '../source/index.js'; +import chalk, {Chalk, chalkStderr} from '../source/index.js'; chalk.level = 3; -chalk.stderr.level = 3; +chalkStderr.level = 3; console.log('TERM:', process.env.TERM || '[none]'); console.log('platform:', process.platform || '[unknown]'); @@ -94,26 +94,26 @@ test('line breaks should open and close colors with CRLF', t => { }); test('properly convert RGB to 16 colors on basic color terminals', t => { - t.is(new chalk.Instance({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m'); - t.is(new chalk.Instance({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m'); + t.is(new Chalk({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m'); + t.is(new Chalk({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m'); }); test('properly convert RGB to 256 colors on basic color terminals', t => { - t.is(new chalk.Instance({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m'); - t.is(new chalk.Instance({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m'); - t.is(new chalk.Instance({level: 3}).bgHex('#FF0000')('hello'), '\u001B[48;2;255;0;0mhello\u001B[49m'); + t.is(new Chalk({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m'); + t.is(new Chalk({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m'); + t.is(new Chalk({level: 3}).bgHex('#FF0000')('hello'), '\u001B[48;2;255;0;0mhello\u001B[49m'); }); test('don\'t emit RGB codes if level is 0', t => { - t.is(new chalk.Instance({level: 0}).hex('#FF0000')('hello'), 'hello'); - t.is(new chalk.Instance({level: 0}).bgHex('#FF0000')('hello'), 'hello'); + t.is(new Chalk({level: 0}).hex('#FF0000')('hello'), 'hello'); + t.is(new Chalk({level: 0}).bgHex('#FF0000')('hello'), 'hello'); }); test('supports blackBright color', t => { t.is(chalk.blackBright('foo'), '\u001B[90mfoo\u001B[39m'); }); -test('sets correct level for chalk.stderr and respects it', t => { - t.is(chalk.stderr.level, 3); - t.is(chalk.stderr.red.bold('foo'), '\u001B[31m\u001B[1mfoo\u001B[22m\u001B[39m'); +test('sets correct level for chalkStderr and respects it', t => { + t.is(chalkStderr.level, 3); + t.is(chalkStderr.red.bold('foo'), '\u001B[31m\u001B[1mfoo\u001B[22m\u001B[39m'); }); diff --git a/test/constructor.js b/test/constructor.js deleted file mode 100644 index 78ee070..0000000 --- a/test/constructor.js +++ /dev/null @@ -1,14 +0,0 @@ -import test from 'ava'; -import chalk from '../source/index.js'; - -test('Chalk.constructor should throw an expected error', t => { - const expectedError = t.throws(() => { - chalk.constructor(); - }); - - t.is(expectedError.message, '`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.'); - - t.throws(() => { - new chalk.constructor(); // eslint-disable-line no-new - }); -}); diff --git a/test/instance.js b/test/instance.js index c063109..c3cc70b 100644 --- a/test/instance.js +++ b/test/instance.js @@ -1,10 +1,10 @@ import test from 'ava'; -import chalk from '../source/index.js'; +import chalk, {Chalk} from '../source/index.js'; chalk.level = 1; test('create an isolated context where colors can be disabled (by level)', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance.red('foo'), 'foo'); t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m'); instance.level = 2; @@ -14,11 +14,11 @@ test('create an isolated context where colors can be disabled (by level)', t => test('the `level` option should be a number from 0 to 3', t => { /* eslint-disable no-new */ t.throws(() => { - new chalk.Instance({level: 10}); + new Chalk({level: 10}); }, {message: /should be an integer from 0 to 3/}); t.throws(() => { - new chalk.Instance({level: -1}); + new Chalk({level: -1}); }, {message: /should be an integer from 0 to 3/}); /* eslint-enable no-new */ }); diff --git a/test/template-literal.js b/test/template-literal.js index 95383c4..773e097 100644 --- a/test/template-literal.js +++ b/test/template-literal.js @@ -1,27 +1,27 @@ /* eslint-disable unicorn/no-hex-escape */ import test from 'ava'; -import chalk from '../source/index.js'; +import chalk, {Chalk} from '../source/index.js'; chalk.level = 1; test('return an empty string for an empty literal', t => { - const instance = new chalk.Instance(); + const instance = new Chalk(); t.is(instance``, ''); }); test('return a regular string for a literal with no templates', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance`hello`, 'hello'); }); test('correctly perform template parsing', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance`{bold Hello, {cyan World!} This is a} test. {green Woo!}`, instance.bold('Hello,', instance.cyan('World!'), 'This is a') + ' test. ' + instance.green('Woo!')); }); test('correctly perform template substitutions', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); const name = 'Sindre'; const exclamation = 'Neat'; t.is(instance`{bold Hello, {cyan.inverse ${name}!} This is a} test. {green ${exclamation}!}`, @@ -29,7 +29,7 @@ test('correctly perform template substitutions', t => { }); test('correctly perform nested template substitutions', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); const name = 'Sindre'; const exclamation = 'Neat'; t.is(instance.bold`Hello, {cyan.inverse ${name}!} This is a` + ' test. ' + instance.green`${exclamation}!`, @@ -48,7 +48,7 @@ test('correctly perform nested template substitutions', t => { }); test('correctly parse and evaluate color-convert functions', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); t.is(instance`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`, '\u001B[1m\u001B[38;2;144;10;178m\u001B[7mHello, ' + '\u001B[27m\u001B[39m\u001B[22m\u001B[1m' + @@ -61,13 +61,13 @@ test('correctly parse and evaluate color-convert functions', t => { }); test('properly handle escapes', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); t.is(instance`{bold hello \{in brackets\}}`, '\u001B[1mhello {in brackets}\u001B[22m'); }); test('throw if there is an unclosed block', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); try { console.log(instance`{bold this shouldn't appear ever\}`); t.fail(); @@ -84,7 +84,7 @@ test('throw if there is an unclosed block', t => { }); test('throw if there is an invalid style', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); try { console.log(instance`{abadstylethatdoesntexist this shouldn't appear ever}`); t.fail(); @@ -94,7 +94,7 @@ test('throw if there is an invalid style', t => { }); test('properly style multiline color blocks', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); t.is( instance`{bold Hello! This is a @@ -114,49 +114,49 @@ test('properly style multiline color blocks', t => { }); test('escape interpolated values', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance`Hello {bold hi}`, 'Hello hi'); t.is(instance`Hello ${'{bold hi}'}`, 'Hello {bold hi}'); }); test('allow custom colors (themes) on custom contexts', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); instance.rose = instance.hex('#F6D9D9'); t.is(instance`Hello, {rose Rose}.`, 'Hello, \u001B[38;2;246;217;217mRose\u001B[39m.'); }); test('correctly parse newline literals (bug #184)', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance`Hello {red there}`, 'Hello\nthere'); }); test('correctly parse newline escapes (bug #177)', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance`Hello\nthere!`, 'Hello\nthere!'); }); test('correctly parse escape in parameters (bug #177 comment 318622809)', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); const string = '\\'; t.is(instance`{blue ${string}}`, '\\'); }); test('correctly parses unicode/hex escapes', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance`\u0078ylophones are fo\x78y! {magenta.inverse \u0078ylophones are fo\x78y!}`, 'xylophones are foxy! xylophones are foxy!'); }); test('correctly parses string arguments', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); t.is(instance`{keyword('black').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m'); t.is(instance`{keyword('blac\x6B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m'); t.is(instance`{keyword('blac\u006B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m'); }); test('throws if a bad argument is encountered', t => { - const instance = new chalk.Instance({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances + const instance = new Chalk({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances try { console.log(instance`{keyword(????) hi}`); t.fail(); @@ -166,7 +166,7 @@ test('throws if a bad argument is encountered', t => { }); test('throws if an extra unescaped } is found', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); try { console.log(instance`{red hi!}}`); t.fail(); @@ -176,18 +176,18 @@ test('throws if an extra unescaped } is found', t => { }); test('should not parse upper-case escapes', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance`\N\n\T\t\X07\x07\U000A\u000A\U000a\u000A`, 'N\nT\tX07\x07U000A\u000AU000a\u000A'); }); test('should properly handle undefined template interpolated values', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance`hello ${undefined}`, 'hello undefined'); t.is(instance`hello ${null}`, 'hello null'); }); test('should allow bracketed Unicode escapes', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); t.is(instance`\u{AB}`, '\u{AB}'); t.is(instance`This is a {bold \u{AB681}} test`, 'This is a \u001B[1m\u{AB681}\u001B[22m test'); t.is(instance`This is a {bold \u{10FFFF}} test`, 'This is a \u001B[1m\u{10FFFF}\u001B[22m test'); diff --git a/test/visible.js b/test/visible.js index cd9e46d..4d34af8 100644 --- a/test/visible.js +++ b/test/visible.js @@ -1,22 +1,22 @@ import test from 'ava'; -import chalk from '../source/index.js'; +import chalk, {Chalk} from '../source/index.js'; chalk.level = 1; test('visible: normal output when level > 0', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m'); }); test('visible: no output when level is too low', t => { - const instance = new chalk.Instance({level: 0}); + const instance = new Chalk({level: 0}); t.is(instance.visible.red('foo'), ''); t.is(instance.red.visible('foo'), ''); }); test('test switching back and forth between level == 0 and level > 0', t => { - const instance = new chalk.Instance({level: 3}); + const instance = new Chalk({level: 3}); t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');