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.
This commit is contained in:
parent
e93b3d2360
commit
a5a422f8e7
|
@ -9,7 +9,7 @@ export {DatePipe} from './pipes/date_pipe';
|
||||||
export {JsonPipe} from './pipes/json_pipe';
|
export {JsonPipe} from './pipes/json_pipe';
|
||||||
export {SlicePipe} from './pipes/slice_pipe';
|
export {SlicePipe} from './pipes/slice_pipe';
|
||||||
export {LowerCasePipe} from './pipes/lowercase_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 {UpperCasePipe} from './pipes/uppercase_pipe';
|
||||||
export {ReplacePipe} from './pipes/replace_pipe';
|
export {ReplacePipe} from './pipes/replace_pipe';
|
||||||
export {I18nPluralPipe} from './pipes/i18n_plural_pipe';
|
export {I18nPluralPipe} from './pipes/i18n_plural_pipe';
|
||||||
|
|
|
@ -6,57 +6,53 @@ import {
|
||||||
isBlank,
|
isBlank,
|
||||||
NumberWrapper,
|
NumberWrapper,
|
||||||
RegExpWrapper,
|
RegExpWrapper,
|
||||||
|
Type
|
||||||
} from '../../src/facade/lang';
|
} from '../../src/facade/lang';
|
||||||
import {BaseException} from '../../src/facade/exceptions';
|
import {BaseException} from '../../src/facade/exceptions';
|
||||||
import {NumberFormatter, NumberFormatStyle} from '../../src/facade/intl';
|
import {NumberFormatter, NumberFormatStyle} from '../../src/facade/intl';
|
||||||
import {ListWrapper} from '../../src/facade/collection';
|
|
||||||
|
|
||||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||||
|
|
||||||
var defaultLocale: string = 'en-US';
|
var defaultLocale: string = 'en-US';
|
||||||
var _re = RegExpWrapper.create('^(\\d+)?\\.((\\d+)(\\-(\\d+))?)?$');
|
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()
|
function formatNumber(pipe: Type, value: number, style: NumberFormatStyle, digits: string, currency: string = null,
|
||||||
export class NumberPipe {
|
|
||||||
/** @internal */
|
|
||||||
static _format(value: number, style: NumberFormatStyle, digits: string, currency: string = null,
|
|
||||||
currencyAsSymbol: boolean = false): string {
|
currencyAsSymbol: boolean = false): string {
|
||||||
if (isBlank(value)) return null;
|
if (isBlank(value)) return null;
|
||||||
if (!isNumber(value)) {
|
if (!isNumber(value)) {
|
||||||
throw new InvalidPipeArgumentException(NumberPipe, 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
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
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.
|
* 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
|
* Formats a number as local text. i.e. group sizing and separator and other locale-specific
|
||||||
* configurations are based on the active locale.
|
* configurations are based on the active locale.
|
||||||
|
@ -79,18 +75,20 @@ export class NumberPipe {
|
||||||
* ### Example
|
* ### Example
|
||||||
*
|
*
|
||||||
* {@example core/pipes/ts/number_pipe/number_pipe_example.ts region='NumberPipe'}
|
* {@example core/pipes/ts/number_pipe/number_pipe_example.ts region='NumberPipe'}
|
||||||
|
*
|
||||||
|
* @experimental
|
||||||
*/
|
*/
|
||||||
@Pipe({name: 'number'})
|
@Pipe({name: 'number'})
|
||||||
@Injectable()
|
export class DecimalPipe implements PipeTransform {
|
||||||
export class DecimalPipe extends NumberPipe implements PipeTransform {
|
|
||||||
transform(value: any, digits: string = null): string {
|
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.
|
* 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.
|
* Formats a number as local percent.
|
||||||
*
|
*
|
||||||
|
@ -103,18 +101,21 @@ export class DecimalPipe extends NumberPipe implements PipeTransform {
|
||||||
* ### Example
|
* ### Example
|
||||||
*
|
*
|
||||||
* {@example core/pipes/ts/number_pipe/number_pipe_example.ts region='PercentPipe'}
|
* {@example core/pipes/ts/number_pipe/number_pipe_example.ts region='PercentPipe'}
|
||||||
|
*
|
||||||
|
* @experimental
|
||||||
*/
|
*/
|
||||||
@Pipe({name: 'percent'})
|
@Pipe({name: 'percent'})
|
||||||
@Injectable()
|
export class PercentPipe implements PipeTransform {
|
||||||
export class PercentPipe extends NumberPipe implements PipeTransform {
|
|
||||||
transform(value: any, digits: string = null): string {
|
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.
|
* 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.
|
* Formats a number as local currency.
|
||||||
*
|
*
|
||||||
|
@ -131,13 +132,14 @@ export class PercentPipe extends NumberPipe implements PipeTransform {
|
||||||
* ### Example
|
* ### Example
|
||||||
*
|
*
|
||||||
* {@example core/pipes/ts/number_pipe/number_pipe_example.ts region='CurrencyPipe'}
|
* {@example core/pipes/ts/number_pipe/number_pipe_example.ts region='CurrencyPipe'}
|
||||||
|
*
|
||||||
|
* @experimental
|
||||||
*/
|
*/
|
||||||
@Pipe({name: 'currency'})
|
@Pipe({name: 'currency'})
|
||||||
@Injectable()
|
export class CurrencyPipe implements PipeTransform {
|
||||||
export class CurrencyPipe extends NumberPipe implements PipeTransform {
|
|
||||||
transform(value: any, currencyCode: string = 'USD', symbolDisplay: boolean = false,
|
transform(value: any, currencyCode: string = 'USD', symbolDisplay: boolean = false,
|
||||||
digits: string = null): string {
|
digits: string = null): string {
|
||||||
return NumberPipe._format(value, NumberFormatStyle.Currency, digits, currencyCode,
|
return formatNumber(CurrencyPipe, value, NumberFormatStyle.Currency, digits, currencyCode,
|
||||||
symbolDisplay);
|
symbolDisplay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,6 @@ var COMMON: string[] = [
|
||||||
'NgSwitch',
|
'NgSwitch',
|
||||||
'NgSwitchWhen',
|
'NgSwitchWhen',
|
||||||
'NgSwitchDefault',
|
'NgSwitchDefault',
|
||||||
'NumberPipe',
|
|
||||||
'ObservableListDiff:dart',
|
'ObservableListDiff:dart',
|
||||||
'ObservableListDiffFactory:dart',
|
'ObservableListDiffFactory:dart',
|
||||||
'PatternValidator',
|
'PatternValidator',
|
||||||
|
|
Loading…
Reference in New Issue