From a5a422f8e7e8b07e7132aec67aed73f35378d8af Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 27 May 2016 12:03:03 -0700 Subject: [PATCH] refactor(NumberPipe): remove NumberPipe and replace it with private helper function NumberPipe was just an implementation detail that we were accidentaly exposing as a public api. --- modules/@angular/common/src/pipes.ts | 2 +- .../@angular/common/src/pipes/number_pipe.ts | 94 ++++++++++--------- .../integration_test/public_api_spec.ts | 1 - 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/modules/@angular/common/src/pipes.ts b/modules/@angular/common/src/pipes.ts index b829810dfb..972a1105ca 100644 --- a/modules/@angular/common/src/pipes.ts +++ b/modules/@angular/common/src/pipes.ts @@ -9,7 +9,7 @@ export {DatePipe} from './pipes/date_pipe'; export {JsonPipe} from './pipes/json_pipe'; export {SlicePipe} from './pipes/slice_pipe'; export {LowerCasePipe} from './pipes/lowercase_pipe'; -export {NumberPipe, DecimalPipe, PercentPipe, CurrencyPipe} from './pipes/number_pipe'; +export {DecimalPipe, PercentPipe, CurrencyPipe} from './pipes/number_pipe'; export {UpperCasePipe} from './pipes/uppercase_pipe'; export {ReplacePipe} from './pipes/replace_pipe'; export {I18nPluralPipe} from './pipes/i18n_plural_pipe'; diff --git a/modules/@angular/common/src/pipes/number_pipe.ts b/modules/@angular/common/src/pipes/number_pipe.ts index a4b0aca547..a8562451d7 100644 --- a/modules/@angular/common/src/pipes/number_pipe.ts +++ b/modules/@angular/common/src/pipes/number_pipe.ts @@ -6,57 +6,53 @@ import { isBlank, NumberWrapper, RegExpWrapper, + Type } from '../../src/facade/lang'; import {BaseException} from '../../src/facade/exceptions'; import {NumberFormatter, NumberFormatStyle} from '../../src/facade/intl'; -import {ListWrapper} from '../../src/facade/collection'; - import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception'; var defaultLocale: string = 'en-US'; var _re = RegExpWrapper.create('^(\\d+)?\\.((\\d+)(\\-(\\d+))?)?$'); /** - * Internal base class for numeric pipes. + * Internal function to format numbers used by Decimal, Percent and Date pipes. */ -@Injectable() -export class NumberPipe { - /** @internal */ - static _format(value: number, style: NumberFormatStyle, digits: string, currency: string = null, +function formatNumber(pipe: Type, value: number, style: NumberFormatStyle, digits: string, currency: string = null, currencyAsSymbol: boolean = false): string { - if (isBlank(value)) return null; - if (!isNumber(value)) { - throw new InvalidPipeArgumentException(NumberPipe, value); - } - var minInt = 1, minFraction = 0, maxFraction = 3; - if (isPresent(digits)) { - var parts = RegExpWrapper.firstMatch(_re, digits); - if (isBlank(parts)) { - throw new BaseException(`${digits} is not a valid digit info for number pipes`); - } - if (isPresent(parts[1])) { // min integer digits - minInt = NumberWrapper.parseIntAutoRadix(parts[1]); - } - if (isPresent(parts[3])) { // min fraction digits - minFraction = NumberWrapper.parseIntAutoRadix(parts[3]); - } - if (isPresent(parts[5])) { // max fraction digits - maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]); - } - } - return NumberFormatter.format(value, defaultLocale, style, { - minimumIntegerDigits: minInt, - minimumFractionDigits: minFraction, - maximumFractionDigits: maxFraction, - currency: currency, - currencyAsSymbol: currencyAsSymbol - }); + if (isBlank(value)) return null; + if (!isNumber(value)) { + throw new InvalidPipeArgumentException(pipe, value); } + var minInt = 1, minFraction = 0, maxFraction = 3; + if (isPresent(digits)) { + var parts = RegExpWrapper.firstMatch(_re, digits); + if (isBlank(parts)) { + throw new BaseException(`${digits} is not a valid digit info for number pipes`); + } + if (isPresent(parts[1])) { // min integer digits + minInt = NumberWrapper.parseIntAutoRadix(parts[1]); + } + if (isPresent(parts[3])) { // min fraction digits + minFraction = NumberWrapper.parseIntAutoRadix(parts[3]); + } + if (isPresent(parts[5])) { // max fraction digits + maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]); + } + } + return NumberFormatter.format(value, defaultLocale, style, { + minimumIntegerDigits: minInt, + minimumFractionDigits: minFraction, + maximumFractionDigits: maxFraction, + currency: currency, + currencyAsSymbol: currencyAsSymbol + }); } /** * WARNING: this pipe uses the Internationalization API. - * Therefore it is only reliable in Chrome and Opera browsers. + * Therefore it is only reliable in Chrome and Opera browsers. For other browsers please use an + * polyfill, for example: [https://github.com/andyearnshaw/Intl.js/]. * * Formats a number as local text. i.e. group sizing and separator and other locale-specific * configurations are based on the active locale. @@ -79,18 +75,20 @@ export class NumberPipe { * ### Example * * {@example core/pipes/ts/number_pipe/number_pipe_example.ts region='NumberPipe'} + * + * @experimental */ @Pipe({name: 'number'}) -@Injectable() -export class DecimalPipe extends NumberPipe implements PipeTransform { +export class DecimalPipe implements PipeTransform { transform(value: any, digits: string = null): string { - return NumberPipe._format(value, NumberFormatStyle.Decimal, digits); + return formatNumber(DecimalPipe, value, NumberFormatStyle.Decimal, digits); } } /** * WARNING: this pipe uses the Internationalization API. - * Therefore it is only reliable in Chrome and Opera browsers. + * Therefore it is only reliable in Chrome and Opera browsers. For other browsers please use an + * polyfill, for example: [https://github.com/andyearnshaw/Intl.js/]. * * Formats a number as local percent. * @@ -103,18 +101,21 @@ export class DecimalPipe extends NumberPipe implements PipeTransform { * ### Example * * {@example core/pipes/ts/number_pipe/number_pipe_example.ts region='PercentPipe'} + * + * @experimental */ @Pipe({name: 'percent'}) -@Injectable() -export class PercentPipe extends NumberPipe implements PipeTransform { +export class PercentPipe implements PipeTransform { transform(value: any, digits: string = null): string { - return NumberPipe._format(value, NumberFormatStyle.Percent, digits); + return formatNumber(PercentPipe, value, NumberFormatStyle.Percent, digits); } } /** * WARNING: this pipe uses the Internationalization API. - * Therefore it is only reliable in Chrome and Opera browsers. + * Therefore it is only reliable in Chrome and Opera browsers. For other browsers please use an + * polyfill, for example: [https://github.com/andyearnshaw/Intl.js/]. + * * * Formats a number as local currency. * @@ -131,13 +132,14 @@ export class PercentPipe extends NumberPipe implements PipeTransform { * ### Example * * {@example core/pipes/ts/number_pipe/number_pipe_example.ts region='CurrencyPipe'} + * + * @experimental */ @Pipe({name: 'currency'}) -@Injectable() -export class CurrencyPipe extends NumberPipe implements PipeTransform { +export class CurrencyPipe implements PipeTransform { transform(value: any, currencyCode: string = 'USD', symbolDisplay: boolean = false, digits: string = null): string { - return NumberPipe._format(value, NumberFormatStyle.Currency, digits, currencyCode, + return formatNumber(CurrencyPipe, value, NumberFormatStyle.Currency, digits, currencyCode, symbolDisplay); } } diff --git a/modules/@angular/integration_test/public_api_spec.ts b/modules/@angular/integration_test/public_api_spec.ts index ad0680cb88..8a96d4dff7 100644 --- a/modules/@angular/integration_test/public_api_spec.ts +++ b/modules/@angular/integration_test/public_api_spec.ts @@ -82,7 +82,6 @@ var COMMON: string[] = [ 'NgSwitch', 'NgSwitchWhen', 'NgSwitchDefault', - 'NumberPipe', 'ObservableListDiff:dart', 'ObservableListDiffFactory:dart', 'PatternValidator',