lion/packages/ui/components/localize/number/getCurrencyName.js
2022-10-31 16:55:07 +01:00

31 lines
1.1 KiB
JavaScript

import { localize } from '../singleton.js';
import { formatNumberToParts } from './formatNumberToParts.js';
import { forceCurrencyNameForPHPEnGB } from './utils/normalize-get-currency-name/forceCurrencyNameForPHPEnGB.js';
/**
* Based on number, returns currency name like 'US dollar'
*
* @typedef {import('../../types/LocalizeMixinTypes').FormatNumberPart} FormatNumberPart
* @param {string} currencyIso iso code like USD
* @param {import('../../types/LocalizeMixinTypes').FormatNumberOptions} [options] Intl options are available extended by roundMode
* @returns {string} currency name like 'US dollar'
*/
export function getCurrencyName(currencyIso, options) {
const parts = /** @type {FormatNumberPart[]} */ (
formatNumberToParts(1, {
...options,
style: 'currency',
currency: currencyIso,
currencyDisplay: 'name',
})
);
let currencyName = parts
.filter(p => p.type === 'currency')
.map(o => o.value)
.join(' ');
const locale = options?.locale || localize.locale;
if (currencyIso === 'PHP' && locale === 'en-GB') {
currencyName = forceCurrencyNameForPHPEnGB(currencyName);
}
return currencyName;
}