Skip to content

Commit

Permalink
[chore]: add flags country list
Browse files Browse the repository at this point in the history
  • Loading branch information
RgnDunes committed May 27, 2024
1 parent 421fbe2 commit 95b10ac
Show file tree
Hide file tree
Showing 5 changed files with 539 additions and 14 deletions.
262 changes: 262 additions & 0 deletions packages/i18nify-js/src/modules/geo/data/countryListForAllFlags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
import { CountryCodeTypeForAllFlags } from '../../types';

export const COUNTRY_LIST_FOR_ALL_FLAGS: CountryCodeTypeForAllFlags[] = [
'AD',
'AE',
'AF',
'AG',
'AI',
'AL',
'AM',
'AO',
'AQ',
'AR',
'AS',
'AT',
'AU',
'AW',
'AX',
'AZ',
'BA',
'BB',
'BD',
'BE',
'BF',
'BG',
'BH',
'BI',
'BJ',
'BL',
'BM',
'BN',
'BO',
'BQ-BO',
'BQ-SA',
'BQ-SE',
'BQ',
'BR',
'BS',
'BT',
'BV',
'BW',
'BY',
'BZ',
'CA',
'CC',
'CD',
'CF',
'CG',
'CH',
'CI',
'CK',
'CL',
'CM',
'CN',
'CO',
'CR',
'CU',
'CV',
'CW',
'CX',
'CY',
'CZ',
'DE',
'DJ',
'DK',
'DM',
'DO',
'DZ',
'EC',
'EE',
'EG',
'EH',
'ER',
'ES',
'ET',
'FI',
'FJ',
'FK',
'FM',
'FO',
'FR',
'GA',
'GB-ENG',
'GB-NIR',
'GB-SCT',
'GB-UKM',
'GB-WLS',
'GB',
'GD',
'GE',
'GF',
'GG',
'GH',
'GI',
'GL',
'GM',
'GN',
'GP',
'GQ',
'GR',
'GS',
'GT',
'GU',
'GW',
'GY',
'HK',
'HM',
'HN',
'HR',
'HT',
'HU',
'ID',
'IE',
'IL',
'IM',
'IN',
'IO',
'IQ',
'IR',
'IS',
'IT',
'JE',
'JM',
'JO',
'JP',
'KE',
'KG',
'KH',
'KI',
'KM',
'KN',
'KP',
'KR',
'KW',
'KY',
'KZ',
'LA',
'LB',
'LC',
'LI',
'LK',
'LR',
'LS',
'LT',
'LU',
'LV',
'LY',
'MA',
'MC',
'MD',
'ME',
'MF',
'MG',
'MH',
'MK',
'ML',
'MM',
'MN',
'MO',
'MP',
'MQ',
'MR',
'MS',
'MT',
'MU',
'MV',
'MW',
'MX',
'MY',
'MZ',
'NA',
'NC',
'NE',
'NF',
'NG',
'NI',
'NL',
'NO',
'NP',
'NR',
'NU',
'NZ',
'OM',
'PA',
'PE',
'PF',
'PG',
'PH',
'PK',
'PL',
'PM',
'PN',
'PR',
'PS',
'PT',
'PW',
'PY',
'QA',
'RE',
'RO',
'RS',
'RU',
'RW',
'SA',
'SB',
'SC',
'SD',
'SE',
'SG',
'SH',
'SI',
'SJ',
'SK',
'SL',
'SM',
'SN',
'SO',
'SR',
'SS',
'ST',
'SV',
'SX',
'SY',
'SZ',
'TC',
'TD',
'TF',
'TG',
'TH',
'TJ',
'TK',
'TL',
'TM',
'TN',
'TO',
'TR',
'TT',
'TV',
'TW',
'TZ',
'UA',
'UG',
'UM',
'US',
'UY',
'UZ',
'VA',
'VC',
'VE',
'VG',
'VI',
'VN',
'VU',
'WF',
'WS',
'YE',
'YT-UNF',
'YT',
'ZA',
'ZM',
'ZW',
];
12 changes: 7 additions & 5 deletions packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CountryCodeType, GetFlagReturnType } from '../types';
import { CountryCodeTypeForAllFlags, GetFlagReturnType } from '../types';
import { withErrorBoundary } from '../../common/errorBoundary';
import { FLAG_4X3_BASE_PATH, FLAG_BASE_PATH } from './constants';
import { isCountryValid } from './utils';
import { isCountryValidForFlags } from './utils';

/**
* Retrieves the URL for the flag of a given country.
Expand All @@ -15,12 +15,14 @@ import { isCountryValid } from './utils';
* @returns The URL of the flag image for the given country code.
* @throws {Error} If the country code is not in the list of valid country codes.
*/
const getFlagOfCountry = (_countryCode: CountryCodeType): GetFlagReturnType => {
if (!isCountryValid(_countryCode)) {
const getFlagOfCountry = (
_countryCode: CountryCodeTypeForAllFlags,
): GetFlagReturnType => {
if (!isCountryValidForFlags(_countryCode)) {
throw new Error(`Invalid country code: ${_countryCode}`);
}

const countryCode = _countryCode.toLowerCase() as CountryCodeType;
const countryCode = _countryCode.toLowerCase() as CountryCodeTypeForAllFlags;
return {
original: `${FLAG_BASE_PATH}/${countryCode}.svg`,
'4X3': `${FLAG_4X3_BASE_PATH}/${countryCode}.svg`,
Expand Down
13 changes: 7 additions & 6 deletions packages/i18nify-js/src/modules/geo/getFlagsForAllCountries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { withErrorBoundary } from '../../common/errorBoundary';
import { LIST_OF_ALL_COUNTRIES } from './data/listOfAllCountries';
import { COUNTRY_LIST_FOR_ALL_FLAGS } from './data/countryListForAllFlags';
import { FLAG_4X3_BASE_PATH, FLAG_BASE_PATH } from './constants';
import { CountryCodeType, GetFlagReturnType } from '../types';
import { GetFlagReturnType, CountryCodeTypeForAllFlags } from '../types';

/**
* Retrieves a mapping of country codes to their corresponding flag image URLs.
Expand All @@ -13,16 +13,17 @@ import { CountryCodeType, GetFlagReturnType } from '../types';
* @returns An object mapping each country code from the list to its flag image URL.
*/
const getFlagsForAllCountries = (): {
[countryCode in CountryCodeType]: GetFlagReturnType;
[countryCode in CountryCodeTypeForAllFlags]: GetFlagReturnType;
} => {
// Initialize an empty object to hold the country code to flag URL mapping
const flagsForAllCountriesMap = {} as {
[countryCode in CountryCodeType]: GetFlagReturnType;
[countryCode in CountryCodeTypeForAllFlags]: GetFlagReturnType;
};

// Loop through each country code in the list
LIST_OF_ALL_COUNTRIES.map((countryCode: CountryCodeType) => {
let lowerCasedCountryCode = countryCode.toLowerCase() as CountryCodeType;
COUNTRY_LIST_FOR_ALL_FLAGS.map((countryCode: CountryCodeTypeForAllFlags) => {
let lowerCasedCountryCode =
countryCode.toLowerCase() as CountryCodeTypeForAllFlags;
// Construct the flag image URL and assign it to the corresponding country code in the map
flagsForAllCountriesMap[countryCode] = {
original: `${FLAG_BASE_PATH}/${lowerCasedCountryCode}.svg`,
Expand Down
6 changes: 3 additions & 3 deletions packages/i18nify-js/src/modules/geo/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LIST_OF_ALL_COUNTRIES } from './data/listOfAllCountries';
import { COUNTRY_LIST_FOR_ALL_FLAGS } from './data/countryListForAllFlags';

export function isCountryValid(_countryCode: string): boolean {
export function isCountryValidForFlags(_countryCode: string): boolean {
const countryCode = _countryCode.toUpperCase();
// @ts-expect-error countryCode here can be a random string
return LIST_OF_ALL_COUNTRIES.includes(countryCode);
return COUNTRY_LIST_FOR_ALL_FLAGS.includes(countryCode);
}
Loading

0 comments on commit 95b10ac

Please sign in to comment.