Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(number): Fixed the number zero being rendered as NaN #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"TODAY": "Today is {today:date}.",
"TOMORROW": "Today is {tomorrow:date}.",
"MONEY": "You owe: {debt:currency}",
"NUMBER": "There are {people:number} things",
"NUMBER": "There are {people:number} people",
"MESSAGES": {
"other": {
"zero": "No messages.",
Expand Down
1 change: 1 addition & 0 deletions demo/src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ chomsky.onLocaleChange.subscribe((locale: string) => {
console.log('\t' + chomsky.translate('TODAY', { today: new Date() }));
console.log('\t' + chomsky.translate('TOMORROW', { tomorrow: '7/4/1776' }));
console.log('\t' + chomsky.translate('MONEY', { debt: 123456.1 }));
console.log('\t' + chomsky.translate('NUMBER', { people: 0 }));
console.log('\t' + chomsky.translate('NUMBER', { people: 1234567 }));
console.log('\t' + chomsky.translate('MESSAGES', { quantity: 0 }));
console.log('\t' + chomsky.translate('MESSAGES', { quantity: 1 }));
Expand Down
9 changes: 9 additions & 0 deletions src/lib/chomsky.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ test('translate should resolve dynamic number variables with custom formatting i
t.is(t.context.chomsky.translate('placements', mockDynamicValues), 'Vous avez besoin 123,456.23.');
});

test('translate should resolve numbers allowing zero as a number', t => {
let mockDynamicValues = { placed: 0 };
t.context.chomsky.formats.override(FORMAT_DEFAULTS);
t.context.chomsky.currentLocale = 'en';
t.is(t.context.chomsky.translate('placements', mockDynamicValues), 'You need 0.');
t.context.chomsky.currentLocale = 'fr';
t.is(t.context.chomsky.translate('placements', mockDynamicValues), 'Vous avez besoin 0.');
});

test('translate should resolve plural values', t => {
t.context.chomsky.currentLocale = 'en';
t.is(t.context.chomsky.translate('messages', { quantity: 0 }), 'No messages.');
Expand Down
2 changes: 1 addition & 1 deletion src/lib/chomsky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class Chomsky {
}
return match;
}
let unparsedValue = replacements[params[0]] || interpolation;
let unparsedValue = replacements.hasOwnProperty(params[0]) ? replacements[params[0]] : interpolation;
switch (params[1]) {
case 'date':
return this.formatDate(unparsedValue, params[2]);
Expand Down