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 {Pipe, PipeTransform} from '@angular/core';
import {BaseException} from '../facade/exceptions';
import {NumberFormatStyle, NumberFormatter} from '../facade/intl'; 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'; import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
var defaultLocale: string = 'en-US'; var defaultLocale: string = 'en-US';
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(\-(\d+))?)?$/g; const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(\-(\d+))?)?$/g;
/**
* Internal function to format numbers used by Decimal, Percent and Date pipes.
*/
function formatNumber( function formatNumber(
pipe: Type, value: number | string, style: NumberFormatStyle, digits: string, pipe: Type, value: number | string, style: NumberFormatStyle, digits: string,
currency: string = null, currencyAsSymbol: boolean = false): string { currency: string = null, currencyAsSymbol: boolean = false): string {
@ -29,13 +25,20 @@ function formatNumber(
if (!isNumber(value)) { if (!isNumber(value)) {
throw new InvalidPipeArgumentException(pipe, value); throw new InvalidPipeArgumentException(pipe, value);
} }
let minInt = 1; let minInt: number;
let minFraction = 0; let minFraction: number;
let maxFraction = 3; let maxFraction: number;
if (style !== NumberFormatStyle.Currency) {
// rely on Intl default for currency
minInt = 1;
minFraction = 0;
maxFraction = 3;
}
if (isPresent(digits)) { if (isPresent(digits)) {
var parts = RegExpWrapper.firstMatch(_NUMBER_FORMAT_REGEXP, digits); var parts = RegExpWrapper.firstMatch(_NUMBER_FORMAT_REGEXP, digits);
if (isBlank(parts)) { if (!parts) {
throw new BaseException(`${digits} is not a valid digit info for number pipes`); throw new Error(`${digits} is not a valid digit info for number pipes`);
} }
if (isPresent(parts[1])) { // min integer digits if (isPresent(parts[1])) { // min integer digits
minInt = NumberWrapper.parseIntAutoRadix(parts[1]); minInt = NumberWrapper.parseIntAutoRadix(parts[1]);

View File

@ -71,9 +71,9 @@ export function main() {
describe('transform', () => { describe('transform', () => {
it('should return correct value for numbers', () => { it('should return correct value for numbers', () => {
expect(pipe.transform(123)).toEqual('USD123'); expect(pipe.transform(123)).toEqual('USD123.00');
expect(pipe.transform(12, 'EUR', false, '.2')).toEqual('EUR12.00'); expect(pipe.transform(12, 'EUR', false, '.1')).toEqual('EUR12.0');
expect(pipe.transform(5.123, 'USD', false, '.0-2')).toEqual('USD5.12'); expect(pipe.transform(5.1234, 'USD', false, '.0-3')).toEqual('USD5.123');
}); });
it('should not support other objects', it('should not support other objects',

View File

@ -15,7 +15,7 @@ export enum NumberFormatStyle {
export class NumberFormatter { export class NumberFormatter {
static format( static format(
num: number, locale: string, style: NumberFormatStyle, num: number, locale: string, style: NumberFormatStyle,
{minimumIntegerDigits = 1, minimumFractionDigits = 0, maximumFractionDigits = 3, currency, {minimumIntegerDigits, minimumFractionDigits, maximumFractionDigits, currency,
currencyAsSymbol = false}: { currencyAsSymbol = false}: {
minimumIntegerDigits?: number, minimumIntegerDigits?: number,
minimumFractionDigits?: number, minimumFractionDigits?: number,
@ -23,17 +23,18 @@ export class NumberFormatter {
currency?: string, currency?: string,
currencyAsSymbol?: boolean currencyAsSymbol?: boolean
} = {}): string { } = {}): string {
var intlOptions: Intl.NumberFormatOptions = { let options: Intl.NumberFormatOptions = {
minimumIntegerDigits: minimumIntegerDigits, minimumIntegerDigits,
minimumFractionDigits: minimumFractionDigits, minimumFractionDigits,
maximumFractionDigits: maximumFractionDigits maximumFractionDigits,
style: NumberFormatStyle[style].toLowerCase()
}; };
intlOptions.style = NumberFormatStyle[style].toLowerCase();
if (style == NumberFormatStyle.Currency) { if (style == NumberFormatStyle.Currency) {
intlOptions.currency = currency; options.currency = currency;
intlOptions.currencyDisplay = currencyAsSymbol ? 'symbol' : 'code'; 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 = var DATE_FORMATS_SPLIT =