UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

121 lines (120 loc) 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatCurrency = formatCurrency; var _formatCore = require("./formatCore.js"); var _currencyPosition = require("./currencyPosition.js"); var _currencyDisplay = require("./currencyDisplay.js"); var _formatNumberCore = require("./formatNumberCore.js"); function formatCurrency(value, { locale: inputLocale = null, clean = false, compact = null, currency = true, currencyDisplay = null, currencyPosition = null, omitCurrencySign = null, decimals = null, rounding = null, signDisplay = null, options = null, returnAria = false, invalidAriaText = null, cleanCopyValue = null } = {}) { value = (0, _formatCore.isAbsent)(value) ? _formatCore.ABSENT_VALUE_FORMAT : value; const locale = (0, _formatCore.resolveLocale)(inputLocale); const opts = (0, _formatCore.prepareFormatOptions)({ options, signDisplay }); if (clean) { value = (0, _formatCore.cleanNumber)(value); } opts.currency = opts.currency || (currency === true ? _currencyDisplay.CURRENCY : currency || undefined); (0, _formatCore.handleCompactBeforeDisplay)({ value, locale, compact, decimals, opts }); if (parseFloat(String(decimals)) >= 0) { value = (0, _formatCore.formatDecimals)(value, decimals, rounding, opts); } else if (decimals === null) { decimals = 2; value = (0, _formatCore.formatDecimals)(value, decimals, rounding, opts); } const cleanedNumber = parseFloat(String(decimals)) >= 0 ? value : clean ? (0, _formatCore.cleanNumber)(value) : value; if (currencyDisplay === false || currencyDisplay === '') { omitCurrencySign = true; } opts.style = 'currency'; opts.currencyDisplay = (0, _currencyDisplay.getFallbackCurrencyDisplay)(locale, opts.currencyDisplay || currencyDisplay); if (typeof opts.minimumFractionDigits === 'undefined' && String(value).indexOf('.') === -1 && Number(cleanedNumber) % 1 === 0) { opts.minimumFractionDigits = 0; } let formatter = null; if (omitCurrencySign) { formatter = item => { switch (item.type) { case 'literal': item.value = item.value === ' ' ? '' : item.value; return item; case 'currency': item.value = ''; return item; default: return item; } }; } let resolvedPosition = currencyPosition; if (!resolvedPosition && locale && /(no|nb|nn)$/i.test(locale)) { resolvedPosition = 'after'; } let currencySuffix = null; if (resolvedPosition) { formatter = (0, _currencyPosition.currencyPositionFormatter)(formatter, ({ value: currencyValue }) => { return currencySuffix = (0, _formatNumberCore.alignCurrencySymbol)(currencyValue.trim(), currencyDisplay); }, resolvedPosition); } let display = (0, _formatCore.formatNumberCore)(cleanedNumber, locale, opts, formatter); display = (0, _formatCore.prepareMinus)(display, locale); if (resolvedPosition && currencySuffix) { if (resolvedPosition === 'after') { display = `${display.trim()} ${currencySuffix}`; } else if (resolvedPosition === 'before') { display = `${currencySuffix} ${display.trim()}`; } } (0, _formatCore.handleCompactBeforeAria)({ value, compact, opts }); let aria = (0, _formatCore.formatNumberCore)(cleanedNumber, locale, { minimumFractionDigits: 0, maximumFractionDigits: 2, ...opts, currencyDisplay: 'name' }); aria = (0, _formatCore.enhanceSR)(cleanedNumber, aria, locale); if (!returnAria) { return display; } return (0, _formatCore.buildReturn)({ value, locale, display, aria, type: 'currency', opts, cleanCopyValue, invalidAriaText }); } //# sourceMappingURL=formatCurrency.js.map