Skip to content

Commit 3c13017

Browse files
committed
prepared 1.0.1
1 parent 01df9f7 commit 3c13017

5 files changed

+68
-7
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.0.1
4+
* [[#46](https://github.com/VadimDez/ngx-filter-pipe/issues/46)] - Filter by property/method on prototype chain.
5+
* [[#47](https://github.com/VadimDez/ngx-filter-pipe/pull/47)] - walk the prototype chain to check if field/property exists.
6+
37
## 1.0.0
48
* [[#36](https://github.com/VadimDez/ngx-filter-pipe/issues/36)] - Changed name tp `ngx-filter-pipe`.
59
* [[#38](https://github.com/VadimDez/ngx-filter-pipe/issues/38)] - Added UMD bundle.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-filter-pipe",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Angular 2+ pipeline for filtering arrays",
55
"author": "Vadym Yatsyuk <[email protected]>",
66
"license": "MIT",
@@ -12,7 +12,7 @@
1212
"e2e": "protractor",
1313
"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",
1414
"build": "rimraf dist && tsc -p tsconfig-esm.json && rollup -c && rollup -c --environment MINIFY && ngc -p tsconfig.json",
15-
"copy": "cp -R ./examples/ng-cli/src/app/order-pipe/ ./src/"
15+
"copy": "cp -R ./examples/ng-cli/src/app/shared/ngx-filter-pipe/ ./src/"
1616
},
1717
"repository": {
1818
"type": "git",

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './ngx-filter.module';

src/ngx-filter.pipe.spec.ts

+49-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ describe('Pipe: FilterPipe', () => {
100100
expect(pipe.transform(objects, { name: 'Qwe123' })).toEqual([]);
101101
});
102102

103+
it('should get value from getter defined in class ancestor', () => {
104+
class User {
105+
firstName: string;
106+
lastName: string;
107+
108+
constructor(first: string, last: string) {
109+
this.firstName = first;
110+
this.lastName = last;
111+
}
112+
113+
get name() {
114+
return `${ this.firstName } ${ this.lastName }`;
115+
}
116+
}
117+
118+
class UserEx extends User {
119+
120+
}
121+
122+
const userA = new UserEx('Abc', '123');
123+
const objects = [
124+
userA,
125+
new UserEx('Qwe', '123')
126+
];
127+
128+
expect(pipe.transform(objects, { name: '123' })).toEqual(objects);
129+
expect(pipe.transform(objects, { name: 'Abc 123' })).toEqual([userA]);
130+
expect(pipe.transform(objects, { name: 'Qwe123' })).toEqual([]);
131+
});
132+
103133
it('should filter by empty filter string', () => {
104134
const objects = [
105135
'test',
@@ -224,5 +254,23 @@ describe('Pipe: FilterPipe', () => {
224254
expect(pipe.transform(objects, { languages: { $or: ['English', 'German'] }, age: 31 })).toEqual([]);
225255
expect(pipe.transform(objects, { languages: { $or: ['English'] }, age: 27 })).toEqual([]);
226256
});
227-
});
228257

258+
it('should filter values with space', () => {
259+
const values = [
260+
'John Writer'
261+
];
262+
263+
expect(pipe.transform(values, 'John')).toEqual(values);
264+
expect(pipe.transform(values, 'John W')).toEqual(values);
265+
});
266+
267+
it('should filter objects that have strings with spaces', () => {
268+
const objects = [
269+
{ name: 'John Writer' },
270+
{ name: 'John Writer2' }
271+
];
272+
273+
expect(pipe.transform(objects , { name: 'John' })).toEqual(objects);
274+
expect(pipe.transform(objects , { name: 'John w' })).toEqual(objects);
275+
})
276+
});

src/ngx-filter.pipe.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@ export class FilterPipe {
3737
continue;
3838
}
3939

40-
if (!value.hasOwnProperty(key) && !Object.getOwnPropertyDescriptor(Object.getPrototypeOf(value), key)) {
40+
let walker = value;
41+
let found = false;
42+
do {
43+
if (walker.hasOwnProperty(key) || Object.getOwnPropertyDescriptor(walker, key)) {
44+
found = true;
45+
break;
46+
}
47+
} while (walker = Object.getPrototypeOf(walker));
48+
49+
if (!found) {
4150
return false;
4251
}
4352

@@ -74,15 +83,14 @@ export class FilterPipe {
7483
return (value: any) => {
7584
let hasMatch = false;
7685
const length = filter.length;
77-
const isArray = value instanceof Array;
7886

7987
const arrayComparison = (i) => {
8088
return value.indexOf(filter[i]) !== -1;
8189
};
8290
const otherComparison = (i) => {
8391
return value === filter[i];
8492
};
85-
const comparison = isArray ? arrayComparison : otherComparison;
93+
const comparison = Array.isArray(value) ? arrayComparison : otherComparison;
8694

8795
for (let i = 0; i < length; i++) {
8896
if (comparison(i)) {
@@ -149,4 +157,4 @@ export class FilterPipe {
149157

150158
return array.filter(this.filterDefault(filter));
151159
}
152-
}
160+
}

0 commit comments

Comments
 (0)