Skip to content

Commit

Permalink
fix: flags ordering for china
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Apr 16, 2024
1 parent caa5e31 commit 6709555
Show file tree
Hide file tree
Showing 8 changed files with 537 additions and 278 deletions.
457 changes: 352 additions & 105 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@
"cldr-core": "^39.0.0",
"country-flag-emoji": "^1.0.3",
"download-git-repo": "^1.1.0",
"eslint": "^7.28.0",
"eslint-plugin-jest": "^24.3.6",
"eslint-plugin-prettier": "^3.4.0",
"husky": "^6.0.0",
"jest": "^27.0.4",
"prettier": "^2.8.8",
"pretty-quick": "^3.1.0",
"semantic-release": "^17.4.3",
"ts-jest": "^27.0.3",
"ts-node": "^10.0.0",
"typescript": "^4.3.2",
"prettier": "^2.3.1",
"eslint": "^7.28.0"
"typescript": "^4.3.2"
},
"dependencies": {
"latinize": "^0.5.0",
"@formatjs/intl-getcanonicallocales": "^1.7.0"
"@formatjs/intl-getcanonicallocales": "^1.7.0",
"latinize": "^0.5.0"
},
"repository": {
"type": "git",
Expand Down
10 changes: 9 additions & 1 deletion scripts/generateFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { languageInfo } from '../src/generated/languageInfo';
import * as fs from 'fs';
import path from 'path';
import { symbolToHex } from '../src/flags';
import { format } from 'prettier';

const DOWNLOAD_PATH = 'tmp/twemoji';
const ADDITIONAL_FLAGS = ['🏁', '🏳️'];
Expand Down Expand Up @@ -46,7 +47,14 @@ const generate = (err: string) => {

const content =
'export const supportedFlags = ' + JSON.stringify([...flagsEmoji]);
fs.writeFileSync(path.resolve('src/generated/supportedFlags.ts'), content);
const formattedContent = format(content, {
parser: 'typescript',
singleQuote: true,
});
fs.writeFileSync(
path.resolve('src/generated/supportedFlags.ts'),
formattedContent
);
}
};

Expand Down
11 changes: 10 additions & 1 deletion scripts/generateLanguageInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import * as fs from 'fs';
import * as path from 'path';
import { getLanguageInfoForAvailableLanguages } from './languageInfo/languageInfo';
import { format } from 'prettier';

const data = getLanguageInfoForAvailableLanguages();
const content = 'export const languageInfo = ' + JSON.stringify(data);

fs.writeFileSync(path.resolve('src/generated/languageInfo.ts'), content);
const formattedContent = format(content, {
parser: 'typescript',
singleQuote: true,
});

fs.writeFileSync(
path.resolve('src/generated/languageInfo.ts'),
formattedContent
);
48 changes: 19 additions & 29 deletions scripts/languageInfo/languageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,19 @@ export const getLanguageRegions = (locale: string): string[] => {
return [];
};

const OFFICIALITY_ORDER = [
undefined,
'official_regional',
'de_facto_official',
'official',
] as const;

type Officiality = (typeof OFFICIALITY_ORDER)[number];

function compareOfficiality(a: Officiality, b: Officiality) {
return OFFICIALITY_ORDER.indexOf(b) - OFFICIALITY_ORDER.indexOf(a);
}

const sortTerritoriesByLanguage = (
territories: (keyof typeof territoryInfoData.supplemental.territoryInfo)[],
language: string
Expand All @@ -714,36 +727,13 @@ const sortTerritoriesByLanguage = (
territories.sort((a, b) => {
const aOfficialStatus = territoriesLanguagePopulation[a]?._officialStatus;
const bOfficialStatus = territoriesLanguagePopulation[b]?._officialStatus;
if (
(aOfficialStatus == 'official' ||
aOfficialStatus == 'de_facto_official') &&
bOfficialStatus != 'official' &&
bOfficialStatus !== 'de_facto_official'
) {
return -1;
}

if (
(bOfficialStatus == 'official' ||
bOfficialStatus == 'de_facto_official') &&
aOfficialStatus != 'official' &&
aOfficialStatus !== 'de_facto_official'
) {
return -1;
}

if (
aOfficialStatus == 'official_regional' &&
bOfficialStatus != 'official_regional'
) {
return -1;
}

if (
bOfficialStatus == 'official_regional' &&
aOfficialStatus != 'official_regional'
) {
return 1;
const statusComparison = compareOfficiality(
aOfficialStatus,
bOfficialStatus
);
if (statusComparison !== 0) {
return statusComparison;
}

const aPercent = territoriesLanguagePopulation[a]?._populationPercent;
Expand Down
5 changes: 5 additions & 0 deletions src/generated/languageInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,10 @@ describe('generated info', () => {
expect(territories.length).toEqual(45);
expect(territories[0]).toEqual('FR');
});

test('returns territories for chinese', () => {
const territories = languageInfo['zh'].regions;
expect(territories[0]).toEqual('CN');
});
});
});

0 comments on commit 6709555

Please sign in to comment.