Skip to content

Commit

Permalink
prepared 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimDez committed Dec 18, 2017
1 parent 01df9f7 commit 3c13017
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "MIT",
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ngx-filter.module';
50 changes: 49 additions & 1 deletion src/ngx-filter.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
})
});
16 changes: 12 additions & 4 deletions src/ngx-filter.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -74,15 +83,14 @@ 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;
};
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)) {
Expand Down Expand Up @@ -149,4 +157,4 @@ export class FilterPipe {

return array.filter(this.filterDefault(filter));
}
}
}

0 comments on commit 3c13017

Please sign in to comment.