diff --git a/CHANGELOG.md b/CHANGELOG.md index fd82814e..f369affc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,14 @@ * [[#31](https://github.com/VadimDez/ng2-filter-pipe/issues/31)] - How to filter by two variables of the same array. * [[#4](https://github.com/VadimDez/ng2-filter-pipe/issues/4)] - Add $or operator. +## 0.1.9 +* [[#32](https://github.com/VadimDez/ng2-filter-pipe/issues/32)] - Filter fails when filter value is zero + ## 0.1.8 * Removed warning in Angular 4+ ## 0.1.7 -* [[#28](https://github.com/VadimDez/ng2-filter-pipe/pull/28)] - Filter number by string +* [[#28](https://github.com/VadimDez/ng2-filter-pipe/issues/28)] - Filter number by string * Removed post install script ## 0.1.6 diff --git a/README.md b/README.md index aaaceb16..4f83ec44 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,14 @@ Angular 2+ pipeline for filtering arrays. -##### [Demo Page](https://vadimdez.github.io/ng2-filter-pipe/) -
+### Demo Page + +[https://vadimdez.github.io/ng2-filter-pipe/](https://vadimdez.github.io/ng2-filter-pipe/) + ## Install ``` diff --git a/examples/ng-cli/src/app/shared/ng2-filter.pipe.spec.ts b/examples/ng-cli/src/app/shared/ng2-filter.pipe.spec.ts index 4981907d..a7faa952 100644 --- a/examples/ng-cli/src/app/shared/ng2-filter.pipe.spec.ts +++ b/examples/ng-cli/src/app/shared/ng2-filter.pipe.spec.ts @@ -148,6 +148,31 @@ describe('Pipe: Ng2FilterPipe', () => { expect(pipe.transform(objects, filter)).toEqual([objects[1]]); }); + it('should filter by two variables', () => { + const objects = [ + { name: 'Mario', code: 'Mario' }, + { name: 'Mario', code: 'Guy' }, + { name: 'Guy', code: 'Mario' }, + ]; + const filter = { name: 'Mario', code: 'Mario' }; + + expect(pipe.transform(objects, filter)).toEqual([objects[0]]); + + expect(pipe.transform(objects, { name: 'Guy', code: 'Guy' })).toEqual([]); + }); + + it('should filter by 0', () => { + const objects = [ + { age: 0 }, + { age: 1 }, + { age: 2 }, + ]; + const filter = { age: 0 }; + + expect(pipe.transform(objects, filter)).toEqual([objects[0]]); + expect(pipe.transform([1, 2, 0], 0)).toEqual([0]); + }); + it('should filter array by string', () => { const objects = [ { languages: ['English'] }, diff --git a/examples/ng-cli/src/app/shared/ng2-filter.pipe.ts b/examples/ng-cli/src/app/shared/ng2-filter.pipe.ts index f7dd07bb..3ffeb009 100644 --- a/examples/ng-cli/src/app/shared/ng2-filter.pipe.ts +++ b/examples/ng-cli/src/app/shared/ng2-filter.pipe.ts @@ -106,7 +106,7 @@ export class Ng2FilterPipe { */ private filterDefault(filter) { return value => { - return !filter || filter == value; + return filter === undefined || filter == value; } } diff --git a/src/ng2-filter.pipe.spec.ts b/src/ng2-filter.pipe.spec.ts index d53cfb0a..45c62e12 100644 --- a/src/ng2-filter.pipe.spec.ts +++ b/src/ng2-filter.pipe.spec.ts @@ -148,6 +148,31 @@ describe('Pipe: Ng2FilterPipe', () => { expect(pipe.transform(objects, filter)).toEqual([objects[1]]); }); + it('should filter by two variables', () => { + const objects = [ + { name: 'Mario', code: 'Mario' }, + { name: 'Mario', code: 'Guy' }, + { name: 'Guy', code: 'Mario' }, + ]; + const filter = { name: 'Mario', code: 'Mario' }; + + expect(pipe.transform(objects, filter)).toEqual([objects[0]]); + + expect(pipe.transform(objects, { name: 'Guy', code: 'Guy' })).toEqual([]); + }); + + it('should filter by 0', () => { + const objects = [ + { age: 0 }, + { age: 1 }, + { age: 2 }, + ]; + const filter = { age: 0 }; + + expect(pipe.transform(objects, filter)).toEqual([objects[0]]); + expect(pipe.transform([1, 2, 0], 0)).toEqual([0]); + }); + it('should filter array by string', () => { const objects = [ { languages: ['English'] }, diff --git a/src/ng2-filter.pipe.ts b/src/ng2-filter.pipe.ts index 1e0a579b..dc35ff51 100644 --- a/src/ng2-filter.pipe.ts +++ b/src/ng2-filter.pipe.ts @@ -98,7 +98,7 @@ export class Ng2FilterPipe { */ private filterDefault(filter) { return value => { - return !filter || filter == value; + return filter === undefined || filter == value; } }