UNPKG

ngx-t-reports

Version:

Angular module for creating dynamic reports and dashboards. Supports various data sources, custom templates, and real-time updates.

78 lines (75 loc) 3.9 kB
import * as i0 from '@angular/core'; import { Pipe } from '@angular/core'; import { CurrencyPipe, DatePipe } from '@angular/common'; class FormatDataPipe { transform(value, dataType, pipeConfig) { try { switch (dataType) { case 'date': const datePipe = new DatePipe('en-US'); // You might want to inject this or make locale configurable return datePipe.transform(value, 'mediumDate'); case 'currency': // The 'R' for ZAR is hardcoded here as per the original function. // Consider making the currency code and locale configurable if needed. const currencyPipeForCurrencyType = new CurrencyPipe('en-US', pipeConfig?.param || 'ZAR'); const numberValueForCurrency = parseFloat(value); return currencyPipeForCurrencyType.transform(numberValueForCurrency, pipeConfig?.param); case 'number': if (pipeConfig?.pipeType === 'currency') { const currencyPipeForNumberType = new CurrencyPipe('en-US'); // Locale hardcoded const currencyCode = pipeConfig?.param; const numberValueForNumber = parseFloat(value); return currencyPipeForNumberType.transform(numberValueForNumber, currencyCode); } else { return value; } case 'daysAgo': if (value) { const seconds = Math.floor((+new Date() - +new Date(value)) / 1000); if (seconds < 15) // less than 30 seconds ago will show as 'Just now' return 'Just now'; const intervals = { year: 31536000, month: 2592000, week: 604800, day: 86400, hr: 3600, min: 60, sec: 1, }; let counter; for (const i in intervals) { counter = Math.floor(seconds / intervals[i]); if (counter > 0) if (counter === 1) { return counter + ' ' + i + ' ago'; // singular (1 day ago) } else { return counter + ' ' + i + 's ago'; // plural (2 days ago) } } } return value; default: return value; } } catch (error) { console.error('Error in FormatDataPipe:', error); return value; // Return original value in case of an error } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: FormatDataPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.6", ngImport: i0, type: FormatDataPipe, isStandalone: true, name: "formatData" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: FormatDataPipe, decorators: [{ type: Pipe, args: [{ name: 'formatData', standalone: true, // For Angular 14+ standalone components/pipes. Remove if not applicable. }] }] }); export { FormatDataPipe }; //# sourceMappingURL=ngx-t-reports-format-Data.pipe-QDzIba5Q.mjs.map