Skip to content
This repository has been archived by the owner on May 4, 2020. It is now read-only.

feat(@formatjs/intl-unified-numberformat): add currencyDisplay… #332

Merged
merged 1 commit into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions packages/formatjs-extract-cldr-data/src/extract-currencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as Currencies from 'cldr-numbers-full/main/en/currencies.json';
import {Locale} from './types';
import generateFieldExtractorFn from './utils';
import {sync as globSync} from 'glob';
import {resolve, dirname} from 'path';
import {CurrencyData} from '@formatjs/intl-utils';
import {mapValues} from 'lodash';

export type Currencies = typeof Currencies['main']['en']['numbers']['currencies'];

export function getAllLocales() {
return globSync('*/currencies.json', {
cwd: resolve(
dirname(require.resolve('cldr-numbers-full/package.json')),
'./main'
),
}).map(dirname);
}

const currenciesLocales = getAllLocales();

function loadCurrencies(locale: Locale): Record<string, CurrencyData> {
const jsonData = require(`cldr-numbers-full/main/${locale}/currencies.json`) as typeof Currencies;
return mapValues<
Record<
string,
{
displayName: string;
'displayName-count-one'?: string;
'displayName-count-other': string;
symbol: string;
'symbol-alt-narrow'?: string;
'symbol-alt-variant'?: string;
}
>,
CurrencyData
>(jsonData.main[locale as 'en'].numbers.currencies, currencyData => {
return {
displayName: {
one: currencyData['displayName-count-one'],
other:
currencyData['displayName-count-other'] || currencyData.displayName,
},
symbol: currencyData.symbol,
narrowSymbol: currencyData['symbol-alt-narrow'],
variantSymbol: currencyData['symbol-alt-variant'],
};
});
}

function hasCurrencies(locale: Locale): boolean {
return currenciesLocales.includes(locale);
}

export default generateFieldExtractorFn<Record<string, CurrencyData>>(
loadCurrencies,
hasCurrencies,
getAllLocales()
);
14 changes: 12 additions & 2 deletions packages/formatjs-extract-cldr-data/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import extractRelativeFields, {
import extractUnits, {
getAllLocales as getAllUnitsLocales,
} from './extract-units';
import extracListPatterns, {
import extractListPatterns, {
getAllLocales as getAllListLocales,
} from './extract-list';
import extractCurrencies, {
getAllLocales as getAllCurrenciesLocales,
} from './extract-currencies';

export interface Opts {
locales?: string[];
Expand All @@ -32,7 +35,13 @@ export function extractAllUnits(options: Opts = {}) {
export function extractAllListPatterns(options: Opts = {}) {
// Default to all CLDR locales if none have been provided.
const locales = options.locales || getAllListLocales();
return extracListPatterns(locales);
return extractListPatterns(locales);
}

export function extractAllCurrencies(options: Opts = {}) {
// Default to all CLDR locales if none have been provided.
const locales = options.locales || getAllCurrenciesLocales();
return extractCurrencies(locales);
}

export {getAllLanguages} from './locales';
Expand All @@ -41,3 +50,4 @@ export const processAliases = process;
export {getAllLocales as getAllDateFieldsLocales} from './extract-relative';
export {getAllLocales as getAllListLocales} from './extract-list';
export {getAllLocales as getAllUnitsLocales} from './extract-units';
export {getAllLocales as getAllCurrenciesLocales} from './extract-currencies';
174 changes: 0 additions & 174 deletions packages/intl-unified-numberformat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading