From e5ea8df1e52a6265caea6f6cab909892ec88765d Mon Sep 17 00:00:00 2001 From: Toon Baeyens Date: Wed, 6 May 2020 15:24:58 +0200 Subject: [PATCH] extra tests, specific benchmark, example in readme --- benchmark.js | 12 ++++++++++++ index.test-d.ts | 5 +++++ readme.md | 3 ++- source/index.js | 2 +- test/template-literal.js | 6 ++++++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/benchmark.js b/benchmark.js index dc24696..25b3f5d 100644 --- a/benchmark.js +++ b/benchmark.js @@ -47,4 +47,16 @@ suite('chalk', () => { bench('cached: 1 style nested non-intersecting', () => { chalkBgRed(blueStyledString); }); + + set('iterations', 10000); + + bench('cached: 1 style template literal', () => { + // eslint-disable-next-line no-unused-expressions + chalkRed`the fox jumps over the lazy dog`; + }); + + bench('cached: nested styles template literal', () => { + // eslint-disable-next-line no-unused-expressions + chalkRed`the fox {bold jumps} over the {underline lazy} dog`; + }); }); diff --git a/index.test-d.ts b/index.test-d.ts index 177d6de..a227dc4 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -154,6 +154,11 @@ expectType(chalk.bgWhiteBright`foo`); expectType(chalk.red.bgGreen.underline('foo')); expectType(chalk.underline.red.bgGreen('foo')); +// -- Complex template literal -- +expectType(chalk.underline``); +expectType(chalk.red.bgGreen.bold`Hello {italic.blue ${name}}`); +expectType(chalk.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`); + // -- Color types == expectType('red'); expectError('hotpink'); diff --git a/readme.md b/readme.md index a0ca245..faca767 100644 --- a/readme.md +++ b/readme.md @@ -215,10 +215,11 @@ console.log(chalk` Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`). -Template styles are chained exactly like normal Chalk styles. The following two statements are equivalent: +Template styles are chained exactly like normal Chalk styles. The following three statements are equivalent: ```js console.log(chalk.bold.rgb(10, 100, 200)('Hello!')); +console.log(chalk.bold.rgb(10, 100, 200)`Hello!`); console.log(chalk`{bold.rgb(10,100,200) Hello!}`); ``` diff --git a/source/index.js b/source/index.js index 73135fe..1486dc3 100644 --- a/source/index.js +++ b/source/index.js @@ -135,7 +135,7 @@ const createStyler = (open, close, parent) => { const createBuilder = (self, _styler, _isEmpty) => { const builder = (...arguments_) => { if (Array.isArray(arguments_[0])) { - // Called as a template litteral, e.g. chalk.red`2 + 3 = {bold ${2+3}}` + // Called as a template literal, e.g. chalk.red`2 + 3 = {bold ${2+3}}` return applyStyle(builder, chalkTag(builder, ...arguments_)); } diff --git a/test/template-literal.js b/test/template-literal.js index dc4b67b..da0ed24 100644 --- a/test/template-literal.js +++ b/test/template-literal.js @@ -36,6 +36,12 @@ test('correctly perform nested template substitutions', t => { const exclamation = 'Neat'; t.is(instance.bold`Hello, {cyan.inverse ${name}!} This is a` + ' test. ' + instance.green`${exclamation}!`, instance.bold('Hello,', instance.cyan.inverse(name + '!'), 'This is a') + ' test. ' + instance.green(exclamation + '!')); + + t.is(instance.red.bgGreen.bold`Hello {italic.blue ${name}}`, + instance.red.bgGreen.bold('Hello ' + instance.italic.blue(name))); + + t.is(instance.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`, + instance.strikethrough.cyanBright.bgBlack('Works with ' + instance.reset.bold('numbers') + ' ' + instance.bold.red(1))); }); test('correctly parse and evaluate color-convert functions', t => {