UNPKG

uno-js

Version:

JS/TS common used functions, zero dependencies

92 lines (91 loc) 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toPercentage = exports.toMoney = exports.formatter = void 0; const dateUtils_1 = require("./dateUtils"); const money_1 = require("./money"); const truncate_1 = require("./truncate"); const defaultLocate = 'en-US'; const defaultCurrency = 'USD'; const defaultOptions = { keepFormat: false, decimals: 2, nullValue: 'N/A', ignoreUndefined: false, locale: defaultLocate, currency: defaultCurrency, showCurrency: false, }; const formatMoney = (stringData, nullValue, locale, currency, showCurrency) => { const parsedMoney = Number.parseFloat(stringData); const amount = !parsedMoney ? nullValue : new Intl.NumberFormat(locale, { style: 'currency', currency }).format((0, truncate_1.truncate)(parsedMoney, 100000)); return showCurrency ? `${amount} ${currency}` : amount; }; const formatDays = (stringData) => (Number(stringData) === 1 ? '1 day' : `${stringData} days`); const formatMonths = (stringData) => (Number(stringData) === 1 ? '1 month' : `${stringData} months`); const formatPercentage = (stringData, decimals) => { if (decimals === 0) return `${Math.round(Number.parseFloat(stringData))}%`; return `${(0, truncate_1.truncate)(Number.parseFloat(stringData)).toFixed(decimals)}%`; }; const internalFotmatter = (stringData, { keepFormat, decimals, nullValue, locale, currency, showCurrency, }, format) => { switch (format) { case 'money': return formatMoney(stringData, nullValue, locale, currency, showCurrency); case 'percentage': return formatPercentage(stringData, decimals); case 'number': return `${Number.parseInt(stringData, 10)}`; case 'date': if (keepFormat) return (0, dateUtils_1.toLocalTime)(stringData).toLocaleDateString(locale); return new Date(stringData).toLocaleDateString(locale); case 'decimal': { const parsedDecimal = Number.parseFloat(stringData); return !parsedDecimal ? nullValue : parsedDecimal.toFixed(2); } case 'days': return formatDays(stringData); case 'months': return formatMonths(stringData); default: return stringData; } }; const formatter = (data, format, options) => { if ((0, money_1.isMoneyObject)(data)) return (0, exports.formatter)(data.Amount, 'money', { ...defaultOptions, ...options, currency: data.Currency, }); const { keepFormat, decimals, nullValue, ignoreUndefined, locale, currency, showCurrency } = { ...defaultOptions, ...options, }; if (data === undefined && !ignoreUndefined) return undefined; return data === null ? nullValue : internalFotmatter(String(data), { keepFormat, decimals, nullValue, locale, currency, showCurrency }, format); }; exports.formatter = formatter; const toMoney = (data, options) => { const defaultOptions = { ...options, nullValue: new Intl.NumberFormat(options?.locale ?? defaultLocate, { style: 'currency', currency: options?.currency ?? defaultCurrency, }).format(0), }; if ((0, money_1.isMoneyObject)(data)) return (0, exports.formatter)(data.Amount, 'money', { ...defaultOptions, currency: data.Currency, }); return (0, exports.formatter)(data, 'money', defaultOptions); }; exports.toMoney = toMoney; const toPercentage = (data, options) => (0, exports.formatter)(data, 'percentage', options); exports.toPercentage = toPercentage;