Skip to content

Commit

Permalink
feat(@formatjs/intl-unified-numberformat): add currencyDisplay… (#332)
Browse files Browse the repository at this point in the history
add support for `currencyDisplay`: `narrowSymbol`

polyfill for tc39/ecma402#200
  • Loading branch information
pyrocat101 authored and longlho committed Apr 26, 2020
1 parent 62b8af6 commit ad4f8bc
Show file tree
Hide file tree
Showing 9 changed files with 1,156 additions and 237 deletions.
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/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/formatjs/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

0 comments on commit ad4f8bc

Please sign in to comment.