From 3c13017f3c9ad842885b1683cbfcca3e52123ebc Mon Sep 17 00:00:00 2001 From: Vadym Yatsyuk Date: Mon, 18 Dec 2017 18:33:24 +0100 Subject: [PATCH] prepared 1.0.1 --- CHANGELOG.md | 4 +++ package.json | 4 +-- src/index.ts | 1 + src/ngx-filter.pipe.spec.ts | 50 ++++++++++++++++++++++++++++++++++++- src/ngx-filter.pipe.ts | 16 +++++++++--- 5 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 src/index.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index fa399d27..ae2752d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.0.1 +* [[#46](https://github.com/VadimDez/ngx-filter-pipe/issues/46)] - Filter by property/method on prototype chain. +* [[#47](https://github.com/VadimDez/ngx-filter-pipe/pull/47)] - walk the prototype chain to check if field/property exists. + ## 1.0.0 * [[#36](https://github.com/VadimDez/ngx-filter-pipe/issues/36)] - Changed name tp `ngx-filter-pipe`. * [[#38](https://github.com/VadimDez/ngx-filter-pipe/issues/38)] - Added UMD bundle. diff --git a/package.json b/package.json index 4a38066a..7345e7ae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngx-filter-pipe", - "version": "1.0.0", + "version": "1.0.1", "description": "Angular 2+ pipeline for filtering arrays", "author": "Vadym Yatsyuk ", "license": "MIT", @@ -12,7 +12,7 @@ "e2e": "protractor", "minify": "uglifyjs ./dist/app/shared/ngx-filter.pipe.js --compress --mangle --output ./ngx-filter.pipe.min.js --source-map ./ngx-filter.pipe.min.js.map", "build": "rimraf dist && tsc -p tsconfig-esm.json && rollup -c && rollup -c --environment MINIFY && ngc -p tsconfig.json", - "copy": "cp -R ./examples/ng-cli/src/app/order-pipe/ ./src/" + "copy": "cp -R ./examples/ng-cli/src/app/shared/ngx-filter-pipe/ ./src/" }, "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 00000000..11229c92 --- /dev/null +++ b/src/index.ts @@ -0,0 +1 @@ +export * from './ngx-filter.module'; \ No newline at end of file diff --git a/src/ngx-filter.pipe.spec.ts b/src/ngx-filter.pipe.spec.ts index 7183e305..dd42173c 100644 --- a/src/ngx-filter.pipe.spec.ts +++ b/src/ngx-filter.pipe.spec.ts @@ -100,6 +100,36 @@ describe('Pipe: FilterPipe', () => { expect(pipe.transform(objects, { name: 'Qwe123' })).toEqual([]); }); + it('should get value from getter defined in class ancestor', () => { + class User { + firstName: string; + lastName: string; + + constructor(first: string, last: string) { + this.firstName = first; + this.lastName = last; + } + + get name() { + return `${ this.firstName } ${ this.lastName }`; + } + } + + class UserEx extends User { + + } + + const userA = new UserEx('Abc', '123'); + const objects = [ + userA, + new UserEx('Qwe', '123') + ]; + + expect(pipe.transform(objects, { name: '123' })).toEqual(objects); + expect(pipe.transform(objects, { name: 'Abc 123' })).toEqual([userA]); + expect(pipe.transform(objects, { name: 'Qwe123' })).toEqual([]); + }); + it('should filter by empty filter string', () => { const objects = [ 'test', @@ -224,5 +254,23 @@ describe('Pipe: FilterPipe', () => { expect(pipe.transform(objects, { languages: { $or: ['English', 'German'] }, age: 31 })).toEqual([]); expect(pipe.transform(objects, { languages: { $or: ['English'] }, age: 27 })).toEqual([]); }); -}); + it('should filter values with space', () => { + const values = [ + 'John Writer' + ]; + + expect(pipe.transform(values, 'John')).toEqual(values); + expect(pipe.transform(values, 'John W')).toEqual(values); + }); + + it('should filter objects that have strings with spaces', () => { + const objects = [ + { name: 'John Writer' }, + { name: 'John Writer2' } + ]; + + expect(pipe.transform(objects , { name: 'John' })).toEqual(objects); + expect(pipe.transform(objects , { name: 'John w' })).toEqual(objects); + }) +}); diff --git a/src/ngx-filter.pipe.ts b/src/ngx-filter.pipe.ts index d3e628c1..77bd2400 100644 --- a/src/ngx-filter.pipe.ts +++ b/src/ngx-filter.pipe.ts @@ -37,7 +37,16 @@ export class FilterPipe { continue; } - if (!value.hasOwnProperty(key) && !Object.getOwnPropertyDescriptor(Object.getPrototypeOf(value), key)) { + let walker = value; + let found = false; + do { + if (walker.hasOwnProperty(key) || Object.getOwnPropertyDescriptor(walker, key)) { + found = true; + break; + } + } while (walker = Object.getPrototypeOf(walker)); + + if (!found) { return false; } @@ -74,7 +83,6 @@ export class FilterPipe { return (value: any) => { let hasMatch = false; const length = filter.length; - const isArray = value instanceof Array; const arrayComparison = (i) => { return value.indexOf(filter[i]) !== -1; @@ -82,7 +90,7 @@ export class FilterPipe { const otherComparison = (i) => { return value === filter[i]; }; - const comparison = isArray ? arrayComparison : otherComparison; + const comparison = Array.isArray(value) ? arrayComparison : otherComparison; for (let i = 0; i < length; i++) { if (comparison(i)) { @@ -149,4 +157,4 @@ export class FilterPipe { return array.filter(this.filterDefault(filter)); } -} \ No newline at end of file +}