Skip to content

Commit

Permalink
Handle falsy values in data. (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
thchia authored and ryandrewjohnson committed Aug 16, 2018
1 parent 322adca commit ee806eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@ describe('locale utils', () => {
const after = ['Hello this is a ', <Comp /> , ' 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', () => {
Expand Down

0 comments on commit ee806eb

Please sign in to comment.