Skip to content

Commit

Permalink
updated src
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimDez committed Jun 1, 2017
1 parent 5e61334 commit 0b024a9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
6 changes: 6 additions & 0 deletions examples/ng-cli/src/app/shared/ng2-filter.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export class Ng2FilterPipe {
}
}

/**
* Filter value by $or
*
* @param filter
* @returns {(value:any)=>boolean}
*/
private filterByOr(filter: any[]) {
return (value: any) => {
let hasMatch = false;
Expand Down
43 changes: 26 additions & 17 deletions src/ng2-filter.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,33 +186,42 @@ describe('Pipe: Ng2FilterPipe', () => {
expect(pipe.transform(objects, filter)).toEqual([objects[0], objects[1], objects[3]]);
});

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

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

expect(pipe.transform(objects, filter)).toEqual([objects[0], objects[1], objects[3]]);
it('should filter string by using $or operator', () => {
const objects = [
{ languages: 'English' },
{ languages: 'German' }
];

filter = { languages: ['English', 'German'] };
expect(pipe.transform(objects, filter)).toEqual(objects);
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([]);
});

filter = { languages: ['German'] };
expect(pipe.transform(objects, filter)).toEqual([ objects[1], objects[2], objects[3]]);
it('should filter array of string by using $or operator', () => {
const objects = [ 'English', 'German' ];
expect(pipe.transform(objects, { $or: ['English'] })).toEqual([objects[0]]);
});

// it('should filter by using $or operator', () => {
// const objects = [
// {
// valueA: 1,
// valueB: 2
// }
// ];
//
// expect(pipe.transform(objects, { $or: [{ valueA: 1 }, { valueB: 2 }] })).toEqual(objects);
// });
it('should filter by using $or operator and another field', () => {
const objects = [
{ languages: ['English', 'German'], age: 30 },
{ languages: 'German', age: 27 }
];

expect(pipe.transform(objects, { languages: { $or: ['English', 'German'] }, age: 27 })).toEqual([objects[1]]);
expect(pipe.transform(objects, { languages: { $or: ['English', 'German'] }, age: 30 })).toEqual([objects[0]]);
expect(pipe.transform(objects, { languages: { $or: ['English', 'German'] }, age: 31 })).toEqual([]);
expect(pipe.transform(objects, { languages: { $or: ['English'] }, age: 27 })).toEqual([]);
});
});
32 changes: 23 additions & 9 deletions src/ng2-filter.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export class Ng2FilterPipe {
return value => {
for (let key in filter) {

if (key === '$or') {
if (!this.filterByOr(filter.$or)(this.getValue(value))) {
return false;
}
continue;
}

if (!value.hasOwnProperty(key) && !Object.getOwnPropertyDescriptor(Object.getPrototypeOf(value), key)) {
return false;
}
Expand All @@ -42,8 +49,6 @@ export class Ng2FilterPipe {
isMatching = this.filterByBoolean(filter[key])(val);
} else if (filterType === 'string') {
isMatching = this.filterByString(filter[key])(val);
} else if (filter[key] instanceof Array && val instanceof Array) {
isMatching = this.filterByArray(filter[key])(val);
} else if (filterType === 'object') {
isMatching = this.filterByObject(filter[key])(val);
} else {
Expand All @@ -60,18 +65,27 @@ export class Ng2FilterPipe {
}

/**
* Filter array by array
* Match at least one value
* Filter value by $or
*
* @param filter
* @returns {(value:any[])=>boolean}
* @returns {(value:any)=>boolean}
*/
private filterByArray(filter: any[]) {
return (value: any[]) => {
private filterByOr(filter: any[]) {
return (value: any) => {
let hasMatch = false;
const length = value.length;
const length = filter.length;
const isArray = value instanceof Array;

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

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

0 comments on commit 0b024a9

Please sign in to comment.