Skip to content

Commit 31e8f86

Browse files
committed
Merge pull request #9 from opiepj/feature/8-custom-pipe
Feature/8 custom pipe
2 parents d41189d + 17b0acc commit 31e8f86

File tree

4 files changed

+222
-5
lines changed

4 files changed

+222
-5
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/// <reference path="../../../typings/_custom.d.ts" />
2+
import {CapitalizePipe, CapitalizeFactory} from './Capitalize-Pipe';
3+
4+
describe('Capitalize', () => {
5+
6+
describe('CapitalizePipe', () => {
7+
var subject;
8+
var result;
9+
var pipe;
10+
11+
beforeEach(() => {
12+
pipe = new CapitalizePipe();
13+
});
14+
afterEach(() => {
15+
expect(subject).toEqual(result);
16+
});
17+
18+
describe('#support', () => {
19+
20+
it('should support string', () => {
21+
subject = pipe.supports('yolo');
22+
result = true;
23+
});
24+
25+
it('should not support null', () => {
26+
subject = pipe.supports(null);
27+
result = false;
28+
});
29+
30+
it('should not support NaN', () => {
31+
subject = pipe.supports(NaN);
32+
result = false;
33+
});
34+
35+
it('should not support new Object()', () => {
36+
subject = pipe.supports(new Object());
37+
result = false;
38+
});
39+
40+
it('should not support function(){}', () => {
41+
subject = pipe.supports(function(){});
42+
result = false;
43+
});
44+
45+
});
46+
47+
describe('#transform', () => {
48+
it('should transform string to Capitalized versions', () => {
49+
subject = pipe.transform('yolo');
50+
result = 'Yolo';
51+
});
52+
53+
it('should transform all strings to Capitalized versions', () => {
54+
var str = 'what does the scouter say about its power level';
55+
56+
subject = pipe.transform(str, true);
57+
result = 'What Does The Scouter Say About Its Power Level';
58+
});
59+
60+
});
61+
62+
63+
describe('#capitalizeWord', () => {
64+
it('should capitalized a word', () => {
65+
subject = pipe.capitalizeWord('something');
66+
result = 'Something';
67+
});
68+
69+
it('should only capitalized first char', () => {
70+
subject = pipe.capitalizeWord('something something something');
71+
result = 'Something something something';
72+
});
73+
74+
});
75+
76+
77+
});
78+
79+
describe('CapitalizeFactory', () => {
80+
var subject;
81+
var result;
82+
var factory;
83+
84+
beforeEach(() => {
85+
factory = new CapitalizeFactory();
86+
});
87+
88+
afterEach(() => {
89+
expect(subject).toEqual(result);
90+
});
91+
92+
it('should exist', () => {
93+
subject = Boolean(factory);
94+
result = true;
95+
});
96+
97+
describe('#support', () => {
98+
it('should support string', () => {
99+
subject = factory.supports('yolo');
100+
result = true;
101+
});
102+
103+
it('should not support null', () => {
104+
subject = factory.supports(null);
105+
result = false;
106+
});
107+
108+
it('should not support NaN', () => {
109+
subject = factory.supports(NaN);
110+
result = false;
111+
});
112+
113+
it('should not support new Object()', () => {
114+
subject = factory.supports(new Object());
115+
result = false;
116+
});
117+
118+
it('should not support function(){}', () => {
119+
subject = factory.supports(function(){});
120+
result = false;
121+
});
122+
123+
124+
});
125+
126+
describe('#create', () => {
127+
it('should be instance of CapitalizePipe', () => {
128+
subject = factory.create() instanceof CapitalizePipe;
129+
result = true;
130+
});
131+
132+
});
133+
134+
135+
});
136+
137+
138+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference path="../../../typings/_custom.d.ts" />
2+
import {Pipe, PipeFactory} from 'angular2/angular2';
3+
4+
// Check if the value is supported for the pipe
5+
export function isString(txt): boolean {
6+
return typeof txt === 'string';
7+
}
8+
9+
// Simple example of a Pipe
10+
export class CapitalizePipe implements Pipe {
11+
regexp: RegExp = /([^\W_]+[^\s-]*) */g;
12+
13+
supports(txt): boolean {
14+
return isString(txt);
15+
}
16+
transform(value: string, args?: List<any>): any {
17+
return (!value) ? '' :
18+
(!args) ?
19+
this.capitalizeWord(value) :
20+
value.replace(this.regexp, this.capitalizeWord);
21+
}
22+
capitalizeWord(txt: string): string {
23+
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
24+
}
25+
onDestroy(): void {
26+
// not needed since this is stateless
27+
}
28+
29+
}
30+
31+
// We create a factory since we create an instance for each binding for stateful pipes
32+
export class CapitalizeFactory implements PipeFactory {
33+
supports(txt): boolean {
34+
return isString(txt);
35+
}
36+
create(cdRef): Pipe {
37+
return new CapitalizePipe();
38+
}
39+
}
40+
41+
// Since templates in angular are async we are passing the value to
42+
// NullPipeFactory if the value is not supported
43+
export var capitalize = [ new CapitalizeFactory() ];

src/app/pipes/pipes.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import {Pipes} from 'angular2/change_detection';
77
/*
88
* App Pipes
99
*/
10-
/* TODO: Create these pipe classes
11-
import {capitalize} from './CapitalizePipe';
10+
import {capitalize} from './Capitalize/Capitalize-Pipe';
1211
import {rxAsync} from './RxPipe';
13-
*/
1412

1513
export var appPipes = [
1614
Pipes.extend({
17-
//'async': rxAsync,
18-
//'capitalize': capitalize
15+
'async': rxAsync,
16+
'capitalize': capitalize
1917
// add more pipes to this Map
2018
})
2119
];

src/app/pipes/rx/rxpipe.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/// <reference path="../../../typings/_custom.d.ts" />
2+
3+
import {Pipe, PipeFactory} from 'angular2/angular2';
4+
import {ObservablePipe} from 'angular2/pipes';
5+
import {async} from 'angular2/src/change_detection/change_detection';
6+
import * as Rx from 'rx';
7+
8+
export function isObservable(obs) {
9+
return obs && typeof obs.subscribe === 'function';
10+
}
11+
12+
//upgrade async pipe with Rx support
13+
export class RxPipe extends ObservablePipe {
14+
_subscription: any;
15+
_observable: any;
16+
constructor(ref) { super(ref); }
17+
supports(obs) { return isObservable(obs); }
18+
_subscribe(obs) {
19+
this._observable = obs;
20+
this._subscription = obs.subscribe(
21+
value => this._updateLatestValue(value),
22+
e => { throw e; }
23+
);
24+
}
25+
transform(value: any, args?: List<any>): any {
26+
return super.transform(value, args);
27+
}
28+
onDestroy(): void {
29+
return super.onDestroy();
30+
}
31+
}
32+
33+
export class RxPipeFactory implements PipeFactory {
34+
supports(obs) { return isObservable(obs); }
35+
create(cdRef): Pipe { return new RxPipe(cdRef); }
36+
}
37+
38+
export var rxAsync = [ new RxPipeFactory() ].concat(async);

0 commit comments

Comments
 (0)