-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
363c4ff
commit 117a3e2
Showing
3 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {provide} from 'angular2/core'; | ||
|
||
import {it, inject, beforeEach, beforeEachProviders} from 'angular2/testing'; | ||
|
||
import {NamePipe} from './name.pipe'; | ||
|
||
describe('NamePipe', () => { | ||
beforeEachProviders(() => [ | ||
NamePipe | ||
]); | ||
|
||
let champions = []; | ||
|
||
beforeEach(() => { | ||
champions = [{ name: 'Ravenous Hydra' }, { name: 'Titanic Hydra' }, { name: 'Dagger' }]; | ||
}); | ||
|
||
it('should not filter on \'null\'', inject([NamePipe], (pipe) => { | ||
champions = pipe.transform(champions, [null]); | ||
expect(champions.length).toBe(3); | ||
})); | ||
|
||
it('should not filter on \'true\'', inject([NamePipe], (pipe) => { | ||
champions = pipe.transform(champions, [true]); | ||
expect(champions.length).toBe(3); | ||
})); | ||
|
||
it('should not filter on invalid champions', inject([NamePipe], (pipe) => { | ||
champions = pipe.transform(null, ['Dagger']); | ||
expect(champions).toBe(null); | ||
})); | ||
|
||
it('should not filter on \'\'', inject([NamePipe], (pipe) => { | ||
champions = pipe.transform(champions, ['']); | ||
expect(champions.length).toBe(3); | ||
})); | ||
|
||
it('should filter out name \'e\'', inject([NamePipe], (pipe) => { | ||
champions = pipe.transform(champions, ['e']); | ||
expect(champions.length).toBe(2); | ||
})); | ||
|
||
it('should filter out name \'dagger\'', inject([NamePipe], (pipe) => { | ||
champions = pipe.transform(champions, ['dagger']); | ||
expect(champions.length).toBe(1); | ||
})); | ||
|
||
it('should filter out name \'Hydra\'', inject([NamePipe], (pipe) => { | ||
champions = pipe.transform(champions, ['Hydra']); | ||
expect(champions.length).toBe(2); | ||
})); | ||
|
||
it('should remove semicolon from names', inject([NamePipe], (pipe) => { | ||
expect(pipe.clean('da\'gger')).toBe('dagger'); | ||
})); | ||
|
||
it('should lowercase names', inject([NamePipe], (pipe) => { | ||
expect(pipe.clean('DaGGer')).toBe('dagger'); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {Pipe, PipeTransform} from 'angular2/core'; | ||
|
||
@Pipe({ | ||
name: 'name' | ||
}) | ||
|
||
export class NamePipe implements PipeTransform { | ||
transform(champions: Array<Object>, [name]) { | ||
if (!champions || !name || typeof name !== 'string') { | ||
return champions; | ||
} | ||
|
||
return champions.filter((champion) => { | ||
return this.clean(champion['name']).indexOf(this.clean(name)) >= 0; | ||
}); | ||
} | ||
|
||
private clean(value: string): string { | ||
return value.toLowerCase().replace('\'', ''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters