UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

132 lines 3.57 kB
import _JSON$parse from "core-js-pure/stable/json/parse.js"; import { LOCALE } from "../../../shared/defaults.js"; import { warn } from "../../../shared/component-helper.js"; import { ABSENT_VALUE_FORMAT, isAbsent } from "./constants.js"; import { cleanNumber } from "./cleanNumber.js"; import { formatDecimals } from "./decimals.js"; import { formatNumberCore, prepareMinus, enhanceSR } from "./formatNumberCore.js"; import { getThousandsSeparator } from "./separators.js"; import { handleCompactBeforeDisplay, handleCompactBeforeAria } from "./compact.js"; import locales from "../../../shared/locales/index.js"; export function resolveLocale(locale) { if (!locale) { return LOCALE; } if (locale === 'auto') { try { return window.navigator.language; } catch (e) { warn(e); } } return locale; } export function prepareFormatOptions({ options, signDisplay }) { const opts = (typeof options === 'string' && options[0] === '{' ? _JSON$parse(options) : options) || {}; if (signDisplay) { opts.signDisplay = signDisplay; } return opts; } export { ABSENT_VALUE_FORMAT, isAbsent }; export function buildReturn({ value, locale, display, aria, type, opts, cleanCopyValue, invalidAriaText }) { let cleanedValue; if (cleanCopyValue) { cleanedValue = formatNumberCore(opts.style === 'percent' ? Number(value) / 100 : value, locale, opts, item => { switch (item.type) { case 'group': case 'literal': case 'currency': case 'percentSign': item.value = ''; return item; default: return item; } }); } else { const thousandsSeparator = getThousandsSeparator(locale); cleanedValue = String(display).replace(new RegExp(`${thousandsSeparator}(?=\\d{3})`, 'g'), ''); } if (value === 'invalid') { var _locales$locale; aria = invalidAriaText || ((_locales$locale = locales[locale]) === null || _locales$locale === void 0 ? void 0 : _locales$locale.NumberFormat.notAvailable) || 'N/A'; } return { value, cleanedValue, number: display, aria, locale, type }; } export function applyDecimalsForPlain({ value, decimals, rounding, opts }) { if (parseFloat(String(decimals)) >= 0) { return formatDecimals(value, decimals, rounding, opts); } else if (typeof opts.maximumFractionDigits === 'undefined') { opts.maximumFractionDigits = 20; } return value; } export { cleanNumber, formatDecimals, formatNumberCore, prepareMinus, enhanceSR, handleCompactBeforeDisplay, handleCompactBeforeAria }; export function formatWith(type, formatterFn) { function formatter(value, { locale: inputLocale = null, clean = null, options = null, signDisplay = 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); } const { number: display, aria: initialAria } = formatterFn(value, locale); if (type === 'phone' && clean === null) { value = cleanNumber(value); } if (!returnAria) { return display; } return buildReturn({ value, locale, display, aria: initialAria, type, opts, cleanCopyValue, invalidAriaText }); } return formatter; } //# sourceMappingURL=formatCore.js.map