- Object Filter - objectfilter.pipe.ts
- Object Sum - objectsum.pipe.ts
- Money Formatter - money.pipe.ts
Last active
September 10, 2019 17:28
-
-
Save dvdvnl/f4cf901165631bcb3ffc4ea558928f3e to your computer and use it in GitHub Desktop.
Angular Pipes (gist for https://www.davole.com/2017/angular-pipes/)
This file contains 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: 'money' | |
}) | |
export class MoneyPipe implements PipeTransform { | |
transform(value: number, symbol?: string): string { | |
if(typeof value == 'undefined') return null; | |
let matches = String(value.toFixed(2)).match(/(-*)(\d*)\.?(\d*)/); | |
let integers = this.reverse(this.split(this.reverse(matches[2])).join('.')); | |
let decimals = matches[3]; | |
let formatted = matches[1] + integers + ',' + decimals; | |
return symbol ? formatted + ' ' + symbol : formatted; | |
} | |
private reverse(str:string):string{ | |
return str.split('').reverse().join(''); | |
} | |
private split(str:string):Array<string>{ | |
let rtn = []; | |
for(let i = 0; i < str.length; i = i + 3) | |
rtn.push(str.substring(i, i + 3)); | |
return rtn; | |
} | |
} |
This file contains 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: 'objectfilter' | |
}) | |
export class ObjectfilterPipe implements PipeTransform { | |
transform(items:any[], search:string, keys?:Array<string>):any { | |
return typeof items == 'undefined' || search == '' || typeof search == 'undefined' ? items : items.filter(item => this.match(item, search, keys)); | |
} | |
private match(item:any[], search:string, keys?:Array<string>):Boolean{ | |
let flat = this.flatten(item, keys); | |
for(let query of search.split(' ')) | |
if(!flat.match(new RegExp(query, 'i'))) | |
return false; | |
return true; | |
} | |
private flatten(items:any[], keys?:Array<string>, route?:string):string{ | |
let val:string = ''; | |
for(let key of Object.keys(items)){ | |
let currentRoute = typeof route == 'undefined' ? key : route + '.' + key; | |
if(typeof items[key] == 'object') | |
val = val + this.flatten(items[key], keys, currentRoute); | |
else if(items[key] !== '' && typeof items[key] !== 'undefined' && (typeof keys == 'undefined' || keys.indexOf(currentRoute) >= 0)) | |
val = val + items[key]; | |
} | |
return val; | |
} | |
} |
This file contains 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: 'objectsum' | |
}) | |
export class ObjectsumPipe implements PipeTransform { | |
transform(items:any[], path:string):number { | |
let sum: number = 0; | |
for(let item of items){ | |
let keys = path.split('.'); | |
while(keys.length && (item = item[keys.shift()])); | |
sum += +item; | |
} | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment