From 69ac663f29e92d309b0b221e117ccb6be4ed4ecd Mon Sep 17 00:00:00 2001 From: Josh Junon Date: Tue, 1 Aug 2017 18:55:54 -0700 Subject: [PATCH] Fix undefined and null interpolated expressions (fixes #194) --- index.js | 4 ++-- test/template-literal.js | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ab3831c..4c81d6d 100644 --- a/index.js +++ b/index.js @@ -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('')); diff --git a/test/template-literal.js b/test/template-literal.js index 6ab17a6..bfa346c 100644 --- a/test/template-literal.js +++ b/test/template-literal.js @@ -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'); +});