Skip to content

Commit

Permalink
Fix handling of falsy values
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
sindresorhus committed Jul 27, 2019
1 parent 427ed2c commit 7cb9ab8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pupa('I like {{0}} and {{1}}', ['<br>🦄</br>', '<i>🐮</i>']);
*/
declare function pupa(
template: string,
data: unknown[] | {[key: string]: unknown}
data: unknown[] | {[key: string]: any}
): string;

export = pupa;
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = (template, data) => {
result = result ? result[property] : '';
}

return htmlEscape(result.toString()) || '';
return htmlEscape(String(result));
});
}

Expand All @@ -33,6 +33,6 @@ module.exports = (template, data) => {
result = result ? result[property] : '';
}

return result || '';
return String(result);
});
};
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit 7cb9ab8

Please sign in to comment.