* Fix XO linting and update some dev dependencies

Fixes #275

* Add some badges to the readme

* Tiny travis.yml tweak

* Require Node.js 6

* Validate the `level` option

Fixes #248

* Add failing test for #234 (#235)

* Add type definitions badge (#286)

* Add Tidelift mention in the readme

* Replace RawGit URL

Fixes #305

* Fix ignore chars regex flags in rainbow example (#306)

Use global matches rather than stopping after the first match.

* Strict mode in Flow definition (#309)

* Add security section

* Add docs comments and tests for TypeScript definitions (#299)

Fixes #293

* Update dependencies and meta tweaks

* Type definition improvements

* Enforce `chalk.constructor` to be called with `new` in TypeScript

* Add extra level/enabled property info in the readme (#308)

* Code style tweaks

* Change tagged template literal argument type to accept `unknown` instead of just `string` (#316)
This commit is contained in:
l198881 2021-05-26 15:23:19 -03:00 committed by GitHub
parent 9776a2ae5b
commit 029b69e482
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 760 additions and 479 deletions

View file

@ -3,51 +3,50 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
console.log('TERM:', process.env.TERM || '[none]');
console.log('platform:', process.platform || '[unknown]');
test('don\'t add any styling when called as the base function', t => {
t.is(m('foo'), 'foo');
t.is(chalk('foo'), 'foo');
});
test('support multiple arguments in base function', t => {
t.is(m('hello', 'there'), 'hello there');
t.is(chalk('hello', 'there'), 'hello there');
});
test('style string', t => {
t.is(m.underline('foo'), '\u001B[4mfoo\u001B[24m');
t.is(m.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(m.bgRed('foo'), '\u001B[41mfoo\u001B[49m');
t.is(chalk.underline('foo'), '\u001B[4mfoo\u001B[24m');
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(chalk.bgRed('foo'), '\u001B[41mfoo\u001B[49m');
});
test('support applying multiple styles at once', t => {
t.is(m.red.bgGreen.underline('foo'), '\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39m');
t.is(m.underline.red.bgGreen('foo'), '\u001B[4m\u001B[31m\u001B[42mfoo\u001B[49m\u001B[39m\u001B[24m');
t.is(chalk.red.bgGreen.underline('foo'), '\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39m');
t.is(chalk.underline.red.bgGreen('foo'), '\u001B[4m\u001B[31m\u001B[42mfoo\u001B[49m\u001B[39m\u001B[24m');
});
test('support nesting styles', t => {
t.is(
m.red('foo' + m.underline.bgBlue('bar') + '!'),
chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'),
'\u001B[31mfoo\u001B[4m\u001B[44mbar\u001B[49m\u001B[24m!\u001B[39m'
);
});
test('support nesting styles of the same type (color, underline, bg)', t => {
t.is(
m.red('a' + m.yellow('b' + m.green('c') + 'b') + 'c'),
chalk.red('a' + chalk.yellow('b' + chalk.green('c') + 'b') + 'c'),
'\u001B[31ma\u001B[33mb\u001B[32mc\u001B[33mb\u001B[31mc\u001B[39m'
);
});
test('reset all styles with `.reset()`', t => {
t.is(m.reset(m.red.bgGreen.underline('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
t.is(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
});
test('support caching multiple styles', t => {
const red = m.red;
const green = m.green;
const {red, green} = chalk.red;
const redBold = red.bold;
const greenBold = green.bold;
@ -57,44 +56,44 @@ test('support caching multiple styles', t => {
});
test('alias gray to grey', t => {
t.is(m.grey('foo'), '\u001B[90mfoo\u001B[39m');
t.is(chalk.grey('foo'), '\u001B[90mfoo\u001B[39m');
});
test('support variable number of arguments', t => {
t.is(m.red('foo', 'bar'), '\u001B[31mfoo bar\u001B[39m');
t.is(chalk.red('foo', 'bar'), '\u001B[31mfoo bar\u001B[39m');
});
test('support falsy values', t => {
t.is(m.red(0), '\u001B[31m0\u001B[39m');
t.is(chalk.red(0), '\u001B[31m0\u001B[39m');
});
test('don\'t output escape codes if the input is empty', t => {
t.is(m.red(), '');
t.is(m.red.blue.black(), '');
t.is(chalk.red(), '');
t.is(chalk.red.blue.black(), '');
});
test('keep Function.prototype methods', t => {
t.is(m.grey.apply(null, ['foo']), '\u001B[90mfoo\u001B[39m');
t.is(m.reset(m.red.bgGreen.underline.bind(null)('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
t.is(m.red.blue.black.call(null), '');
t.is(chalk.grey.apply(null, ['foo']), '\u001B[90mfoo\u001B[39m');
t.is(chalk.reset(chalk.red.bgGreen.underline.bind(null)('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
t.is(chalk.red.blue.black.call(null), '');
});
test('line breaks should open and close colors', t => {
t.is(m.grey('hello\nworld'), '\u001B[90mhello\u001B[39m\n\u001B[90mworld\u001B[39m');
t.is(chalk.grey('hello\nworld'), '\u001B[90mhello\u001B[39m\n\u001B[90mworld\u001B[39m');
});
test('properly convert RGB to 16 colors on basic color terminals', t => {
t.is(new m.constructor({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m');
t.is(new m.constructor({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m');
t.is(new chalk.constructor({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m');
t.is(new chalk.constructor({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m');
});
test('properly convert RGB to 256 colors on basic color terminals', t => {
t.is(new m.constructor({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
t.is(new m.constructor({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
t.is(new m.constructor({level: 3}).bgHex('#FF0000')('hello'), '\u001B[48;2;255;0;0mhello\u001B[49m');
t.is(new chalk.constructor({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
t.is(new chalk.constructor({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
t.is(new chalk.constructor({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 m.constructor({level: 0}).hex('#FF0000')('hello'), 'hello');
t.is(new m.constructor({level: 0}).bgHex('#FF0000')('hello'), 'hello');
t.is(new chalk.constructor({level: 0}).hex('#FF0000')('hello'), 'hello');
t.is(new chalk.constructor({level: 0}).bgHex('#FF0000')('hello'), 'hello');
});