refactor(common): pipe code cleanup
This commit is contained in:
parent
70488ed382
commit
5509453e72
|
@ -8,7 +8,6 @@
|
|||
|
||||
import {ChangeDetectorRef, OnDestroy, Pipe, WrappedValue} from '@angular/core';
|
||||
import {EventEmitter, Observable} from '../facade/async';
|
||||
import {isBlank, isPresent} from '../facade/lang';
|
||||
import {isPromise} from '../private_import_core';
|
||||
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
||||
|
||||
|
@ -38,9 +37,8 @@ class PromiseStrategy implements SubscriptionStrategy {
|
|||
onDestroy(subscription: any): void {}
|
||||
}
|
||||
|
||||
var _promiseStrategy = new PromiseStrategy();
|
||||
var _observableStrategy = new ObservableStrategy();
|
||||
var __unused: Promise<any>; // avoid unused import when Promise union types are erased
|
||||
const _promiseStrategy = new PromiseStrategy();
|
||||
const _observableStrategy = new ObservableStrategy();
|
||||
|
||||
/**
|
||||
* @ngModule CommonModule
|
||||
|
@ -69,30 +67,24 @@ var __unused: Promise<any>; // avoid unused import when Promise union types are
|
|||
*/
|
||||
@Pipe({name: 'async', pure: false})
|
||||
export class AsyncPipe implements OnDestroy {
|
||||
/** @internal */
|
||||
_latestValue: Object = null;
|
||||
/** @internal */
|
||||
_latestReturnedValue: Object = null;
|
||||
private _latestValue: Object = null;
|
||||
private _latestReturnedValue: Object = null;
|
||||
|
||||
/** @internal */
|
||||
_subscription: Object = null;
|
||||
/** @internal */
|
||||
_obj: Observable<any>|Promise<any>|EventEmitter<any> = null;
|
||||
/** @internal */
|
||||
_ref: ChangeDetectorRef;
|
||||
private _subscription: Object = null;
|
||||
private _obj: Observable<any>|Promise<any>|EventEmitter<any> = null;
|
||||
private _strategy: SubscriptionStrategy = null;
|
||||
|
||||
constructor(_ref: ChangeDetectorRef) { this._ref = _ref; }
|
||||
constructor(private _ref: ChangeDetectorRef) {}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
if (isPresent(this._subscription)) {
|
||||
if (this._subscription) {
|
||||
this._dispose();
|
||||
}
|
||||
}
|
||||
|
||||
transform(obj: Observable<any>|Promise<any>|EventEmitter<any>): any {
|
||||
if (isBlank(this._obj)) {
|
||||
if (isPresent(obj)) {
|
||||
if (!this._obj) {
|
||||
if (obj) {
|
||||
this._subscribe(obj);
|
||||
}
|
||||
this._latestReturnedValue = this._latestValue;
|
||||
|
@ -106,33 +98,32 @@ export class AsyncPipe implements OnDestroy {
|
|||
|
||||
if (this._latestValue === this._latestReturnedValue) {
|
||||
return this._latestReturnedValue;
|
||||
} else {
|
||||
this._latestReturnedValue = this._latestValue;
|
||||
return WrappedValue.wrap(this._latestValue);
|
||||
}
|
||||
|
||||
this._latestReturnedValue = this._latestValue;
|
||||
return WrappedValue.wrap(this._latestValue);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_subscribe(obj: Observable<any>|Promise<any>|EventEmitter<any>): void {
|
||||
private _subscribe(obj: Observable<any>|Promise<any>|EventEmitter<any>): void {
|
||||
this._obj = obj;
|
||||
this._strategy = this._selectStrategy(obj);
|
||||
this._subscription = this._strategy.createSubscription(
|
||||
obj, (value: Object) => this._updateLatestValue(obj, value));
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_selectStrategy(obj: Observable<any>|Promise<any>|EventEmitter<any>): any {
|
||||
private _selectStrategy(obj: Observable<any>|Promise<any>|EventEmitter<any>): any {
|
||||
if (isPromise(obj)) {
|
||||
return _promiseStrategy;
|
||||
} else if ((<any>obj).subscribe) {
|
||||
return _observableStrategy;
|
||||
} else {
|
||||
throw new InvalidPipeArgumentError(AsyncPipe, obj);
|
||||
}
|
||||
|
||||
if ((<any>obj).subscribe) {
|
||||
return _observableStrategy;
|
||||
}
|
||||
|
||||
throw new InvalidPipeArgumentError(AsyncPipe, obj);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_dispose(): void {
|
||||
private _dispose(): void {
|
||||
this._strategy.dispose(this._subscription);
|
||||
this._latestValue = null;
|
||||
this._latestReturnedValue = null;
|
||||
|
@ -140,8 +131,7 @@ export class AsyncPipe implements OnDestroy {
|
|||
this._obj = null;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_updateLatestValue(async: any, value: Object) {
|
||||
private _updateLatestValue(async: any, value: Object) {
|
||||
if (async === this._obj) {
|
||||
this._latestValue = value;
|
||||
this._ref.markForCheck();
|
||||
|
|
|
@ -7,10 +7,8 @@
|
|||
*/
|
||||
|
||||
import {Inject, LOCALE_ID, Pipe, PipeTransform} from '@angular/core';
|
||||
|
||||
import {StringMapWrapper} from '../facade/collection';
|
||||
import {DateFormatter} from '../facade/intl';
|
||||
import {DateWrapper, NumberWrapper, isBlank, isDate, isString} from '../facade/lang';
|
||||
import {NumberWrapper, isBlank, isDate} from '../facade/lang';
|
||||
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
||||
|
||||
|
||||
|
@ -83,7 +81,7 @@ import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
|||
@Pipe({name: 'date', pure: true})
|
||||
export class DatePipe implements PipeTransform {
|
||||
/** @internal */
|
||||
static _ALIASES: {[key: string]: String} = {
|
||||
static _ALIASES: {[key: string]: string} = {
|
||||
'medium': 'yMMMdjms',
|
||||
'short': 'yMdjm',
|
||||
'fullDate': 'yMMMMEEEEd',
|
||||
|
@ -104,23 +102,15 @@ export class DatePipe implements PipeTransform {
|
|||
}
|
||||
|
||||
if (NumberWrapper.isNumeric(value)) {
|
||||
value = DateWrapper.fromMillis(parseFloat(value));
|
||||
} else if (isString(value)) {
|
||||
value = DateWrapper.fromISOString(value);
|
||||
value = parseFloat(value);
|
||||
}
|
||||
if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) {
|
||||
pattern = <string>StringMapWrapper.get(DatePipe._ALIASES, pattern);
|
||||
}
|
||||
return DateFormatter.format(value, this._locale, pattern);
|
||||
|
||||
return DateFormatter.format(
|
||||
new Date(value), this._locale, DatePipe._ALIASES[pattern] || pattern);
|
||||
}
|
||||
|
||||
private supports(obj: any): boolean {
|
||||
if (isDate(obj) || NumberWrapper.isNumeric(obj)) {
|
||||
return true;
|
||||
}
|
||||
if (isString(obj) && isDate(DateWrapper.fromISOString(obj))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return isDate(obj) || NumberWrapper.isNumeric(obj) ||
|
||||
(typeof obj === 'string' && isDate(new Date(obj)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
import {Pipe, PipeTransform} from '@angular/core';
|
||||
import {StringWrapper, isBlank, isStringMap} from '../facade/lang';
|
||||
import {isBlank, isStringMap} from '../facade/lang';
|
||||
import {NgLocalization, getPluralCategory} from '../localization';
|
||||
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
||||
|
||||
|
@ -43,6 +43,6 @@ export class I18nPluralPipe implements PipeTransform {
|
|||
|
||||
const key = getPluralCategory(value, Object.keys(pluralMap), this._localization);
|
||||
|
||||
return StringWrapper.replaceAll(pluralMap[key], _INTERPOLATION_REGEXP, value.toString());
|
||||
return pluralMap[key].replace(_INTERPOLATION_REGEXP, value.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
import {Pipe, PipeTransform} from '@angular/core';
|
||||
import {isBlank, isString} from '../facade/lang';
|
||||
import {isBlank} from '../facade/lang';
|
||||
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
||||
|
||||
|
||||
|
@ -29,7 +29,7 @@ import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
|||
export class LowerCasePipe implements PipeTransform {
|
||||
transform(value: string): string {
|
||||
if (isBlank(value)) return value;
|
||||
if (!isString(value)) {
|
||||
if (typeof value !== 'string') {
|
||||
throw new InvalidPipeArgumentError(LowerCasePipe, value);
|
||||
}
|
||||
return value.toLowerCase();
|
||||
|
|
|
@ -9,21 +9,23 @@
|
|||
import {Inject, LOCALE_ID, Pipe, PipeTransform, Type} from '@angular/core';
|
||||
|
||||
import {NumberFormatStyle, NumberFormatter} from '../facade/intl';
|
||||
import {NumberWrapper, isBlank, isNumber, isPresent, isString} from '../facade/lang';
|
||||
import {NumberWrapper, isBlank, isPresent} from '../facade/lang';
|
||||
|
||||
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
||||
|
||||
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(\-(\d+))?)?$/;
|
||||
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/;
|
||||
|
||||
function formatNumber(
|
||||
pipe: Type<any>, locale: string, 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)) {
|
||||
value = typeof value === 'string' && NumberWrapper.isNumeric(value) ? +value : value;
|
||||
if (typeof value !== 'number') {
|
||||
throw new InvalidPipeArgumentError(pipe, value);
|
||||
}
|
||||
|
||||
let minInt: number;
|
||||
let minFraction: number;
|
||||
let maxFraction: number;
|
||||
|
@ -34,8 +36,8 @@ function formatNumber(
|
|||
maxFraction = 3;
|
||||
}
|
||||
|
||||
if (isPresent(digits)) {
|
||||
var parts = digits.match(_NUMBER_FORMAT_REGEXP);
|
||||
if (digits) {
|
||||
let parts = digits.match(_NUMBER_FORMAT_REGEXP);
|
||||
if (parts === null) {
|
||||
throw new Error(`${digits} is not a valid digit info for number pipes`);
|
||||
}
|
||||
|
@ -49,12 +51,13 @@ function formatNumber(
|
|||
maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]);
|
||||
}
|
||||
}
|
||||
|
||||
return NumberFormatter.format(value as number, locale, style, {
|
||||
minimumIntegerDigits: minInt,
|
||||
minimumFractionDigits: minFraction,
|
||||
maximumFractionDigits: maxFraction,
|
||||
currency: currency,
|
||||
currencyAsSymbol: currencyAsSymbol
|
||||
currencyAsSymbol: currencyAsSymbol,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -7,8 +7,7 @@
|
|||
*/
|
||||
|
||||
import {Pipe, PipeTransform} from '@angular/core';
|
||||
import {ListWrapper} from '../facade/collection';
|
||||
import {StringWrapper, isArray, isBlank, isString} from '../facade/lang';
|
||||
import {isBlank} from '../facade/lang';
|
||||
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
||||
|
||||
/**
|
||||
|
@ -58,16 +57,15 @@ import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
|||
|
||||
@Pipe({name: 'slice', pure: false})
|
||||
export class SlicePipe implements PipeTransform {
|
||||
transform(value: any, start: number, end: number = null): any {
|
||||
transform(value: any, start: number, end?: number): any {
|
||||
if (isBlank(value)) return value;
|
||||
|
||||
if (!this.supports(value)) {
|
||||
throw new InvalidPipeArgumentError(SlicePipe, value);
|
||||
}
|
||||
if (isString(value)) {
|
||||
return StringWrapper.slice(value, start, end);
|
||||
}
|
||||
return ListWrapper.slice(value, start, end);
|
||||
|
||||
return value.slice(start, end);
|
||||
}
|
||||
|
||||
private supports(obj: any): boolean { return isString(obj) || isArray(obj); }
|
||||
private supports(obj: any): boolean { return typeof obj === 'string' || Array.isArray(obj); }
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
import {Pipe, PipeTransform} from '@angular/core';
|
||||
import {isBlank, isString} from '../facade/lang';
|
||||
import {isBlank} from '../facade/lang';
|
||||
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
||||
|
||||
/**
|
||||
|
@ -28,7 +28,7 @@ import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
|||
export class UpperCasePipe implements PipeTransform {
|
||||
transform(value: string): string {
|
||||
if (isBlank(value)) return value;
|
||||
if (!isString(value)) {
|
||||
if (typeof value !== 'string') {
|
||||
throw new InvalidPipeArgumentError(UpperCasePipe, value);
|
||||
}
|
||||
return value.toUpperCase();
|
||||
|
|
Loading…
Reference in New Issue