unicorn-components
Version:
<a target="_blank" href="https://getunicorn.io"><img src="https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2017/Jul/07/2615006260-5-nitsnetsstudios-ondemand-UNI_avatar.png" align="left"></a>
21 lines (17 loc) • 950 B
text/typescript
import { CurrencyPipe, DatePipe, DecimalPipe, PercentPipe } from '@angular/common';
import { Inject, LOCALE_ID, Pipe, PipeTransform } from '@angular/core';
({ name: 'format' })
export class FormatPipe implements PipeTransform {
constructor( (LOCALE_ID) private _locale: string) { }
transform(value: string, format: string = null, locale: string = null): string {
if (!value || !format) { return value; }
switch (format) {
case 'percentage': return (new PercentPipe(locale || this._locale)).transform(value);
case 'decimal': return (new DecimalPipe(locale || this._locale)).transform(value);
case 'currency': return (new CurrencyPipe(locale || this._locale)).transform(value);
case 'date': return (new DatePipe(locale || this._locale)).transform(value);
default: console.warn(`Format not available: ${format}`);
}
return value;
}
}