@lion/localize
Version:
The localization system helps to manage localization data split into locales and automate its loading
36 lines (35 loc) • 1.41 kB
JavaScript
/**
* When in some locales there is no space between currency and amount it is added
*
* @typedef {import('../../../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {FormatNumberPart[]} formattedParts
* @param {import('../../../../types/LocalizeMixinTypes').FormatNumberOptions} [options]
* @returns {FormatNumberPart[]}
*/
export function forceSpaceBetweenCurrencyCodeAndNumber(
formattedParts,
{ currency, currencyDisplay } = {},
) {
const numberOfParts = formattedParts.length;
const literalObject = { type: 'literal', value: ' ' };
if (numberOfParts > 1 && currency && currencyDisplay === 'code') {
if (formattedParts[0].type === 'currency' && formattedParts[1].type !== 'literal') {
// currency in front of a number: EUR 1.00
formattedParts.splice(1, 0, literalObject);
} else if (
formattedParts[0].type === 'minusSign' &&
formattedParts[1].type === 'currency' &&
formattedParts[2].type !== 'literal'
) {
// currency in front of a negative number: -EUR 1.00
formattedParts.splice(2, 0, literalObject);
} else if (
formattedParts[numberOfParts - 1].type === 'currency' &&
formattedParts[numberOfParts - 2].type !== 'literal'
) {
// currency in behind a number: 1.00 EUR || -1.00 EUR
formattedParts.splice(numberOfParts - 1, 0, literalObject);
}
}
return formattedParts;
}