Permalink
Browse files

fix(CurrencyPipe): use default Intl formatting options when none prov…

…ided

fixes #10189
  • Loading branch information...
1 parent cdb3678 commit d45594238968dfbe0ae8082b63974e7a25af43a6 @vicb vicb committed Jul 21, 2016
@@ -8,18 +8,14 @@
import {Pipe, PipeTransform} from '@angular/core';
-import {BaseException} from '../facade/exceptions';
import {NumberFormatStyle, NumberFormatter} from '../facade/intl';
-import {NumberWrapper, RegExpWrapper, StringWrapper, Type, isBlank, isNumber, isPresent, isString} from '../facade/lang';
+import {NumberWrapper, RegExpWrapper, Type, isBlank, isNumber, isPresent, isString} from '../facade/lang';
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
var defaultLocale: string = 'en-US';
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(\-(\d+))?)?$/g;
-/**
- * Internal function to format numbers used by Decimal, Percent and Date pipes.
- */
function formatNumber(
pipe: Type, value: number | string, style: NumberFormatStyle, digits: string,
currency: string = null, currencyAsSymbol: boolean = false): string {
@@ -29,13 +25,20 @@ function formatNumber(
if (!isNumber(value)) {
throw new InvalidPipeArgumentException(pipe, value);
}
- let minInt = 1;
- let minFraction = 0;
- let maxFraction = 3;
+ let minInt: number;
+ let minFraction: number;
+ let maxFraction: number;
+ if (style !== NumberFormatStyle.Currency) {
+ // rely on Intl default for currency
+ minInt = 1;
+ minFraction = 0;
+ maxFraction = 3;
+ }
+
if (isPresent(digits)) {
var parts = RegExpWrapper.firstMatch(_NUMBER_FORMAT_REGEXP, digits);
- if (isBlank(parts)) {
- throw new BaseException(`${digits} is not a valid digit info for number pipes`);
+ if (!parts) {
+ throw new Error(`${digits} is not a valid digit info for number pipes`);
}
if (isPresent(parts[1])) { // min integer digits
minInt = NumberWrapper.parseIntAutoRadix(parts[1]);
@@ -71,9 +71,9 @@ export function main() {
describe('transform', () => {
it('should return correct value for numbers', () => {
- expect(pipe.transform(123)).toEqual('USD123');
- expect(pipe.transform(12, 'EUR', false, '.2')).toEqual('EUR12.00');
- expect(pipe.transform(5.123, 'USD', false, '.0-2')).toEqual('USD5.12');
+ expect(pipe.transform(123)).toEqual('USD123.00');
+ expect(pipe.transform(12, 'EUR', false, '.1')).toEqual('EUR12.0');
+ expect(pipe.transform(5.1234, 'USD', false, '.0-3')).toEqual('USD5.123');
});
it('should not support other objects',
@@ -15,25 +15,26 @@ export enum NumberFormatStyle {
export class NumberFormatter {
static format(
num: number, locale: string, style: NumberFormatStyle,
- {minimumIntegerDigits = 1, minimumFractionDigits = 0, maximumFractionDigits = 3, currency,
+ {minimumIntegerDigits, minimumFractionDigits, maximumFractionDigits, currency,
currencyAsSymbol = false}: {
minimumIntegerDigits?: number,
minimumFractionDigits?: number,
maximumFractionDigits?: number,
currency?: string,
currencyAsSymbol?: boolean
} = {}): string {
- var intlOptions: Intl.NumberFormatOptions = {
- minimumIntegerDigits: minimumIntegerDigits,
- minimumFractionDigits: minimumFractionDigits,
- maximumFractionDigits: maximumFractionDigits
+ let options: Intl.NumberFormatOptions = {
+ minimumIntegerDigits,
+ minimumFractionDigits,
+ maximumFractionDigits,
+ style: NumberFormatStyle[style].toLowerCase()
};
- intlOptions.style = NumberFormatStyle[style].toLowerCase();
+
if (style == NumberFormatStyle.Currency) {
- intlOptions.currency = currency;
- intlOptions.currencyDisplay = currencyAsSymbol ? 'symbol' : 'code';
+ options.currency = currency;
+ options.currencyDisplay = currencyAsSymbol ? 'symbol' : 'code';
}
- return new Intl.NumberFormat(locale, intlOptions).format(num);
+ return new Intl.NumberFormat(locale, options).format(num);
}
}
var DATE_FORMATS_SPLIT =

0 comments on commit d455942

Please sign in to comment.