@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
57 lines • 1.25 kB
JavaScript
export function handleCompactBeforeDisplay({
value,
locale,
compact,
decimals = 0,
opts
}) {
if (!canHandleCompact({
value,
compact
})) {
return;
}
value = parseInt(String(Math.abs(Number(value))), 10);
opts.notation = 'compact';
if (compact === true && locale && /(no|nb|nn)$/i.test(locale)) {
opts.compactDisplay = Math.abs(value) < 1000000 ? 'long' : 'short';
} else if (compact === 'long' || compact === 'short') {
opts.compactDisplay = compact;
}
if (typeof opts.maximumSignificantDigits === 'undefined') {
let decimalCount = parseFloat(String(decimals));
if (isNaN(decimalCount)) {
decimalCount = 0;
}
const ref = String(value).length % 3;
if (ref === 2) {
decimalCount += 1;
} else if (ref === 0) {
decimalCount += 2;
}
opts.maximumSignificantDigits = decimalCount + 1;
}
}
export function handleCompactBeforeAria({
value,
compact,
opts
}) {
if (!canHandleCompact({
value,
compact
})) {
return;
}
opts.compactDisplay = 'long';
}
export function canHandleCompact({
value,
compact
}) {
if (compact && Math.abs(Number(value)) >= 1000) {
return true;
}
return false;
}
//# sourceMappingURL=compact.js.map