ngx-t-reports
Version:
Angular module for creating dynamic reports and dashboards. Supports various data sources, custom templates, and real-time updates.
99 lines (96 loc) • 4.74 kB
JavaScript
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 now = +new Date();
const pastDate = +new Date(value);
const seconds = Math.floor((now - pastDate) / 1000);
const futureSeconds = Math.floor((pastDate - now) / 1000);
const intervals = {
year: 31536000,
month: 2592000,
week: 604800,
day: 86400,
hr: 3600,
min: 60,
sec: 1,
};
if (futureSeconds < 15 && futureSeconds >= 0) {
return 'Soon';
}
if (seconds < 15 && seconds >= 0) {
return 'Just now';
}
if (seconds > 0) {
let counter;
for (const i in intervals) {
counter = Math.floor(seconds / intervals[i]);
if (counter > 0) {
return formatCounter(counter, i, 'ago');
}
}
}
else if (futureSeconds > 0) {
let counter;
for (const i in intervals) {
counter = Math.floor(futureSeconds / intervals[i]);
if (counter > 0) {
return 'in ' + formatCounter(counter, i, '');
}
}
}
}
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: "21.2.12", ngImport: i0, type: FormatDataPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "21.2.12", ngImport: i0, type: FormatDataPipe, isStandalone: true, name: "formatData" }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.12", ngImport: i0, type: FormatDataPipe, decorators: [{
type: Pipe,
args: [{
name: 'formatData',
standalone: true, // For Angular 14+ standalone components/pipes. Remove if not applicable.
}]
}] });
function formatCounter(counter, interval, suffix) {
if (counter === 1) {
return counter + ' ' + interval + (suffix ? ' ' + suffix : '');
}
else {
return counter + ' ' + interval + 's' + (suffix ? ' ' + suffix : '');
}
}
export { FormatDataPipe };
//# sourceMappingURL=ngx-t-reports-format-Data.pipe-k2-OWdRN.mjs.map