Skip to content

Commit

Permalink
#4 #31 finished first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimDez committed May 30, 2017
1 parent f893c7b commit 574020f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
39 changes: 16 additions & 23 deletions examples/ng-cli/src/app/shared/ng2-filter.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,6 @@ describe('Pipe: Ng2FilterPipe', () => {
expect(pipe.transform(objects, filter)).toEqual([objects[0], objects[1], objects[3]]);
});

// it('should filter array by array', () => {
// const objects = [
// { languages: ['English'] },
// { languages: ['English', 'German'] },
// { languages: ['German'] },
// { languages: ['German', 'English'] }
// ];
//
// let filter = { languages: ['English'] };
//
// expect(pipe.transform(objects, filter)).toEqual([objects[0], objects[1], objects[3]]);
//
// filter = { languages: ['English', 'German'] };
// expect(pipe.transform(objects, filter)).toEqual(objects);
//
// filter = { languages: ['German'] };
// expect(pipe.transform(objects, filter)).toEqual([ objects[1], objects[2], objects[3]]);
// });

it('should filter array by using $or operator', () => {
const objects = [
{ languages: ['English'] },
Expand All @@ -202,8 +183,20 @@ describe('Pipe: Ng2FilterPipe', () => {
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]]);
// });
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 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([]);
});
});
17 changes: 12 additions & 5 deletions examples/ng-cli/src/app/shared/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 @@ -43,7 +50,7 @@ export class Ng2FilterPipe {
} else if (filterType === 'string') {
isMatching = this.filterByString(filter[key])(val);
} else if (filterType === 'object') {
isMatching = filter[key].hasOwnProperty('$or') ? this.filterByOr(filter[key])(val) : this.filterByObject(filter[key])(val);
isMatching = this.filterByObject(filter[key])(val);
} else {
isMatching = this.filterDefault(filter[key])(val);
}
Expand All @@ -57,17 +64,17 @@ export class Ng2FilterPipe {
}
}

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

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

Expand Down

0 comments on commit 574020f

Please sign in to comment.