@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
154 lines • 4.72 kB
JavaScript
import { ABSENT_VALUE_FORMAT, isAbsent, buildReturn, cleanNumber, formatDecimals, formatNumberCore, formatNumberCoreParts, handleCompactBeforeAria, handleCompactBeforeDisplay, prepareFormatOptions, prepareMinusParts, resolveLocale, enhanceSR } from "./formatCore.js";
import { currencyPositionFormatter } from "./currencyPosition.js";
import { getFallbackCurrencyDisplay, CURRENCY } from "./currencyDisplay.js";
import { alignCurrencySymbol } from "./formatNumberCore.js";
function joinParts(parts) {
return parts.reduce((acc, {
value
}) => acc + value, '');
}
function trimBoundaryLiterals(parts) {
const nextParts = parts.filter(item => item.value);
while (nextParts[0]?.type === 'literal' && nextParts[0].value.trim() === '') {
nextParts.shift();
}
while (nextParts.length > 0) {
const lastPart = nextParts[nextParts.length - 1];
if (lastPart.type !== 'literal' || lastPart.value.trim() !== '') {
break;
}
nextParts.pop();
}
return nextParts;
}
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);
}
const formatted = formatNumberCoreParts(cleanedNumber, locale, opts, formatter);
const prepared = prepareMinusParts(formatted.number, formatted.parts, locale);
let display = prepared.number;
let parts = prepared.parts.length ? prepared.parts : undefined;
if (resolvedPosition && currencySuffix) {
if (resolvedPosition === 'after') {
display = `${display.trim()} ${currencySuffix}`;
parts = parts && [...trimBoundaryLiterals(parts), {
type: 'literal',
value: ' '
}, {
type: 'currency',
value: currencySuffix
}];
} else if (resolvedPosition === 'before') {
display = `${currencySuffix} ${display.trim()}`;
parts = parts && [{
type: 'currency',
value: currencySuffix
}, {
type: 'literal',
value: ' '
}, ...trimBoundaryLiterals(parts)];
}
}
if (parts && joinParts(parts) !== display) {
parts = undefined;
}
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,
parts
});
}
//# sourceMappingURL=formatCurrency.js.map