Created
December 11, 2016 22:13
-
-
Save joejordanbrown/6fca2b0db0450a84b923bcea33ed9ad0 to your computer and use it in GitHub Desktop.
Angular2 Pipe Filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ | |
name: 'filterBy' | |
}) | |
export class FilterByPipe implements PipeTransform { | |
private filterByString(filter) { | |
filter = filter.toLowerCase(); | |
return value => { | |
return !filter || value.toLowerCase().indexOf(filter) !== -1; | |
} | |
} | |
private filterByObject(filter) { | |
return value => { | |
for (let key in filter) { | |
if (!value.hasOwnProperty(key)) { | |
return false; | |
} | |
const type = typeof value[key]; | |
let isMatching; | |
if (type === 'string') { | |
isMatching = this.filterByString(filter[key])(value[key]); | |
} else if (type === 'object') { | |
isMatching = this.filterByObject(filter[key])(value[key]); | |
} else { | |
isMatching = this.filterDefault(filter[key])(value[key]); | |
} | |
if (!isMatching) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
/** | |
* Defatul filterDefault function | |
* | |
* @param filter | |
* @returns {(value:any)=>boolean} | |
*/ | |
private filterDefault(filter) { | |
return value => { | |
return !filter || filter == value; | |
} | |
} | |
private isNumber(value) { | |
return !isNaN(parseInt(value, 10)) && isFinite(value); | |
} | |
transform(array: any[], filter: any): any { | |
const type = typeof filter; | |
if (type === 'string') { | |
if (this.isNumber(filter)) { | |
return array.filter(this.filterDefault(filter)); | |
} | |
return array.filter(this.filterByString(filter)); | |
} | |
if (type === 'object') { | |
return array.filter(this.filterByObject(filter)); | |
} | |
return array.filter(this.filterDefault(filter)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment