From 7cb9ab8111151dedcb6ff560e9671dd2f7f58c48 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 27 Jul 2019 17:48:45 +0200 Subject: [PATCH] Fix handling of falsy values Fixes #10 --- index.d.ts | 2 +- index.js | 4 ++-- test.js | 2 ++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 28b78ae..762aae0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -26,7 +26,7 @@ pupa('I like {{0}} and {{1}}', ['
🦄
', '🐮']); */ declare function pupa( template: string, - data: unknown[] | {[key: string]: unknown} + data: unknown[] | {[key: string]: any} ): string; export = pupa; diff --git a/index.js b/index.js index 4555579..c8dc040 100644 --- a/index.js +++ b/index.js @@ -20,7 +20,7 @@ module.exports = (template, data) => { result = result ? result[property] : ''; } - return htmlEscape(result.toString()) || ''; + return htmlEscape(String(result)); }); } @@ -33,6 +33,6 @@ module.exports = (template, data) => { result = result ? result[property] : ''; } - return result || ''; + return String(result); }); }; diff --git a/test.js b/test.js index 3babccd..a8a1a13 100644 --- a/test.js +++ b/test.js @@ -5,6 +5,7 @@ test('main', t => { // Normal placeholder t.is(pupa('{foo}', {foo: '!'}), '!'); t.is(pupa('{foo}', {foo: 10}), '10'); + t.is(pupa('{foo}', {foo: 0}), '0'); t.is(pupa('{foo}{foo}', {foo: '!'}), '!!'); t.is(pupa('{foo}{bar}{foo}', {foo: '!', bar: '#'}), '!#!'); t.is(pupa('yo {foo} lol {bar} sup', {foo: '🦄', bar: '🌈'}), 'yo 🦄 lol 🌈 sup'); @@ -23,6 +24,7 @@ test('main', t => { // Encoding HTML Entities to avoid code injection t.is(pupa('{{foo}}', {foo: '!'}), '!'); t.is(pupa('{{foo}}', {foo: 10}), '10'); + t.is(pupa('{{foo}}', {foo: 0}), '0'); t.is(pupa('{{foo}}{{foo}}', {foo: '!'}), '!!'); t.is(pupa('{foo}{{bar}}{foo}', {foo: '!', bar: '#'}), '!#!'); t.is(pupa('yo {{foo}} lol {{bar}} sup', {foo: '🦄', bar: '🌈'}), 'yo 🦄 lol 🌈 sup');