Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 5b27178

Browse files
joeeameswardbell
authored andcommitted
docs(pipes) clarify impure pipes; explain missing filter/orderBy pipes
adds flying-heroes example and its test adds random-pipe example
1 parent 16b3c94 commit 5b27178

21 files changed

+615
-128
lines changed

public/docs/_examples/pipes/e2e-spec.js

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ describe('Pipes', function () {
55
});
66

77
it('should open correctly', function () {
8-
expect(element.all(by.css('h4')).get(0).getText()).toEqual('Hero Birthday v.1');
9-
expect(element(by.css('body > hero-birthday p')).getText()).toEqual("The hero's birthday is Apr 15, 1988");
8+
expect(element.all(by.tagName('h1')).get(0).getText()).toEqual('Pipes');
9+
expect(element(by.css('hero-birthday p')).getText()).toEqual("The hero's birthday is Apr 15, 1988");
1010
});
1111

12-
it('should show delayed message', function () {
13-
expect(element.all(by.css('hero-message')).get(0).getText()).toEqual('Message: You are my Hero!');
12+
it('should show an async hero message', function () {
13+
expect(element.all(by.tagName('hero-message')).get(0).getText()).toContain('hero');
1414
});
1515

1616
it('should show 4 heroes', function () {
1717
expect(element.all(by.css('hero-list div')).count()).toEqual(4);
1818
});
1919

20-
it('should show 4 heroes in json', function () {
20+
it('should show a familiar hero in json', function () {
2121
expect(element(by.cssContainingText('hero-list p', 'Heroes as JSON')).getText()).toContain('Bombasto');
2222
});
2323

@@ -27,9 +27,9 @@ describe('Pipes', function () {
2727
});
2828

2929
it('should be able to toggle birthday formats', function () {
30-
var birthDayEle = element(by.css('my-app > hero-birthday > p'));
30+
var birthDayEle = element(by.css('hero-birthday2 > p'));
3131
expect(birthDayEle.getText()).toEqual("The hero's birthday is 4/15/1988");
32-
var buttonEle = element(by.cssContainingText('my-app > hero-birthday > button', "Toggle Format"));
32+
var buttonEle = element(by.cssContainingText('hero-birthday2 > button', "Toggle Format"));
3333
expect(buttonEle.isDisplayed()).toBe(true);
3434
buttonEle.click().then(function() {
3535
expect(birthDayEle.getText()).toEqual("The hero's birthday is Friday, April 15, 1988");
@@ -66,5 +66,52 @@ describe('Pipes', function () {
6666
});
6767

6868

69+
it('should support flying heroes (pure) ', function () {
70+
var nameEle = element(by.css('flying-heroes input[type="text"]'));
71+
var canFlyCheckEle = element(by.css('flying-heroes #can-fly'));
72+
var mutateCheckEle = element(by.css('flying-heroes #mutate'));
73+
var resetEle = element(by.css('flying-heroes button'));
74+
var flyingHeroesEle = element.all(by.css('flying-heroes #flyers div'));
75+
76+
expect(canFlyCheckEle.getAttribute('checked')).toEqual('true', 'should default to "can fly"');
77+
expect(mutateCheckEle.getAttribute('checked')).toEqual('true', 'should default to mutating array');
78+
expect(flyingHeroesEle.count()).toEqual(2, 'only two of the original heroes can fly');
79+
80+
return sendKeys(nameEle, "test1\n")
81+
.then(function(){
82+
expect(flyingHeroesEle.count()).toEqual(2, 'no change while mutating array');
83+
return mutateCheckEle.click();
84+
})
85+
.then(function() {
86+
return sendKeys(nameEle, "test2\n");
87+
})
88+
.then(function() {
89+
expect(flyingHeroesEle.count()).toEqual(4, 'not mutating; should see both adds');
90+
expect(flyingHeroesEle.get(2).getText()).toContain('test1');
91+
expect(flyingHeroesEle.get(3).getText()).toContain('test2');
92+
return resetEle.click();
93+
})
94+
.then(function() {
95+
expect(flyingHeroesEle.count()).toEqual(2, 'reset should restore orginal flying heroes');
96+
})
97+
});
98+
99+
100+
it('should support flying heroes (impure) ', function () {
101+
var nameEle = element(by.css('flying-heroes-impure input[type="text"]'));
102+
var canFlyCheckEle = element(by.css('flying-heroes-impure #can-fly'));
103+
var mutateCheckEle = element(by.css('flying-heroes-impure #mutate'));
104+
var resetEle = element(by.css('flying-heroes-impure button'));
105+
var flyingHeroesEle = element.all(by.css('flying-heroes-impure #flyers div'));
106+
107+
expect(canFlyCheckEle.getAttribute('checked')).toEqual('true', 'should default to "can fly"');
108+
expect(mutateCheckEle.getAttribute('checked')).toEqual('true', 'should default to mutating array');
109+
expect(flyingHeroesEle.count()).toEqual(2, 'only two of the original heroes can fly');
110+
111+
return sendKeys(nameEle, "test1\n")
112+
.then(function(){
113+
expect(flyingHeroesEle.count()).toEqual(3, 'new flying hero should show in mutating array');
114+
})
115+
});
69116

70117
});
Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
<hr>
2-
<!-- async examples at the top so can see them in action -->
3-
<hero-message></hero-message>
1+
<a id="toc"></a>
2+
<h1>Pipes</h1>
3+
<a href="#happy-birthday1">Happy Birthday v.1</a><br>
4+
<a href="#birthday-date-pipe">Birthday DatePipe</a><br>
5+
<a href="#happy-birthday2">Happy Birthday v.2</a><br>
6+
<a href="#birthday-pipe-chaining">Birthday Pipe Chaining</a><br>
7+
<a href="#power-booster">Power Booster custom pipe</a><br>
8+
<a href="#power-boost-calc">Power Boost Calculator custom pipe with params</a><br>
9+
<a href="#flying-heroes">Flying Heroes filter pipe (pure)</a><br>
10+
<a href="#flying-heroes-impure">Flying Heroes filter pipe (impure)</a><br>
11+
<a href="#hero-message">Async Hero Message and AsyncPipe</a><br>
12+
<a href="#hero-list">Hero List with caching FetchJsonPipe</a><br>
13+
<a href="#random-pipe">Random Pipe (pure pipe / impure function)</a><br>
14+
415

516
<hr>
6-
<hero-list></hero-list>
17+
<a id="happy-birthday1"></a>
18+
<h2>Hero Birthday v.1</h2>
19+
<hero-birthday></hero-birthday>
720

821
<hr>
22+
<a id="birthday-date-pipe"></a>
23+
<h2>Birthday DatePipe</h2>
924
<!-- #docregion hero-birthday-template -->
1025
<p>The hero's birthday is {{ birthday | date }}</p>
1126
<!-- #enddocregion hero-birthday-template-->
@@ -15,33 +30,59 @@
1530
<!-- #enddocregion format-birthday-->
1631

1732
<hr>
18-
<h4>Hero Birthday v.2</h4>
19-
<hero-birthday>loading...</hero-birthday>
20-
<hr>
21-
33+
<a id="happy-birthday2"></a>
34+
<h2>Hero Birthday v.2</h2>
35+
<hero-birthday2></hero-birthday2>
2236

23-
<!-- #docregion chained-birthday -->
37+
<hr>
38+
<a id="birthday-pipe-chaining"></a>
39+
<h2>Birthday Pipe Chaining</h2>
2440
<p>
41+
<!-- #docregion chained-birthday -->
2542
The chained hero's birthday is
2643
{{ birthday | date | uppercase}}
27-
</p>
2844
<!-- #enddocregion chained-birthday -->
45+
</p>
2946

30-
<!-- #docregion chained-parameter-birthday -->
3147
<p>
48+
<!-- #docregion chained-parameter-birthday -->
3249
The chained hero's birthday is
3350
{{ birthday | date:'fullDate' | uppercase}}
34-
</p>
3551
<!-- #enddocregion chained-parameter-birthday -->
36-
<!-- #docregion chained-parameter-birthday-parens -->
52+
</p>
3753
<p>
54+
<!-- #docregion chained-parameter-birthday-parens -->
3855
The chained hero's birthday is
3956
{{ ( birthday | date:'fullDate' ) | uppercase}}
40-
</p>
4157
<!-- #enddocregion chained-parameter-birthday-parens -->
58+
</p>
59+
<hr>
60+
<a id="power-booster"></a>
61+
<power-booster></power-booster>
62+
63+
<hr>
64+
<a id="power-boost-calc"></a>
65+
<power-boost-calculator>loading</power-boost-calculator>
66+
67+
<hr>
68+
<a id="flying-heroes"></a>
69+
<flying-heroes></flying-heroes>
70+
71+
<hr>
72+
<a id="flying-heroes-impure"></a>
73+
<flying-heroes-impure></flying-heroes-impure>
74+
75+
<hr>
76+
<a id="hero-message"></a>
77+
<!-- async examples at the top so can see them in action -->
78+
<hero-message></hero-message>
79+
4280
<hr>
43-
<power-booster>loading...</power-booster>
81+
<a id="hero-list"></a>
82+
<hero-list></hero-list>
4483

4584
<hr>
46-
<power-boost-calculator>loading ..</power-boost-calculator>
85+
<a id="random-pipe"></a>
86+
<random-pipe></random-pipe>
4787

88+
<div style="margin-top:12em;"></div>

public/docs/_examples/pipes/ts/app/app.component.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
// #docregion
22
import {Component} from 'angular2/core';
3+
import {HTTP_PROVIDERS} from 'angular2/http';
4+
5+
import {FlyingHeroesComponent,
6+
FlyingHeroesImpureComponent} from './flying-heroes.component';
37
import {HeroAsyncMessageComponent} from './hero-async-message.component';
4-
import {HeroBirthday} from './hero-birthday2.component';
8+
import {HeroBirthday} from './hero-birthday1.component';
9+
import {HeroBirthday2} from './hero-birthday2.component';
510
import {HeroListComponent} from './hero-list.component';
611
import {PowerBooster} from './power-booster.component';
712
import {PowerBoostCalculator} from './power-boost-calculator.component';
13+
import {RandomPipeComponent} from './random-pipe.component';
814

915
@Component({
1016
selector: 'my-app',
1117
templateUrl: 'app/app.component.html',
1218
directives:[
19+
FlyingHeroesComponent, FlyingHeroesImpureComponent,
1320
HeroAsyncMessageComponent,
1421
HeroBirthday,
22+
HeroBirthday2,
1523
HeroListComponent,
16-
PowerBooster, PowerBoostCalculator
17-
]
24+
PowerBooster, PowerBoostCalculator,
25+
RandomPipeComponent
26+
],
27+
providers:[HTTP_PROVIDERS]
1828
})
1929
export class AppComponent {
2030
birthday = new Date(1988,3,15); // April 15, 1988

public/docs/_examples/pipes/ts/app/exponential-strength.pipe.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {Pipe, PipeTransform} from 'angular2/core';
1111
*/
1212
@Pipe({name: 'exponentialStrength'})
1313
export class ExponentialStrengthPipe implements PipeTransform {
14-
15-
transform(value:number, args:string[]) : any {
16-
return Math.pow(value, parseInt(args[0] || '1', 10));
14+
transform(value:number, [exponent]) : number {
15+
var exp = parseFloat(exponent);
16+
return Math.pow(value, isNaN(exp) ? 1 : exp);
1717
}
1818
}
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/// <reference path='./window.extension.d.ts'/>
21
// #docregion
32
import {Pipe, PipeTransform} from 'angular2/core';
3+
import {Http} from 'angular2/http';
44

55
// #docregion pipe-metadata
66
@Pipe({
@@ -9,16 +9,20 @@ import {Pipe, PipeTransform} from 'angular2/core';
99
})
1010
// #enddocregion pipe-metadata
1111
export class FetchJsonPipe implements PipeTransform{
12-
private fetchedValue:any;
13-
private fetchPromise:Promise<any>;
12+
private fetched:any = null;
13+
private prevUrl = '';
1414

15-
transform(value:string, args:string[]):any {
16-
if (!this.fetchPromise) {
17-
this.fetchPromise = window.fetch(value)
18-
.then((result:any) => result.json())
19-
.then((json:any) => this.fetchedValue = json);
15+
constructor(private _http: Http) { }
16+
17+
transform(url:string):any {
18+
if (url !== this.prevUrl) {
19+
this.prevUrl = url;
20+
this.fetched = null;
21+
this._http.get(url)
22+
.map( result => result.json() )
23+
.subscribe( result => this.fetched = result )
2024
}
2125

22-
return this.fetchedValue;
26+
return this.fetched;
2327
}
24-
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!-- #docplaster-->
2+
<!-- #docregion -->
3+
<h2>{{title}}</h2>
4+
<p>
5+
<!-- #docregion template-1 -->
6+
New hero:
7+
<input type="text" #box
8+
(keyup.enter)="addHero(box.value); box.value=''"
9+
placeholder="hero name">
10+
<!-- #enddocregion template-1 -->
11+
<input id="can-fly" type="checkbox" [(ngModel)]="canFly"> can fly
12+
</p>
13+
<p>
14+
<input id="mutate" type="checkbox" [(ngModel)]="mutate">Mutate array
15+
<!-- #docregion template-1 -->
16+
<button (click)="reset()">Reset</button>
17+
<!-- #enddocregion template-1 -->
18+
</p>
19+
20+
<h4>Heroes who fly (piped)</h4>
21+
<div id="flyers">
22+
<!-- #docregion template-flying-heroes -->
23+
<div *ngFor="#hero of (heroes | flyingHeroes)">
24+
{{hero.name}}
25+
</div>
26+
<!-- #enddocregion template-flying-heroes -->
27+
</div>
28+
29+
<h4>All Heroes (no pipe)</h4>
30+
<div id="all">
31+
<!-- #docregion template-1 -->
32+
<!-- #docregion template-all-heroes -->
33+
<div *ngFor="#hero of heroes">
34+
{{hero.name}}
35+
</div>
36+
<!-- #enddocregion template-all-heroes -->
37+
<!-- #enddocregion template-1 -->
38+
39+
</div>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// #docplaster
2+
// #docregion
3+
import {Component} from 'angular2/core';
4+
import {FlyingHeroesPipe,
5+
FlyingHeroesImpurePipe} from './flying-heroes.pipe';
6+
import {HEROES} from './heroes';
7+
8+
@Component({
9+
selector: 'flying-heroes',
10+
templateUrl: 'app/flying-heroes.component.html',
11+
styles: ['#flyers, #all {font-style: italic}'],
12+
pipes: [FlyingHeroesPipe]
13+
})
14+
// #docregion v1
15+
export class FlyingHeroesComponent {
16+
heroes:any[] = [];
17+
canFly = true;
18+
// #enddocregion v1
19+
mutate = true;
20+
title = 'Flying Heroes (pure pipe)';
21+
22+
// #docregion v1
23+
constructor() { this.reset(); }
24+
25+
addHero(name:string) {
26+
name = name.trim();
27+
if (!name) { return; }
28+
let hero = {name, canFly: this.canFly};
29+
// #enddocregion v1
30+
if (this.mutate) {
31+
// Pure pipe won't update display because heroes array reference is unchanged
32+
// Impure pipe will display
33+
// #docregion v1
34+
// #docregion push
35+
this.heroes.push(hero)
36+
// #enddocregion push
37+
// #enddocregion v1
38+
} else {
39+
// Pipe updates display because heroes array is a new object
40+
// #docregion concat
41+
this.heroes = this.heroes.concat(hero);
42+
// #enddocregion concat
43+
}
44+
// #docregion v1
45+
}
46+
47+
reset() { this.heroes = HEROES.slice(); }
48+
}
49+
// #enddocregion v1
50+
51+
////// Identical except for impure pipe //////
52+
// #docregion impure-component
53+
@Component({
54+
selector: 'flying-heroes-impure',
55+
templateUrl: 'app/flying-heroes.component.html',
56+
// #enddocregion impure-component
57+
styles: ['.flyers, .all {font-style: italic}'],
58+
// #docregion impure-component
59+
pipes: [FlyingHeroesImpurePipe]
60+
})
61+
export class FlyingHeroesImpureComponent extends FlyingHeroesComponent {
62+
title = 'Flying Heroes (impure pipe)';
63+
}
64+
// #docregion impure-component

0 commit comments

Comments
 (0)