@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
115 lines • 3.59 kB
JavaScript
import { ABSENT_VALUE_FORMAT, isAbsent, buildReturn, cleanNumber, formatDecimals, formatNumberCore, handleCompactBeforeAria, handleCompactBeforeDisplay, prepareFormatOptions, prepareMinus, resolveLocale, enhanceSR } from "./formatCore.js";
import { currencyPositionFormatter } from "./currencyPosition.js";
import { getFallbackCurrencyDisplay, CURRENCY } from "./currencyDisplay.js";
import { alignCurrencySymbol } from "./formatNumberCore.js";
export 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 = isAbsent(value) ? ABSENT_VALUE_FORMAT : value;
const locale = resolveLocale(inputLocale);
const opts = prepareFormatOptions({
options,
signDisplay
});
if (clean) {
value = cleanNumber(value);
}
opts.currency = opts.currency || (currency === true ? CURRENCY : currency || undefined);
handleCompactBeforeDisplay({
value,
locale,
compact,
decimals,
opts
});
if (parseFloat(String(decimals)) >= 0) {
value = formatDecimals(value, decimals, rounding, opts);
} else if (decimals === null) {
decimals = 2;
value = formatDecimals(value, decimals, rounding, opts);
}
const cleanedNumber = parseFloat(String(decimals)) >= 0 ? value : clean ? cleanNumber(value) : value;
if (currencyDisplay === false || currencyDisplay === '') {
omitCurrencySign = true;
}
opts.style = 'currency';
opts.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 = currencyPositionFormatter(formatter, ({
value: currencyValue
}) => {
return currencySuffix = alignCurrencySymbol(currencyValue.trim(), currencyDisplay);
}, resolvedPosition);
}
let display = formatNumberCore(cleanedNumber, locale, opts, formatter);
display = prepareMinus(display, locale);
if (resolvedPosition && currencySuffix) {
if (resolvedPosition === 'after') {
display = `${display.trim()} ${currencySuffix}`;
} else if (resolvedPosition === 'before') {
display = `${currencySuffix} ${display.trim()}`;
}
}
handleCompactBeforeAria({
value,
compact,
opts
});
let aria = formatNumberCore(cleanedNumber, locale, {
minimumFractionDigits: 0,
maximumFractionDigits: 2,
...opts,
currencyDisplay: 'name'
});
aria = enhanceSR(cleanedNumber, aria, locale);
if (!returnAria) {
return display;
}
return buildReturn({
value,
locale,
display,
aria,
type: 'currency',
opts,
cleanCopyValue,
invalidAriaText
});
}
//# sourceMappingURL=formatCurrency.js.map