feat(localize): add getCurrencyName
This commit is contained in:
parent
8db04b8bab
commit
708b6f991d
3 changed files with 37 additions and 0 deletions
|
|
@ -6,6 +6,7 @@ export { normalizeDateTime } from './src/date/normalizeDateTime.js';
|
|||
export { parseDate } from './src/date/parseDate.js';
|
||||
export { formatNumber } from './src/number/formatNumber.js';
|
||||
export { formatNumberToParts } from './src/number/formatNumberToParts.js';
|
||||
export { getCurrencyName } from './src/number/getCurrencyName.js';
|
||||
export { getDecimalSeparator } from './src/number/getDecimalSeparator.js';
|
||||
export { getFractionDigits } from './src/number/getFractionDigits.js';
|
||||
export { getGroupSeparator } from './src/number/getGroupSeparator.js';
|
||||
|
|
|
|||
22
packages/localize/src/number/getCurrencyName.js
Normal file
22
packages/localize/src/number/getCurrencyName.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { formatNumberToParts } from './formatNumberToParts.js';
|
||||
|
||||
/**
|
||||
* Based on number, returns currency name like 'US dollar'
|
||||
*
|
||||
* @param {string} currencyIso iso code like USD
|
||||
* @param {Object} options Intl options are available extended by roundMode
|
||||
* @returns {string} currency name like 'US dollar'
|
||||
*/
|
||||
export function getCurrencyName(currencyIso, options) {
|
||||
const parts = formatNumberToParts(1, {
|
||||
...options,
|
||||
style: 'currency',
|
||||
currency: currencyIso,
|
||||
currencyDisplay: 'name',
|
||||
});
|
||||
const currencyName = parts
|
||||
.filter(p => p.type === 'currency')
|
||||
.map(o => o.value)
|
||||
.join(' ');
|
||||
return currencyName;
|
||||
}
|
||||
14
packages/localize/test/number/getCurrencyName.test.js
Normal file
14
packages/localize/test/number/getCurrencyName.test.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { expect } from '@open-wc/testing';
|
||||
import { localizeTearDown } from '../../test-helpers.js';
|
||||
import { getCurrencyName } from '../../src/number/getCurrencyName.js';
|
||||
|
||||
describe('getCurrencyName', () => {
|
||||
afterEach(localizeTearDown);
|
||||
|
||||
it('returns the right name for currency and locale combination', async () => {
|
||||
expect(getCurrencyName('USD', { locale: 'en-GB' })).to.equal('US dollars');
|
||||
expect(getCurrencyName('USD', { locale: 'nl-NL' })).to.equal('Amerikaanse dollar');
|
||||
expect(getCurrencyName('EUR', { locale: 'en-GB' })).to.equal('euros');
|
||||
expect(getCurrencyName('EUR', { locale: 'nl-NL' })).to.equal('euro');
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue