Skip to content

Commit ee806eb

Browse files
thchiaryandrewjohnson
authored andcommitted
Handle falsy values in data. (#117)
1 parent 322adca commit ee806eb

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export const templater = (
8888
const regex = new RegExp(pattern, 'gmi');
8989
if (regex.test(templatePortion)) matched = data[prop];
9090
}
91-
return matched || templatePortion;
91+
if (typeof matched === 'undefined') return templatePortion;
92+
return matched;
9293
});
9394

9495
// if there is a React element, return as array

tests/utils.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ describe('locale utils', () => {
9090
const after = ['Hello this is a ', <Comp /> , ' translation'];
9191
const result = utils.templater(before, data);
9292
expect(result).toEqual(after);
93-
})
93+
});
94+
95+
it('should handle falsy data values (except undefined)', () => {
96+
const data = { zero: 0, empty: '' };
97+
const before = 'Number ${zero}, empty ${empty}';
98+
const after = 'Number 0, empty ';
99+
const result = utils.templater(before, data);
100+
expect(result).toEqual(after);
101+
});
94102
});
95103

96104
describe('getIndexForLanguageCode', () => {

0 commit comments

Comments
 (0)