From f3dd91e1d70642ed1e80f350c54244f3426d2cf2 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 19 Jul 2016 11:27:06 -0700 Subject: [PATCH] feat(NumberPipe): add string support (#10163) fixes #10159 --- modules/@angular/common/src/pipes/number_pipe.ts | 16 +++++++++++----- .../common/test/pipes/number_pipe_spec.ts | 15 +++++++++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/modules/@angular/common/src/pipes/number_pipe.ts b/modules/@angular/common/src/pipes/number_pipe.ts index 20fe1c3714..f77cdcc7b8 100644 --- a/modules/@angular/common/src/pipes/number_pipe.ts +++ b/modules/@angular/common/src/pipes/number_pipe.ts @@ -7,9 +7,11 @@ */ import {Pipe, PipeTransform} from '@angular/core'; + import {BaseException} from '../facade/exceptions'; import {NumberFormatStyle, NumberFormatter} from '../facade/intl'; -import {NumberWrapper, RegExpWrapper, Type, isBlank, isNumber, isPresent} from '../facade/lang'; +import {NumberWrapper, RegExpWrapper, StringWrapper, Type, isBlank, isNumber, isPresent, isString} from '../facade/lang'; + import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception'; var defaultLocale: string = 'en-US'; @@ -19,13 +21,17 @@ 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, style: NumberFormatStyle, digits: string, currency: string = null, - currencyAsSymbol: boolean = false): string { + pipe: Type, value: number | string, style: NumberFormatStyle, digits: string, + currency: string = null, currencyAsSymbol: boolean = false): string { if (isBlank(value)) return null; + // Convert strings to numbers + value = isString(value) && NumberWrapper.isNumeric(value) ? +value : value; if (!isNumber(value)) { throw new InvalidPipeArgumentException(pipe, value); } - var minInt = 1, minFraction = 0, maxFraction = 3; + let minInt = 1; + let minFraction = 0; + let maxFraction = 3; if (isPresent(digits)) { var parts = RegExpWrapper.firstMatch(_NUMBER_FORMAT_REGEXP, digits); if (isBlank(parts)) { @@ -41,7 +47,7 @@ function formatNumber( maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]); } } - return NumberFormatter.format(value, defaultLocale, style, { + return NumberFormatter.format(value as number, defaultLocale, style, { minimumIntegerDigits: minInt, minimumFractionDigits: minFraction, maximumFractionDigits: maxFraction, diff --git a/modules/@angular/common/test/pipes/number_pipe_spec.ts b/modules/@angular/common/test/pipes/number_pipe_spec.ts index f2cb9d74ca..efadaa9d6e 100644 --- a/modules/@angular/common/test/pipes/number_pipe_spec.ts +++ b/modules/@angular/common/test/pipes/number_pipe_spec.ts @@ -32,8 +32,19 @@ export function main() { expect(pipe.transform(1.1234)).toEqual('1.123'); }); - it('should not support other objects', - () => { expect(() => pipe.transform(new Object())).toThrowError(); }); + it('should support strings', () => { + expect(pipe.transform('12345')).toEqual('12,345'); + expect(pipe.transform('123', '.2')).toEqual('123.00'); + expect(pipe.transform('1', '3.')).toEqual('001'); + expect(pipe.transform('1.1', '3.4-5')).toEqual('001.1000'); + expect(pipe.transform('1.123456', '3.4-5')).toEqual('001.12346'); + expect(pipe.transform('1.1234')).toEqual('1.123'); + }); + + it('should not support other objects', () => { + expect(() => pipe.transform(new Object())).toThrowError(); + expect(() => pipe.transform('123abc')).toThrowError(); + }); }); });