Require Node.js 6

This commit is contained in:
Sindre Sorhus 2018-09-18 14:05:08 +07:00
parent 70f22d87ba
commit 0307f263cb
13 changed files with 204 additions and 216 deletions

3
.gitattributes vendored
View file

@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf

View file

@ -2,6 +2,5 @@ language: node_js
node_js:
- '8'
- '6'
- '4'
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'

View file

@ -1,4 +1,4 @@
/* globals set bench */
/* globals suite, set, bench */
'use strict';
const chalk = require('.');

View file

@ -31,10 +31,7 @@ function Chalk(options) {
const chalk = {};
applyOptions(chalk, options);
chalk.template = function () {
const args = [].slice.call(arguments);
return chalkTag.apply(null, [chalk.template].concat(args));
};
chalk.template = (...args) => chalkTag(...[chalk.template].concat(args));
Object.setPrototypeOf(chalk, Chalk.prototype);
Object.setPrototypeOf(chalk.template, chalk);
@ -77,9 +74,9 @@ for (const model of Object.keys(ansiStyles.color.ansi)) {
styles[model] = {
get() {
const level = this.level;
return function () {
const open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments);
const {level} = this;
return function (...args) {
const open = ansiStyles.color[levelMapping[level]][model](...args);
const codes = {
open,
close: ansiStyles.color.close,
@ -100,9 +97,9 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
styles[bgModel] = {
get() {
const level = this.level;
return function () {
const open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments);
const {level} = this;
return function (...args) {
const open = ansiStyles.bgColor[levelMapping[level]][model](...args);
const codes = {
open,
close: ansiStyles.bgColor.close,
@ -117,10 +114,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
const proto = Object.defineProperties(() => {}, styles);
function build(_styles, _empty, key) {
const builder = function () {
return applyStyle.apply(builder, arguments);
};
const builder = (...args) => applyStyle.call(builder, ...args);
builder._styles = _styles;
builder._empty = _empty;
@ -156,11 +150,10 @@ function build(_styles, _empty, key) {
return builder;
}
function applyStyle() {
function applyStyle(...args) {
// Support varags, but simply cast to string in case there's only one arg
const args = arguments;
const argsLen = args.length;
let str = String(arguments[0]);
let str = String(args[0]);
if (argsLen === 0) {
return '';
@ -203,19 +196,21 @@ function applyStyle() {
return str;
}
function chalkTag(chalk, strings) {
if (!Array.isArray(strings)) {
function chalkTag(chalk, ...strings) {
const firstString = strings[0];
if (!Array.isArray(firstString)) {
// If chalk() was called by itself or with a string,
// return the string itself as a string.
return [].slice.call(arguments, 1).join(' ');
return strings.join(' ');
}
const args = [].slice.call(arguments, 2);
const parts = [strings.raw[0]];
const args = strings.slice(1);
const parts = [firstString.raw[0]];
for (let i = 1; i < strings.length; i++) {
for (let i = 1; i < firstString.length; i++) {
parts.push(String(args[i - 1]).replace(/[{}\\]/g, '\\$&'));
parts.push(String(strings.raw[i]));
parts.push(String(firstString.raw[i]));
}
return template(chalk, parts.join(''));

View file

@ -5,7 +5,7 @@
"license": "MIT",
"repository": "chalk/chalk",
"engines": {
"node": ">=4"
"node": ">=6"
},
"scripts": {
"test": "xo && nyc ava && tsc --project types && flow --max-warnings=0",
@ -43,26 +43,22 @@
"dependencies": {
"ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5",
"supports-color": "^5.3.0"
"supports-color": "^5.5.0"
},
"devDependencies": {
"ava": "0.25.0",
"coveralls": "^3.0.0",
"execa": "^0.10.0",
"flow-bin": "^0.73.0",
"ava": "^0.25.0",
"coveralls": "^3.0.2",
"execa": "^1.0.0",
"flow-bin": "^0.81.0",
"import-fresh": "^2.0.0",
"matcha": "^0.7.0",
"nyc": "^11.0.2",
"nyc": "^13.0.1",
"resolve-from": "^4.0.0",
"typescript": "^2.5.3",
"xo": "0.20.3"
"typescript": "^3.0.3",
"xo": "^0.23.0"
},
"types": "types/index.d.ts",
"xo": {
"envs": [
"node",
"mocha"
],
"ignores": [
"test/_flow.js"
]

View file

@ -80,7 +80,7 @@ function buildStyle(chalk, styles) {
}
if (enabled[styleName].length > 0) {
current = current[styleName].apply(current, enabled[styleName]);
current = current[styleName](...enabled[styleName]);
} else {
current = current[styleName];
}

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

View file

@ -3,20 +3,20 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('create an isolated context where colors can be disabled (by level)', t => {
const ctx = new m.constructor({level: 0, enabled: true});
t.is(ctx.red('foo'), 'foo');
t.is(m.red('foo'), '\u001B[31mfoo\u001B[39m');
ctx.level = 2;
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
const instance = new chalk.constructor({level: 0, enabled: true});
t.is(instance.red('foo'), 'foo');
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.level = 2;
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
});
test('create an isolated context where colors can be disabled (by enabled flag)', t => {
const ctx = new m.constructor({enabled: false});
t.is(ctx.red('foo'), 'foo');
t.is(m.red('foo'), '\u001B[31mfoo\u001B[39m');
ctx.enabled = true;
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
const instance = new chalk.constructor({enabled: false});
t.is(instance.red('foo'), 'foo');
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.enabled = true;
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
});

View file

@ -3,33 +3,33 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('don\'t output colors when manually disabled', t => {
m.enabled = false;
t.is(m.red('foo'), 'foo');
m.enabled = true;
chalk.enabled = false;
t.is(chalk.red('foo'), 'foo');
chalk.enabled = true;
});
test('enable/disable colors based on overall chalk enabled property, not individual instances', t => {
m.enabled = false;
const red = m.red;
chalk.enabled = false;
const {red} = chalk;
t.false(red.enabled);
m.enabled = true;
chalk.enabled = true;
t.true(red.enabled);
m.enabled = true;
chalk.enabled = true;
});
test('propagate enable/disable changes from child colors', t => {
m.enabled = false;
const red = m.red;
chalk.enabled = false;
const {red} = chalk;
t.false(red.enabled);
t.false(m.enabled);
t.false(chalk.enabled);
red.enabled = true;
t.true(red.enabled);
t.true(m.enabled);
m.enabled = false;
t.true(chalk.enabled);
chalk.enabled = false;
t.false(red.enabled);
t.false(m.enabled);
m.enabled = true;
t.false(chalk.enabled);
chalk.enabled = true;
});

View file

@ -5,38 +5,38 @@ import execa from 'execa';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('don\'t output colors when manually disabled', t => {
const oldLevel = m.level;
m.level = 0;
t.is(m.red('foo'), 'foo');
m.level = oldLevel;
const oldLevel = chalk.level;
chalk.level = 0;
t.is(chalk.red('foo'), 'foo');
chalk.level = oldLevel;
});
test('enable/disable colors based on overall chalk enabled property, not individual instances', t => {
const oldLevel = m.level;
m.level = 1;
const red = m.red;
const oldLevel = chalk.level;
chalk.level = 1;
const {red} = chalk;
t.is(red.level, 1);
m.level = 0;
t.is(red.level, m.level);
m.level = oldLevel;
chalk.level = 0;
t.is(red.level, chalk.level);
chalk.level = oldLevel;
});
test('propagate enable/disable changes from child colors', t => {
const oldLevel = m.level;
m.level = 1;
const red = m.red;
const oldLevel = chalk.level;
chalk.level = 1;
const {red} = chalk;
t.is(red.level, 1);
t.is(m.level, 1);
t.is(chalk.level, 1);
red.level = 0;
t.is(red.level, 0);
t.is(m.level, 0);
m.level = 1;
t.is(chalk.level, 0);
chalk.level = 1;
t.is(red.level, 1);
t.is(m.level, 1);
m.level = oldLevel;
t.is(chalk.level, 1);
chalk.level = oldLevel;
});
test('disable colors if they are not supported', async t => {

View file

@ -4,82 +4,82 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('return an empty string for an empty literal', t => {
const ctx = m.constructor();
t.is(ctx``, '');
const instance = chalk.constructor();
t.is(instance``, '');
});
test('return a regular string for a literal with no templates', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`hello`, 'hello');
const instance = chalk.constructor({level: 0});
t.is(instance`hello`, 'hello');
});
test('correctly perform template parsing', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`{bold Hello, {cyan World!} This is a} test. {green Woo!}`,
ctx.bold('Hello,', ctx.cyan('World!'), 'This is a') + ' test. ' + ctx.green('Woo!'));
const instance = chalk.constructor({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 ctx = m.constructor({level: 0});
const instance = chalk.constructor({level: 0});
const name = 'Sindre';
const exclamation = 'Neat';
t.is(ctx`{bold Hello, {cyan.inverse ${name}!} This is a} test. {green ${exclamation}!}`,
ctx.bold('Hello,', ctx.cyan.inverse(name + '!'), 'This is a') + ' test. ' + ctx.green(exclamation + '!'));
t.is(instance`{bold Hello, {cyan.inverse ${name}!} This is a} test. {green ${exclamation}!}`,
instance.bold('Hello,', instance.cyan.inverse(name + '!'), 'This is a') + ' test. ' + instance.green(exclamation + '!'));
});
test('correctly parse and evaluate color-convert functions', t => {
const ctx = m.constructor({level: 3});
t.is(ctx`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`,
const instance = chalk.constructor({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' +
'\u001B[38;2;144;10;178mthere!\u001B[39m\u001B[22m');
t.is(ctx`{bold.bgRgb(144,10,178).inverse Hello, {~inverse there!}}`,
t.is(instance`{bold.bgRgb(144,10,178).inverse Hello, {~inverse there!}}`,
'\u001B[1m\u001B[48;2;144;10;178m\u001B[7mHello, ' +
'\u001B[27m\u001B[49m\u001B[22m\u001B[1m' +
'\u001B[48;2;144;10;178mthere!\u001B[49m\u001B[22m');
});
test('properly handle escapes', t => {
const ctx = m.constructor({level: 3});
t.is(ctx`{bold hello \{in brackets\}}`,
const instance = chalk.constructor({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 ctx = m.constructor({level: 3});
const instance = chalk.constructor({level: 3});
try {
console.log(ctx`{bold this shouldn't appear ever\}`);
console.log(instance`{bold this shouldn't appear ever\}`);
t.fail();
} catch (err) {
t.is(err.message, 'Chalk template literal is missing 1 closing bracket (`}`)');
} catch (error) {
t.is(error.message, 'Chalk template literal is missing 1 closing bracket (`}`)');
}
try {
console.log(ctx`{bold this shouldn't {inverse appear {underline ever\} :) \}`);
console.log(instance`{bold this shouldn't {inverse appear {underline ever\} :) \}`);
t.fail();
} catch (err) {
t.is(err.message, 'Chalk template literal is missing 3 closing brackets (`}`)');
} catch (error) {
t.is(error.message, 'Chalk template literal is missing 3 closing brackets (`}`)');
}
});
test('throw if there is an invalid style', t => {
const ctx = m.constructor({level: 3});
const instance = chalk.constructor({level: 3});
try {
console.log(ctx`{abadstylethatdoesntexist this shouldn't appear ever}`);
console.log(instance`{abadstylethatdoesntexist this shouldn't appear ever}`);
t.fail();
} catch (err) {
t.is(err.message, 'Unknown Chalk style: abadstylethatdoesntexist');
} catch (error) {
t.is(error.message, 'Unknown Chalk style: abadstylethatdoesntexist');
}
});
test('properly style multiline color blocks', t => {
const ctx = m.constructor({level: 3});
const instance = chalk.constructor({level: 3});
t.is(
ctx`{bold
instance`{bold
Hello! This is a
${'multiline'} block!
:)
@ -97,74 +97,74 @@ test('properly style multiline color blocks', t => {
});
test('escape interpolated values', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`Hello {bold hi}`, 'Hello hi');
t.is(ctx`Hello ${'{bold hi}'}`, 'Hello {bold hi}');
const instance = chalk.constructor({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 ctx = m.constructor({level: 3});
ctx.rose = ctx.hex('#F6D9D9');
t.is(ctx`Hello, {rose Rose}.`, 'Hello, \u001B[38;2;246;217;217mRose\u001B[39m.');
const instance = chalk.constructor({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 ctx = m.constructor({level: 0});
t.is(ctx`Hello
const instance = chalk.constructor({level: 0});
t.is(instance`Hello
{red there}`, 'Hello\nthere');
});
test('correctly parse newline escapes (bug #177)', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`Hello\nthere!`, `Hello\nthere!`);
const instance = chalk.constructor({level: 0});
t.is(instance`Hello\nthere!`, 'Hello\nthere!');
});
test('correctly parse escape in parameters (bug #177 comment 318622809)', t => {
const ctx = m.constructor({level: 0});
const instance = chalk.constructor({level: 0});
const str = '\\';
t.is(ctx`{blue ${str}}`, '\\');
t.is(instance`{blue ${str}}`, '\\');
});
test('correctly parses unicode/hex escapes', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`\u0078ylophones are fo\x78y! {magenta.inverse \u0078ylophones are fo\x78y!}`,
const instance = chalk.constructor({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 ctx = m.constructor({level: 3});
t.is(ctx`{keyword('black').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
t.is(ctx`{keyword('blac\x6B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
t.is(ctx`{keyword('blac\u006B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
const instance = chalk.constructor({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 ctx = m.constructor({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances
const instance = chalk.constructor({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances
try {
console.log(ctx`{keyword(????) hi}`);
console.log(instance`{keyword(????) hi}`);
t.fail();
} catch (err) {
t.is(err.message, 'Invalid Chalk template style argument: ???? (in style \'keyword\')');
} catch (error) {
t.is(error.message, 'Invalid Chalk template style argument: ???? (in style \'keyword\')');
}
});
test('throws if an extra unescaped } is found', t => {
const ctx = m.constructor({level: 0});
const instance = chalk.constructor({level: 0});
try {
console.log(ctx`{red hi!}}`);
console.log(instance`{red hi!}}`);
t.fail();
} catch (err) {
t.is(err.message, 'Found extraneous } in Chalk template literal');
} catch (error) {
t.is(error.message, 'Found extraneous } in Chalk template literal');
}
});
test('should not parse upper-case escapes', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`\N\n\T\t\X07\x07\U000A\u000A\U000a\u000a`, 'N\nT\tX07\x07U000A\u000AU000a\u000A');
const instance = chalk.constructor({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 ctx = m.constructor({level: 0});
t.is(ctx`hello ${undefined}`, 'hello undefined');
t.is(ctx`hello ${null}`, 'hello null');
const instance = chalk.constructor({level: 0});
t.is(instance`hello ${undefined}`, 'hello undefined');
t.is(instance`hello ${null}`, 'hello null');
});

View file

@ -3,45 +3,45 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('visible: normal output when enabled', t => {
const ctx = new m.constructor({level: 3, enabled: true});
t.is(ctx.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
const instance = new chalk.constructor({level: 3, enabled: true});
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 disabled', t => {
const ctx = new m.constructor({level: 3, enabled: false});
t.is(ctx.red.visible('foo'), '');
t.is(ctx.visible.red('foo'), '');
const instance = new chalk.constructor({level: 3, enabled: false});
t.is(instance.red.visible('foo'), '');
t.is(instance.visible.red('foo'), '');
});
test('visible: no output when level is too low', t => {
const ctx = new m.constructor({level: 0, enabled: true});
t.is(ctx.visible.red('foo'), '');
t.is(ctx.red.visible('foo'), '');
const instance = new chalk.constructor({level: 0, enabled: true});
t.is(instance.visible.red('foo'), '');
t.is(instance.red.visible('foo'), '');
});
test('test switching back and forth between enabled and disabled', t => {
const ctx = new m.constructor({level: 3, enabled: true});
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.visible('foo'), 'foo');
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
const instance = new chalk.constructor({level: 3, enabled: true});
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');
t.is(instance.visible('foo'), 'foo');
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
ctx.enabled = false;
t.is(ctx.red('foo'), 'foo');
t.is(ctx.visible('foo'), '');
t.is(ctx.visible.red('foo'), '');
t.is(ctx.red.visible('foo'), '');
t.is(ctx.red('foo'), 'foo');
instance.enabled = false;
t.is(instance.red('foo'), 'foo');
t.is(instance.visible('foo'), '');
t.is(instance.visible.red('foo'), '');
t.is(instance.red.visible('foo'), '');
t.is(instance.red('foo'), 'foo');
ctx.enabled = true;
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.visible('foo'), 'foo');
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.enabled = true;
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');
t.is(instance.visible('foo'), 'foo');
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
});

View file

@ -28,36 +28,36 @@ test.beforeEach(() => {
test('detect a simple term if TERM isn\'t set', t => {
delete process.env.TERM;
const m = importFresh('..');
t.is(m.blue('foo'), '\u001B[94mfoo\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.blue('foo'), '\u001B[94mfoo\u001B[39m');
});
test('replace blue foreground color in cmd.exe', t => {
process.env.TERM = 'dumb';
const m = importFresh('..');
t.is(m.blue('foo'), '\u001B[94mfoo\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.blue('foo'), '\u001B[94mfoo\u001B[39m');
});
test('don\'t replace blue foreground color in xterm based terminals', t => {
process.env.TERM = 'xterm-256color';
const m = importFresh('..');
t.is(m.blue('foo'), '\u001B[34mfoo\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.blue('foo'), '\u001B[34mfoo\u001B[39m');
});
test('don\'t apply dimmed styling on gray strings, see https://github.com/chalk/chalk/issues/58', t => {
process.env.TERM = 'dumb';
const m = importFresh('..');
t.is(m.gray.dim('foo'), '\u001B[90mfoo\u001B[22m\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.gray.dim('foo'), '\u001B[90mfoo\u001B[22m\u001B[39m');
});
test('apply dimmed styling on xterm compatible terminals', t => {
process.env.TERM = 'xterm';
const m = importFresh('..');
t.is(m.gray.dim('foo'), '\u001B[90m\u001B[2mfoo\u001B[22m\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.gray.dim('foo'), '\u001B[90m\u001B[2mfoo\u001B[22m\u001B[39m');
});
test('apply dimmed styling on strings of other colors', t => {
process.env.TERM = 'dumb';
const m = importFresh('..');
t.is(m.blue.dim('foo'), '\u001B[94m\u001B[2mfoo\u001B[22m\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.blue.dim('foo'), '\u001B[94m\u001B[2mfoo\u001B[22m\u001B[39m');
});