template literals with a style tag, fixes #341
This commit is contained in:
parent
797461ee32
commit
7d238e2ebb
2 changed files with 13 additions and 0 deletions
|
|
@ -134,6 +134,11 @@ 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}}`
|
||||
return applyStyle(builder, chalkTag(builder, ...arguments_));
|
||||
}
|
||||
|
||||
// Single argument is hot path, implicit coercion is faster than anything
|
||||
// eslint-disable-next-line no-implicit-coercion
|
||||
return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
|
||||
|
|
|
|||
|
|
@ -30,6 +30,14 @@ test('correctly perform template substitutions', t => {
|
|||
instance.bold('Hello,', instance.cyan.inverse(name + '!'), 'This is a') + ' test. ' + instance.green(exclamation + '!'));
|
||||
});
|
||||
|
||||
test('correctly perform nested template substitutions', t => {
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
const name = 'Sindre';
|
||||
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 + '!'));
|
||||
});
|
||||
|
||||
test('correctly parse and evaluate color-convert functions', t => {
|
||||
const instance = new chalk.Instance({level: 3});
|
||||
t.is(instance`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue