forked from danrevah/ngx-pipes
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
113 additions
and
28 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 |
---|---|---|
|
@@ -19,6 +19,7 @@ coverage/ | |
.c9/ | ||
*.launch | ||
.settings/ | ||
ng-pipes.iml | ||
|
||
# misc | ||
/.sass-cache | ||
|
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
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
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,62 @@ | ||
import {FilterByPipe} from './filter-by'; | ||
|
||
describe('FilterByPipe', () => { | ||
let pipe: FilterByPipe; | ||
|
||
const users = [ | ||
{id: 1, first_name: 'John', last_name: 'Doe', work: { title: 'Software Engineer', company: 'Foo Tech', previous_company: 'Unknown' }}, | ||
{id: 2, first_name: 'Jane', last_name: 'West', work: { title: 'Designer', company: 'AAA Solutions', previous_company: 'Unknown' }}, | ||
{id: 3, first_name: 'Bruce', last_name: 'John', work: { title: 'Software Engineer', company: 'Bar Tech', previous_company: 'Unknown' }}, | ||
{id: 4, first_name: 'William', last_name: 'Cent', work: { title: 'Designer', company: 'Foo Tech', previous_company: 'Bar Tech' }} | ||
]; | ||
|
||
beforeEach(() => { | ||
pipe = new FilterByPipe(); | ||
}); | ||
|
||
it('should not do anything in-case of not an array', () => { | ||
expect(pipe.transform('foo', [''], '')).toEqual('foo'); | ||
expect(pipe.transform(null, [''], '')).toEqual(null); | ||
expect(pipe.transform(undefined, [''], '')).toEqual(undefined); | ||
expect(pipe.transform(42, [''], '')).toEqual(42); | ||
expect(pipe.transform({foo: 1, bar: 2}, [''], '')).toEqual({foo: 1, bar: 2}); | ||
}); | ||
|
||
it('should filter by single field with a single result', () => { | ||
const filtered = pipe.transform(users, ['id'], 1); | ||
|
||
expect(filtered.length).toEqual(1); | ||
expect(filtered[0]).toEqual(users[0]); | ||
}); | ||
|
||
it('should filter by multiple fields with a two result', () => { | ||
const filtered = pipe.transform(users, ['first_name', 'last_name'], 'John'); | ||
|
||
expect(filtered.length).toEqual(2); | ||
expect(filtered[0]).toEqual(users[0]); | ||
expect(filtered[1]).toEqual(users[2]); | ||
}); | ||
|
||
it('should filter by nested field with a single result', () => { | ||
const filtered = pipe.transform(users, ['work.company'], 'Bar Tech'); | ||
|
||
expect(filtered.length).toEqual(1); | ||
expect(filtered[0]).toEqual(users[2]); | ||
}); | ||
|
||
it('should filter by nested field with a multiple result', () => { | ||
const filtered = pipe.transform(users, ['work.title'], 'Designer'); | ||
|
||
expect(filtered.length).toEqual(2); | ||
expect(filtered[0]).toEqual(users[1]); | ||
expect(filtered[1]).toEqual(users[3]); | ||
}); | ||
|
||
it('should filter by multiple nested field with a multiple result', () => { | ||
const filtered = pipe.transform(users, ['work.company', 'work.previous_company'], 'Bar Tech'); | ||
|
||
expect(filtered.length).toEqual(2); | ||
expect(filtered[0]).toEqual(users[2]); | ||
expect(filtered[1]).toEqual(users[3]); | ||
}); | ||
}); |
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,18 @@ | ||
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
|
||
@Pipe({name: 'filterBy'}) | ||
export class FilterByPipe implements PipeTransform { | ||
|
||
transform(arr: any, props: Array<string>, search: any): any[] { | ||
if (!Array.isArray(arr)) { | ||
return arr; | ||
} | ||
|
||
return arr.filter((obj) => { | ||
return props.some((prop) => | ||
search === GeneralHelper.extractDeepPropertyByMapKey(obj, prop) | ||
); | ||
}); | ||
} | ||
} |
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