Skip to content

Commit

Permalink
fix(currency): Handling default currency of NONE
Browse files Browse the repository at this point in the history
  • Loading branch information
jgodi authored Jan 30, 2018
2 parents 02173f6 + 52d9d89 commit 9f4c4f7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/currency-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export const currencyOverridesMap = {
SEK: 'kr',
NOK: 'kr',
ZAR: 'R'
};
};
6 changes: 6 additions & 0 deletions src/lib/formats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ test('currency should use the override currency if set', t => {
t.is(t.context.formats.formatCurrency(123456.789), 'RUB123,456.79');
});

test('currency should be able to format with currency format of None', t => {
t.context.formats.setLocale('en-US');
t.context.formats.overrideCurrency = 'None';
t.is(t.context.formats.formatCurrency(123456.789), '123,456.79');
});

// formatDate(value, format)

test('formatDate should default to en-US for unknown locale', t => {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/formats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mergeDeep } from './object-assign-deep';
import { currencyOverridesMap } from './currency-overrides'
import { currencyOverridesMap } from './currency-overrides';

// Interface for defaults
export interface IFormatDefaults {
Expand Down Expand Up @@ -55,6 +55,9 @@ export class Formats {
let _format: Intl.NumberFormatOptions = (typeof format === 'string') ? mergeDeep({}, { currency: format }, this.defaults.currency) : mergeDeep({}, format, this.defaults.currency);
let options = mergeDeep({ style: 'currency', currency: 'USD' }, _format);
if (this.overrideCurrency) {
if (this.overrideCurrency === 'None') {
return new Intl.NumberFormat([this.locale, 'en-US'], {minimumFractionDigits: 2, maximumFractionDigits: 2}).format(value);
}
options.currency = this.overrideCurrency;
}

Expand Down

0 comments on commit 9f4c4f7

Please sign in to comment.