UNPKG

dbl-utils

Version:

Utilities for dbl, adba and others projects

1 lines 8.91 kB
{"version":3,"names":["exports","default","formatValue","moment_1","cov_1lc2qy3cdw","s","__importDefault","require","numeral_1","i18n_1","__importStar","value","conf","f","b","format","locale","getLang","formatConf","formatNumberCompact","context","Number","toLocaleString","formatNumber","globalConf","formatCurrency","Error","Object","assign","style","currency","formatDate","formatTime","formatDateTime"],"sources":["/home/diablo/dev/dbl-utils/src/format-value.ts"],"sourcesContent":["import moment from \"moment\";\r\nimport numeral from \"numeral\";\r\n\r\nimport t, {\r\n formatDate, formatNumber, formatNumberCompact,\r\n formatCurrency, formatTime, formatDateTime, getLang\r\n} from \"./i18n\";\r\n\r\n// Define types for format configurations\r\ntype FormatType =\r\n | 'number-compact'\r\n | 'numbercompact'\r\n | 'number'\r\n | 'currency'\r\n | 'dictionary'\r\n | 'date'\r\n | 'time'\r\n | 'date-time'\r\n | 'datetime';\r\n\r\ninterface FormatConfig {\r\n format?: FormatType;\r\n formatConf?: string | Intl.NumberFormatOptions;\r\n context?: any;\r\n currency?: string;\r\n}\r\n\r\n/**\r\n * Formats a value based on the provided configuration.\r\n * \r\n * @param value - The value to format\r\n * @param conf - Configuration options for formatting\r\n * @returns The formatted value or the original value if the format is not specified\r\n */\r\nexport default function formatValue(value: any, conf: FormatConfig): any {\r\n if (!conf?.format) return value;\r\n\r\n switch (conf.format) {\r\n case 'number-compact':\r\n case 'numbercompact': {\r\n // TODO: move to i18n\r\n // TODO: move to i18n\r\n numeral.locale(getLang());\r\n return numeral(value).format(conf.formatConf as string || formatNumberCompact(conf.context));\r\n }\r\n case 'number': {\r\n return typeof value === 'boolean' ? Number(value)\r\n : value.toLocaleString(getLang(), conf.formatConf || formatNumber(conf.context));\r\n }\r\n case 'currency': {\r\n const globalConf = (conf.formatConf || formatCurrency(conf.context)) as Intl.NumberFormatOptions;\r\n if (typeof globalConf === 'string')\r\n throw new Error(\"currency format must have formatConf as an Intl.NumberFormatOptions\");\r\n\r\n return value.toLocaleString(getLang(), {\r\n ...globalConf,\r\n style: \"currency\",\r\n currency: conf.currency || (globalConf.currency as string),\r\n });\r\n }\r\n case 'dictionary': {\r\n return t(value, conf.context);\r\n }\r\n case 'date': {\r\n return moment(value).format(conf.formatConf as string || formatDate(conf.context));\r\n }\r\n case 'time': {\r\n return moment(value).format(conf.formatConf as string || formatTime(conf.context));\r\n }\r\n case 'date-time':\r\n case 'datetime': {\r\n return moment(value).format(conf.formatConf as string || formatDateTime(conf.context));\r\n }\r\n default: {\r\n return value;\r\n }\r\n }\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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