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

fixes #10189
This commit is contained in:
Victor Berchet 2016-07-20 18:48:17 -07:00
parent cdb3678fe3
commit d455942389
3 changed files with 26 additions and 22 deletions

View File

@ -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]);

View File

@ -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',

View File

@ -15,7 +15,7 @@ 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,
@ -23,17 +23,18 @@ export class NumberFormatter {
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 =