diff --git a/src/utils.js b/src/utils.js index 1745330..b07dcee 100644 --- a/src/utils.js +++ b/src/utils.js @@ -88,7 +88,8 @@ export const templater = ( const regex = new RegExp(pattern, 'gmi'); if (regex.test(templatePortion)) matched = data[prop]; } - return matched || templatePortion; + if (typeof matched === 'undefined') return templatePortion; + return matched; }); // if there is a React element, return as array diff --git a/tests/utils.test.js b/tests/utils.test.js index 0f94511..1fb85a7 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -90,7 +90,15 @@ describe('locale utils', () => { const after = ['Hello this is a ', , ' translation']; const result = utils.templater(before, data); expect(result).toEqual(after); - }) + }); + + it('should handle falsy data values (except undefined)', () => { + const data = { zero: 0, empty: '' }; + const before = 'Number ${zero}, empty ${empty}'; + const after = 'Number 0, empty '; + const result = utils.templater(before, data); + expect(result).toEqual(after); + }); }); describe('getIndexForLanguageCode', () => {