UNPKG

ngx-t-reports

Version:

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

1 lines 7.63 kB
{"version":3,"file":"ngx-t-reports-format-Data.pipe-k2-OWdRN.mjs","sources":["../../../projects/ngx-t-reports/src/lib/pipes/format-Data.pipe.ts"],"sourcesContent":["import { Pipe, PipeTransform } from '@angular/core';\r\nimport { CurrencyPipe, DatePipe } from '@angular/common';\r\nimport { DocumentLitsLabelConfigInterfaceValueType, FormColumnInputs, InputDataTypes } from 'ngx-t-forms-types'; // Assuming this path is correct relative to your pipe file\r\n\r\n@Pipe({\r\n name: 'formatData',\r\n standalone: true, // For Angular 14+ standalone components/pipes. Remove if not applicable.\r\n})\r\nexport class FormatDataPipe implements PipeTransform {\r\n\r\n transform(\r\n value: any,\r\n dataType: DocumentLitsLabelConfigInterfaceValueType | InputDataTypes | undefined,\r\n pipeConfig?: FormColumnInputs['pipe'] | undefined\r\n ): any {\r\n try {\r\n switch (dataType) {\r\n case 'date':\r\n const datePipe = new DatePipe('en-US'); // You might want to inject this or make locale configurable\r\n return datePipe.transform(value, 'mediumDate');\r\n case 'currency':\r\n // The 'R' for ZAR is hardcoded here as per the original function.\r\n // Consider making the currency code and locale configurable if needed.\r\n const currencyPipeForCurrencyType = new CurrencyPipe('en-US', pipeConfig?.param || 'ZAR');\r\n const numberValueForCurrency = parseFloat(value);\r\n return currencyPipeForCurrencyType.transform(numberValueForCurrency, pipeConfig?.param);\r\n case 'number':\r\n if (pipeConfig?.pipeType === 'currency') {\r\n const currencyPipeForNumberType = new CurrencyPipe('en-US'); // Locale hardcoded\r\n const currencyCode = pipeConfig?.param;\r\n const numberValueForNumber = parseFloat(value);\r\n return currencyPipeForNumberType.transform(numberValueForNumber, currencyCode);\r\n } else {\r\n return value;\r\n }\r\n case 'daysAgo':\r\n if (value) {\r\n const now = +new Date();\r\n const pastDate = +new Date(value);\r\n const seconds = Math.floor((now - pastDate) / 1000);\r\n const futureSeconds = Math.floor((pastDate - now) / 1000);\r\n\r\n const intervals: any = {\r\n year: 31536000,\r\n month: 2592000,\r\n week: 604800,\r\n day: 86400,\r\n hr: 3600,\r\n min: 60,\r\n sec: 1,\r\n };\r\n\r\n if (futureSeconds < 15 && futureSeconds >= 0) {\r\n return 'Soon';\r\n }\r\n\r\n if (seconds < 15 && seconds >= 0) {\r\n return 'Just now';\r\n }\r\n\r\n if (seconds > 0) {\r\n let counter;\r\n for (const i in intervals) {\r\n counter = Math.floor(seconds / intervals[i]);\r\n if (counter > 0) {\r\n return formatCounter(counter, i, 'ago');\r\n }\r\n }\r\n } else if (futureSeconds > 0) {\r\n let counter;\r\n for (const i in intervals) {\r\n counter = Math.floor(futureSeconds / intervals[i]);\r\n if (counter > 0) {\r\n return 'in ' + formatCounter(counter, i, '');\r\n }\r\n }\r\n }\r\n }\r\n return value;\r\n default:\r\n return value;\r\n }\r\n } catch (error) {\r\n console.error('Error in FormatDataPipe:', error);\r\n return value; // Return original value in case of an error\r\n }\r\n }\r\n}\r\n\r\n\r\nfunction formatCounter(counter: number, interval: string, suffix: string): string {\r\n if (counter === 1) {\r\n return counter + ' ' + interval + (suffix ? ' ' + suffix : '');\r\n } else {\r\n return counter + ' ' + interval + 's' + (suffix ? ' ' + suffix : '');\r\n }\r\n}"],"names":[],"mappings":";;;;MAQa,cAAc,CAAA;AAEvB,IAAA,SAAS,CACL,KAAU,EACV,QAAgF,EAChF,UAAiD,EAAA;AAEjD,QAAA,IAAI;YACA,QAAQ,QAAQ;AACZ,gBAAA,KAAK,MAAM;oBACP,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,OAAO,CAAC,CAAC;oBACvC,OAAO,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAE,YAAY,CAAC;AAClD,gBAAA,KAAK,UAAU;;;AAGX,oBAAA,MAAM,2BAA2B,GAAG,IAAI,YAAY,CAAC,OAAO,EAAE,UAAU,EAAE,KAAK,IAAI,KAAK,CAAC;AACzF,oBAAA,MAAM,sBAAsB,GAAG,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