@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
160 lines (159 loc) • 4.92 kB
JavaScript
;
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 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;
}
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);
}
const formatted = (0, _formatCore.formatNumberCoreParts)(cleanedNumber, locale, opts, formatter);
const prepared = (0, _formatCore.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;
}
(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,
parts
});
}
//# sourceMappingURL=formatCurrency.js.map