Skip to content

Commit 152a117

Browse files
committed
fix(compiler): properly implement pure pipes and change pipe syntax
Pure pipes as well as arrays and maps are implemented via proxy functions. This is faster than the previous implementation and also generates less code. BREAKING CHANGE: - pipes now take a variable number of arguments, and not an array that contains all arguments.
1 parent d662630 commit 152a117

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+698
-283
lines changed

modules/angular2/src/common/pipes/async_pipe.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import {isBlank, isPresent, isPromise, CONST} from 'angular2/src/facade/lang';
22
import {ObservableWrapper, Observable, EventEmitter} from 'angular2/src/facade/async';
3-
import {
4-
Pipe,
5-
Injectable,
6-
ChangeDetectorRef,
7-
OnDestroy,
8-
PipeTransform,
9-
WrappedValue
10-
} from 'angular2/core';
3+
import {Pipe, Injectable, ChangeDetectorRef, OnDestroy, WrappedValue} from 'angular2/core';
114

125
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
136

@@ -55,7 +48,7 @@ var __unused: Promise<any>; // avoid unused import when Promise union types are
5548
*/
5649
@Pipe({name: 'async', pure: false})
5750
@Injectable()
58-
export class AsyncPipe implements PipeTransform, OnDestroy {
51+
export class AsyncPipe implements OnDestroy {
5952
/** @internal */
6053
_latestValue: Object = null;
6154
/** @internal */
@@ -76,7 +69,7 @@ export class AsyncPipe implements PipeTransform, OnDestroy {
7669
}
7770
}
7871

79-
transform(obj: Observable<any>| Promise<any>| EventEmitter<any>, args?: any[]): any {
72+
transform(obj: Observable<any>| Promise<any>| EventEmitter<any>): any {
8073
if (isBlank(this._obj)) {
8174
if (isPresent(obj)) {
8275
this._subscribe(obj);

modules/angular2/src/common/pipes/date_pipe.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,13 @@ export class DatePipe implements PipeTransform {
101101
};
102102

103103

104-
transform(value: any, args: any[]): string {
104+
transform(value: any, pattern: string = 'mediumDate'): string {
105105
if (isBlank(value)) return null;
106106

107107
if (!this.supports(value)) {
108108
throw new InvalidPipeArgumentException(DatePipe, value);
109109
}
110110

111-
var pattern: string = isPresent(args) && args.length > 0 ? args[0] : 'mediumDate';
112111
if (isNumber(value)) {
113112
value = DateWrapper.fromMillis(value);
114113
}

modules/angular2/src/common/pipes/i18n_plural_pipe.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ var interpolationExp: RegExp = RegExpWrapper.create('#');
4545
@Pipe({name: 'i18nPlural', pure: true})
4646
@Injectable()
4747
export class I18nPluralPipe implements PipeTransform {
48-
transform(value: number, args: any[] = null): string {
48+
transform(value: number, pluralMap: {[count: string]: string}): string {
4949
var key: string;
5050
var valueStr: string;
51-
var pluralMap: {[count: string]: string} = <{[count: string]: string}>(args[0]);
5251

5352
if (!isStringMap(pluralMap)) {
5453
throw new InvalidPipeArgumentException(I18nPluralPipe, pluralMap);

modules/angular2/src/common/pipes/i18n_select_pipe.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
3636
@Pipe({name: 'i18nSelect', pure: true})
3737
@Injectable()
3838
export class I18nSelectPipe implements PipeTransform {
39-
transform(value: string, args: any[] = null): string {
40-
var mapping: {[key: string]: string} = <{[count: string]: string}>(args[0]);
39+
transform(value: string, mapping: {[key: string]: string}): string {
4140
if (!isStringMap(mapping)) {
4241
throw new InvalidPipeArgumentException(I18nSelectPipe, mapping);
4342
}

modules/angular2/src/common/pipes/json_pipe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ import {Injectable, PipeTransform, WrappedValue, Pipe} from 'angular2/core';
1111
@Pipe({name: 'json', pure: false})
1212
@Injectable()
1313
export class JsonPipe implements PipeTransform {
14-
transform(value: any, args: any[] = null): string { return Json.stringify(value); }
14+
transform(value: any): string { return Json.stringify(value); }
1515
}

modules/angular2/src/common/pipes/lowercase_pipe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
1313
@Pipe({name: 'lowercase'})
1414
@Injectable()
1515
export class LowerCasePipe implements PipeTransform {
16-
transform(value: string, args: any[] = null): string {
16+
transform(value: string): string {
1717
if (isBlank(value)) return value;
1818
if (!isString(value)) {
1919
throw new InvalidPipeArgumentException(LowerCasePipe, value);

modules/angular2/src/common/pipes/number_pipe.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
1212
import {NumberFormatter, NumberFormatStyle} from 'angular2/src/facade/intl';
1313
import {Injectable, PipeTransform, WrappedValue, Pipe} from 'angular2/core';
14-
import {ListWrapper} from 'angular2/src/facade/collection';
1514

1615
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
1716

@@ -87,8 +86,7 @@ export class NumberPipe {
8786
@Pipe({name: 'number'})
8887
@Injectable()
8988
export class DecimalPipe extends NumberPipe implements PipeTransform {
90-
transform(value: any, args: any[]): string {
91-
var digits: string = ListWrapper.first(args);
89+
transform(value: any, digits: string = null): string {
9290
return NumberPipe._format(value, NumberFormatStyle.Decimal, digits);
9391
}
9492
}
@@ -113,8 +111,7 @@ export class DecimalPipe extends NumberPipe implements PipeTransform {
113111
@Pipe({name: 'percent'})
114112
@Injectable()
115113
export class PercentPipe extends NumberPipe implements PipeTransform {
116-
transform(value: any, args: any[]): string {
117-
var digits: string = ListWrapper.first(args);
114+
transform(value: any, digits: string = null): string {
118115
return NumberPipe._format(value, NumberFormatStyle.Percent, digits);
119116
}
120117
}
@@ -143,10 +140,8 @@ export class PercentPipe extends NumberPipe implements PipeTransform {
143140
@Pipe({name: 'currency'})
144141
@Injectable()
145142
export class CurrencyPipe extends NumberPipe implements PipeTransform {
146-
transform(value: any, args: any[]): string {
147-
var currencyCode: string = isPresent(args) && args.length > 0 ? args[0] : 'USD';
148-
var symbolDisplay: boolean = isPresent(args) && args.length > 1 ? args[1] : false;
149-
var digits: string = isPresent(args) && args.length > 2 ? args[2] : null;
143+
transform(value: any, currencyCode: string = 'USD', symbolDisplay: boolean = false,
144+
digits: string = null): string {
150145
return NumberPipe._format(value, NumberFormatStyle.Currency, digits, currencyCode,
151146
symbolDisplay);
152147
}

modules/angular2/src/common/pipes/replace_pipe.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
RegExpWrapper,
77
StringWrapper
88
} from 'angular2/src/facade/lang';
9-
import {BaseException} from 'angular2/src/facade/exceptions';
109
import {Injectable, PipeTransform, Pipe} from 'angular2/core';
1110
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
1211

@@ -39,11 +38,7 @@ import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
3938
@Pipe({name: 'replace'})
4039
@Injectable()
4140
export class ReplacePipe implements PipeTransform {
42-
transform(value: any, args: any[]): any {
43-
if (isBlank(args) || args.length !== 2) {
44-
throw new BaseException('ReplacePipe requires two arguments');
45-
}
46-
41+
transform(value: any, pattern: string | RegExp, replacement: Function | string): any {
4742
if (isBlank(value)) {
4843
return value;
4944
}
@@ -53,9 +48,6 @@ export class ReplacePipe implements PipeTransform {
5348
}
5449

5550
var input = value.toString();
56-
var pattern = args[0];
57-
var replacement = args[1];
58-
5951

6052
if (!this._supportedPattern(pattern)) {
6153
throw new InvalidPipeArgumentException(ReplacePipe, pattern);
@@ -67,16 +59,16 @@ export class ReplacePipe implements PipeTransform {
6759
// var rgx = pattern instanceof RegExp ? pattern : RegExpWrapper.create(pattern);
6860

6961
if (isFunction(replacement)) {
70-
var rgxPattern = isString(pattern) ? RegExpWrapper.create(pattern) : pattern;
62+
var rgxPattern = isString(pattern) ? RegExpWrapper.create(<string>pattern) : <RegExp>pattern;
7163

72-
return StringWrapper.replaceAllMapped(input, rgxPattern, replacement);
64+
return StringWrapper.replaceAllMapped(input, rgxPattern, <Function>replacement);
7365
}
7466
if (pattern instanceof RegExp) {
7567
// use the replaceAll variant
76-
return StringWrapper.replaceAll(input, pattern, replacement);
68+
return StringWrapper.replaceAll(input, pattern, <string>replacement);
7769
}
7870

79-
return StringWrapper.replace(input, pattern, replacement);
71+
return StringWrapper.replace(input, <string>pattern, <string>replacement);
8072
}
8173

8274
private _supportedInput(input: any): boolean { return isString(input) || isNumber(input); }

modules/angular2/src/common/pipes/slice_pipe.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {isBlank, isString, isArray, StringWrapper, CONST} from 'angular2/src/facade/lang';
2-
import {BaseException} from 'angular2/src/facade/exceptions';
32
import {ListWrapper} from 'angular2/src/facade/collection';
43
import {Injectable, PipeTransform, WrappedValue, Pipe} from 'angular2/core';
54
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
@@ -59,16 +58,11 @@ import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
5958
@Pipe({name: 'slice', pure: false})
6059
@Injectable()
6160
export class SlicePipe implements PipeTransform {
62-
transform(value: any, args: any[] = null): any {
63-
if (isBlank(args) || args.length == 0) {
64-
throw new BaseException('Slice pipe requires one argument');
65-
}
61+
transform(value: any, start: number, end: number = null): any {
6662
if (!this.supports(value)) {
6763
throw new InvalidPipeArgumentException(SlicePipe, value);
6864
}
6965
if (isBlank(value)) return value;
70-
var start: number = args[0];
71-
var end: number = args.length > 1 ? args[1] : null;
7266
if (isString(value)) {
7367
return StringWrapper.slice(value, start, end);
7468
}

modules/angular2/src/common/pipes/uppercase_pipe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
1313
@Pipe({name: 'uppercase'})
1414
@Injectable()
1515
export class UpperCasePipe implements PipeTransform {
16-
transform(value: string, args: any[] = null): string {
16+
transform(value: string): string {
1717
if (isBlank(value)) return value;
1818
if (!isString(value)) {
1919
throw new InvalidPipeArgumentException(UpperCasePipe, value);

0 commit comments

Comments
 (0)