Skip to content

Commit 69ac663

Browse files
committed
Fix undefined and null interpolated expressions (fixes #194)
1 parent 106f086 commit 69ac663

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ function chalkTag(chalk, strings) {
207207
const parts = [strings.raw[0]];
208208

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

214214
return template(chalk, parts.join(''));

test/template-literal.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,9 @@ test('should not parse upper-case escapes', t => {
161161
const ctx = m.constructor({level: 0});
162162
t.is(ctx`\N\n\T\t\X07\x07\U000A\u000A\U000a\u000a`, 'N\nT\tX07\x07U000A\u000AU000a\u000A');
163163
});
164+
165+
test('should properly handle undefined template interpolated values', t => {
166+
const ctx = m.constructor({level: 0});
167+
t.is(ctx`hello ${undefined}`, 'hello undefined');
168+
t.is(ctx`hello ${null}`, 'hello null');
169+
});

0 commit comments

Comments
 (0)