Skip to content

Commit

Permalink
fix(@formatjs/intl-locale): fix minimize
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed May 19, 2020
1 parent 2c21dcb commit 5ee8909
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
79 changes: 42 additions & 37 deletions packages/intl-locale/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ function applyUnicodeExtensionToTag(
return result;
}

const UND_LOCALE_ID = parseUnicodeLocaleId(likelySubtags.und);

function addLikelySubtags(unicodeLangId: UnicodeLanguageId): UnicodeLanguageId {
const {lang, script, region} = unicodeLangId;
const match =
Expand All @@ -163,7 +161,7 @@ function addLikelySubtags(unicodeLangId: UnicodeLanguageId): UnicodeLanguageId {
likelySubtags[lang as 'aa'] ||
likelySubtags[printLanguageId({lang: 'und', script}) as 'aa'];
if (!match) {
return UND_LOCALE_ID.lang;
throw new Error(`No match for addLikelySubtags`);
}
const parts = match.split('-');
return {
Expand All @@ -174,6 +172,26 @@ function addLikelySubtags(unicodeLangId: UnicodeLanguageId): UnicodeLanguageId {
};
}

function removeLikelySubtags(
unicodeLangId: UnicodeLanguageId
): UnicodeLanguageId {
const {variants, ...max} = addLikelySubtags(unicodeLangId);
const trials: UnicodeLanguageId[] = [
{lang: unicodeLangId.lang},
{lang: unicodeLangId.lang, region: unicodeLangId.region},
{lang: unicodeLangId.lang, script: unicodeLangId.script},
];
for (const trial of trials) {
if (isLanguageEqualWithoutVariants(max, addLikelySubtags(trial))) {
return {
...trial,
variants,
};
}
}
return {...max, variants};
}

function isLanguageEqualWithoutVariants(
l1: UnicodeLanguageId,
l2: UnicodeLanguageId
Expand Down Expand Up @@ -322,48 +340,35 @@ export class IntlLocale {
*/
public maximize(): IntlLocale {
const ast = getInternalSlot(__INTERNAL_SLOT_MAP__, this, 'ast');
const maximizedLang = addLikelySubtags(ast.lang);
return new IntlLocale(
printAST({
...ast,
lang: maximizedLang,
})
);
try {
const maximizedLang = addLikelySubtags(ast.lang);
return new IntlLocale(
printAST({
...ast,
lang: maximizedLang,
})
);
} catch (e) {
return new IntlLocale(printAST(ast));
}
}

/**
* https://www.unicode.org/reports/tr35/#Likely_Subtags
*/
public minimize(): IntlLocale {
const ast = getInternalSlot(__INTERNAL_SLOT_MAP__, this, 'ast');
const {variants, ...max} = addLikelySubtags(ast.lang);
const trials: UnicodeLanguageId[] = [
{lang: max.lang},
{lang: max.lang, region: max.region},
{lang: max.lang, script: max.script},
];
for (const trial of trials) {
if (isLanguageEqualWithoutVariants(max, addLikelySubtags(trial))) {
return new IntlLocale(
printAST({
...ast,
lang: {
...trial,
variants,
},
})
);
}
try {
const minimalLang = removeLikelySubtags(ast.lang);
return new IntlLocale(
printAST({
...ast,
lang: minimalLang,
})
);
} catch (e) {
return new IntlLocale(printAST(ast));
}
return new IntlLocale(
printAST({
...ast,
lang: {
...max,
variants,
},
})
);
}

public toString() {
Expand Down
27 changes: 14 additions & 13 deletions packages/intl-locale/tests/minimize.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import {IntlLocale} from '../src';
// This is different from test262 bc of https://github.com/tc39/test262/issues/2628
const testDataMinimal = {
// Undefined primary language.
// und: 'en',
// 'und-Thai': 'th',
// 'und-419': 'es-419',
// 'und-150': 'ru',
// 'und-AT': 'de-AT',
und: 'und',
'und-Thai': 'und-Thai',
'und-419': 'und-419',
'und-150': 'und-150',
'und-AT': 'und-AT',

// // https://unicode-org.atlassian.net/browse/ICU-13786
// 'aae-Latn-IT': 'aae-Latn-IT',
// 'aae-Thai-CO': 'aae-Thai-CO',
// https://unicode-org.atlassian.net/browse/ICU-13786
'aae-Latn-IT': 'aae-Latn-IT',
'aae-Thai-CO': 'aae-Thai-CO',

// // https://unicode-org.atlassian.net/browse/ICU-10220
// // https://unicode-org.atlassian.net/browse/ICU-12345
// 'und-CW': 'pap-CW',
// 'und-US': 'en',
// 'zh-Hant': 'zh-TW',
// https://unicode-org.atlassian.net/browse/ICU-10220
// https://unicode-org.atlassian.net/browse/ICU-12345
'und-CW': 'und-CW',
'und-US': 'und',
'zh-Hant': 'zh-Hant',
'zh-Hani': 'zh-Hani',
};

Expand Down

0 comments on commit 5ee8909

Please sign in to comment.