Skip to content

Commit 545fc1b

Browse files
committed
(#8) Capitalize Pipes
1 parent d41189d commit 545fc1b

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
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() ];

0 commit comments

Comments
 (0)