Skip to content

Commit

Permalink
feat(@formatjs/intl-utils): Remove aliases and parentLocales
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed May 24, 2020
1 parent 1aea676 commit e0e9370
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 123 deletions.
3 changes: 1 addition & 2 deletions packages/intl-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
],
"scripts": {
"clean": "rimraf dist dist-es6 lib *.tsbuildinfo",
"cldr": "ts-node scripts/cldr",
"build": "yarn run cldr && tsc && tsc -p tsconfig.esm.json && tsc -p tsconfig.es6.json && api-extractor run --local",
"build": "tsc && tsc -p tsconfig.esm.json && tsc -p tsconfig.es6.json && api-extractor run --local",
"test": "jest"
},
"homepage": "https://github.com/formatjs/formatjs",
Expand Down
45 changes: 0 additions & 45 deletions packages/intl-utils/scripts/cldr.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/intl-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
export {selectUnit} from './diff';
export {
defaultNumberOption,
getAliasesByLang,
getInternalSlot,
getMultiInternalSlots,
getNumberOption,
getOption,
getParentLocalesByLang,
isLiteralPart,
LiteralPart,
partitionPattern,
Expand Down
23 changes: 0 additions & 23 deletions packages/intl-utils/src/polyfill-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import aliases from './aliases';
import parentLocales from './parentLocales';
import {invariant} from './invariant';
import {
NumberFormatDigitInternalSlots,
Expand Down Expand Up @@ -115,27 +113,6 @@ export function getNumberOption<T extends object, K extends keyof T>(
return defaultNumberOption(val, minimum, maximum, fallback);
}

export function getAliasesByLang(lang: string): Record<string, string> {
return Object.keys(aliases).reduce((all: Record<string, string>, locale) => {
if (locale.split('-')[0] === lang) {
all[locale] = aliases[locale as 'zh-CN'];
}
return all;
}, {});
}

export function getParentLocalesByLang(lang: string): Record<string, string> {
return Object.keys(parentLocales).reduce(
(all: Record<string, string>, locale) => {
if (locale.split('-')[0] === lang) {
all[locale] = parentLocales[locale as 'en-150'];
}
return all;
},
{}
);
}

export function setInternalSlot<
Instance extends object,
Internal extends object,
Expand Down
20 changes: 2 additions & 18 deletions packages/intl-utils/src/resolve-locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,8 @@ function createBestFitMatcher(getDefaultLocale: () => string) {
};
}

export function getLocaleHierarchy(
locale: string,
aliases: Record<string, string>,
parentLocales: Record<string, string>
): string[] {
export function getLocaleHierarchy(locale: string): string[] {
const results = [locale];
if (aliases[locale as 'zh-CN']) {
locale = aliases[locale as 'zh-CN'];
results.push(locale);
}
const parentLocale = parentLocales[locale as 'en-150'];
if (parentLocale) {
results.push(parentLocale);
}
const localeParts = locale.split('-');
for (let i = localeParts.length; i > 1; i--) {
results.push(localeParts.slice(0, i - 1).join('-'));
Expand Down Expand Up @@ -320,11 +308,7 @@ export function unpackData<T extends Record<string, any>>(
/** By default shallow merge the dictionaries. */
reducer: (all: T, d: T) => T = (all, d) => ({...all, ...d})
): T {
const localeHierarchy = getLocaleHierarchy(
locale,
localeData.aliases,
localeData.parentLocales
);
const localeHierarchy = getLocaleHierarchy(locale);
const dataToMerge = localeHierarchy
.map(l => localeData.data[l])
.filter(Boolean);
Expand Down
2 changes: 0 additions & 2 deletions packages/intl-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export type Locale = string;
export interface LocaleData<T> {
data: Record<Locale, T>;
aliases: Record<string, string>;
availableLocales: string[];
parentLocales: Record<string, string>;
}
47 changes: 16 additions & 31 deletions packages/intl-utils/tests/resolve-locale.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
import {
getLocaleHierarchy,
getAliasesByLang,
getParentLocalesByLang,
unpackData,
} from '../src';
import {getLocaleHierarchy, unpackData} from '../src';

describe('resolve-locale', function () {
it('should handle zh-TW', function () {
expect(
getLocaleHierarchy(
'zh-TW',
getAliasesByLang('zh'),
getParentLocalesByLang('zh')
)
).toEqual(['zh-TW', 'zh-Hant-TW', 'zh-Hant', 'zh']);
expect(getLocaleHierarchy('zh-Hant-TW')).toEqual([
'zh-Hant-TW',
'zh-Hant',
'zh',
]);
});
it('should handle zh-CN', function () {
expect(
getLocaleHierarchy(
'zh-CN',
getAliasesByLang('zh'),
getParentLocalesByLang('zh')
)
).toEqual(['zh-CN', 'zh-Hans-CN', 'zh-Hans', 'zh']);
expect(getLocaleHierarchy('zh-Hans-CN')).toEqual([
'zh-Hans-CN',
'zh-Hans',
'zh',
]);
});
it('should handle zh-MO', function () {
expect(
getLocaleHierarchy(
'zh-MO',
getAliasesByLang('zh'),
getParentLocalesByLang('zh')
)
).toEqual(['zh-MO', 'zh-Hant-MO', 'zh-Hant-HK', 'zh-Hant', 'zh']);
expect(getLocaleHierarchy('zh-Hant-MO')).toEqual([
'zh-Hant-MO',
'zh-Hant',
'zh',
]);
});
it('unpackData', function () {
expect(
Expand Down Expand Up @@ -59,11 +48,7 @@ describe('resolve-locale', function () {
},
},
},
aliases: {},
availableLocales: ['en-US', 'en-AI', 'en'],
parentLocales: {
'en-US': 'en',
},
})
).toEqual({
currencies: {
Expand Down

0 comments on commit e0e9370

Please sign in to comment.