UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

142 lines 5.06 kB
import _Object$hasOwn from "core-js-pure/stable/object/has-own.js"; import useNumberFormat from "./useNumberFormat.js"; function useNumberFormatWithParts(value, options = {}) { var _normalizedOptions$cu; const normalizedOptions = { returnAria: true, ...options }; if (!_Object$hasOwn(options, 'currency')) { normalizedOptions.currency = false; } const formatOptions = { ...normalizedOptions, currency_display: normalizedOptions.currencyDisplay, currency_position: normalizedOptions.currencyPosition === 'auto' ? null : normalizedOptions.currencyPosition, omit_currency_sign: normalizedOptions.omitCurrencySign, clean_copy_value: normalizedOptions.cleanCopyValue, omit_rounding: normalizedOptions.omitRounding }; const amountOnly = useNumberFormat(value, { ...formatOptions, omit_currency_sign: true, returnAria: true }); const result = useNumberFormat(value, formatOptions); if (!normalizedOptions.returnAria || typeof result !== 'object' || result === null) { return result; } const formatted = result; if (formatted.type !== 'currency') { return { ...formatted, parts: getFallbackParts(formatted.number) }; } if (typeof amountOnly !== 'object' || amountOnly === null) { return { ...formatted, parts: getFallbackParts(formatted.number) }; } const unsignedAmount = amountOnly; const splitAmount = splitLeadingSign(unsignedAmount.number); const splitFull = splitLeadingSign(formatted.number); const amountWithoutSign = splitAmount.value; const fullWithoutSign = splitFull.value; const amountPosition = fullWithoutSign.indexOf(amountWithoutSign); if (amountPosition < 0) { return { ...formatted, parts: { sign: splitAmount.sign, signedNumber: unsignedAmount.number, number: splitAmount.value, currency: null, currencyPosition: null, spaceAfterCurrency: false, spaceBeforeCurrency: false, percent: null, percentSpacing: '' } }; } const beforeAmount = fullWithoutSign.slice(0, amountPosition).trim(); const afterAmount = fullWithoutSign.slice(amountPosition + amountWithoutSign.length).trim(); const hasCurrencyBefore = beforeAmount.length > 0; const currency = hasCurrencyBefore ? beforeAmount : afterAmount; const hasCurrency = Boolean(currency); const usedCurrencyPosition = (_normalizedOptions$cu = normalizedOptions.currencyPosition) !== null && _normalizedOptions$cu !== void 0 ? _normalizedOptions$cu : 'auto'; let renderCurrencyBefore = hasCurrencyBefore; if (normalizedOptions.signDisplay === 'always' && usedCurrencyPosition === 'auto') { renderCurrencyBefore = false; } if (normalizedOptions.forceCurrencyAfterAmount && usedCurrencyPosition === 'auto') { renderCurrencyBefore = false; } const shouldOmitCurrencySpace = normalizedOptions.signDisplay === 'always' && usedCurrencyPosition === 'auto'; const spaceAfterCurrency = renderCurrencyBefore && hasCurrency && !shouldOmitCurrencySpace; const spaceBeforeCurrency = !renderCurrencyBefore && hasCurrency && !shouldOmitCurrencySpace; return { ...formatted, parts: { sign: splitAmount.sign, signedNumber: unsignedAmount.number, number: splitAmount.value, currency: currency || null, currencyPosition: hasCurrency ? renderCurrencyBefore ? 'before' : 'after' : null, spaceAfterCurrency, spaceBeforeCurrency, percent: null, percentSpacing: '' } }; } function getFallbackParts(number) { var _splitNumber$sign; const splitNumber = splitLeadingSign(number); const splitPercent = splitTrailingPercent(splitNumber.value); const hasPercent = Boolean(splitPercent); const valueWithoutPercent = hasPercent ? splitPercent.number : splitNumber.value; return { sign: splitNumber.sign, signedNumber: `${(_splitNumber$sign = splitNumber.sign) !== null && _splitNumber$sign !== void 0 ? _splitNumber$sign : ''}${valueWithoutPercent}`, number: valueWithoutPercent, currency: null, currencyPosition: null, spaceAfterCurrency: false, spaceBeforeCurrency: false, percent: hasPercent ? splitPercent.percent : null, percentSpacing: hasPercent ? splitPercent.spacing : '' }; } function splitTrailingPercent(value) { const match = String(value || '').match(/^(.*?)([\u00a0\s]*[%٪])$/); if (!match) { return null; } const before = match[1]; const suffix = match[2]; const percent = suffix.trim(); return { number: before, percent, spacing: suffix.slice(0, -percent.length) }; } function splitLeadingSign(value) { const normalizedValue = String(value || '').replace(/^[\u200e\u200f\u061c\s]+/, ''); const match = normalizedValue.match(/^([+\-−])\s?(.*)$/); if (!match) { return { sign: null, value: normalizedValue }; } return { sign: match[1], value: match[2] }; } export default useNumberFormatWithParts; //# sourceMappingURL=useNumberFormatWithParts.js.map