Skip to content

Commit

Permalink
Fix undefined and null interpolated expressions (fixes #194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Aug 7, 2017
1 parent 106f086 commit 69ac663
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ function chalkTag(chalk, strings) {
const parts = [strings.raw[0]];

for (let i = 1; i < strings.length; i++) {
parts.push(args[i - 1].toString().replace(/[{}\\]/g, '\\$&'));
parts.push(strings.raw[i]);
parts.push(String(args[i - 1]).replace(/[{}\\]/g, '\\$&'));
parts.push(String(strings.raw[i]));
}

return template(chalk, parts.join(''));
Expand Down
6 changes: 6 additions & 0 deletions test/template-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,9 @@ 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');
});

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

0 comments on commit 69ac663

Please sign in to comment.