Add named exports (#432)

This commit is contained in:
Richie Bendall 2021-04-18 00:33:03 +12:00 committed by GitHub
parent fa16f4ec37
commit d798222a5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 147 additions and 165 deletions

View file

@ -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')}`);

View file

@ -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');
});

View file

@ -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
});
});

View file

@ -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 */
});

View file

@ -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');

View file

@ -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');