Skip to content

Commit

Permalink
#31 #4 further development
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimDez committed May 29, 2017
1 parent cd8ef56 commit f893c7b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 17 additions & 1 deletion examples/ng-cli/src/app/shared/ng2-filter.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('Pipe: Ng2FilterPipe', () => {
// expect(pipe.transform(objects, filter)).toEqual([ objects[1], objects[2], objects[3]]);
// });

it('should filter by using $or operator', () => {
it('should filter array by using $or operator', () => {
const objects = [
{ languages: ['English'] },
{ languages: ['English', 'German'] },
Expand All @@ -190,4 +190,20 @@ describe('Pipe: Ng2FilterPipe', () => {

expect(pipe.transform(objects, { languages: { $or: ['English', 'German'] }})).toEqual(objects);
});

it('should filter string by using $or operator', () => {
const objects = [
{ languages: 'English' },
{ languages: 'German' }
];

expect(pipe.transform(objects, { languages: { $or: ['English', 'German'] }})).toEqual(objects);
expect(pipe.transform(objects, { languages: { $or: ['English'] }})).toEqual([objects[0]]);
expect(pipe.transform(objects, { languages: { $or: ['asd'] }})).toEqual([]);
});

// it('should filter array of string by using $or operator', () => {
// const objects = [ 'English', 'German' ];
// expect(pipe.transform(objects, { $or: ['English'] })).toEqual([objects[0]]);
// });
});
13 changes: 11 additions & 2 deletions examples/ng-cli/src/app/shared/ng2-filter.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ export class Ng2FilterPipe {
}

private filterByOr(filter: { $or: any[] }) {
return (value: any[]) => {
return (value: any) => {
let hasMatch = false;
const length = filter.$or.length;
const isArray = value instanceof Array;

const arrayComparison = (i) => {
return value.indexOf(filter.$or[i]) !== -1;
};
const otherComparison = (i) => {
return value === filter.$or[i];
};
const comparison = isArray ? arrayComparison : otherComparison;

for (let i = 0; i < length; i++) {
if (value.indexOf(filter.$or[i]) !== -1) {
if (comparison(i)) {
hasMatch = true;
break;
}
Expand Down

0 comments on commit f893c7b

Please sign in to comment.