From a9b8a3b5d78e54a531af0f7a64b24a80fe4f5b6c Mon Sep 17 00:00:00 2001 From: Jamie Lynch Date: Mon, 28 Jun 2021 17:04:00 +0100 Subject: [PATCH] feat: migrate countries from types (#212) * Rename types to @types * migrate countries from types * chore(docs): update TOC * add test for countries enum testing the existence of a single pair is enough, so we test against where The Guardian has offices * don't export Country as type * rename Country to CountriesByCountryCode * provide a better example usage of countries * change countries to be object * remove erroneous function call * rename some country keys and fix order * don't export CountryKey * update countries docs * use migrated types * update docs * chore(docs): update TOC * nothing Co-authored-by: jamie-lynch Co-authored-by: Max Duval Co-authored-by: Alex Sanders Co-authored-by: sndrs --- README.md | 5 + package.json | 4 - src/@types/countries.ts | 501 +++++++++++++++++++ src/countries.README.md | 64 +++ src/countries.ts | 1010 +++++++++++++++++++++++++++++++++++++++ src/coutries.test.ts | 22 + src/getLocale.ts | 2 +- src/index.ts | 3 + 8 files changed, 1606 insertions(+), 5 deletions(-) create mode 100644 src/@types/countries.ts create mode 100644 src/countries.README.md create mode 100644 src/countries.ts create mode 100644 src/coutries.test.ts diff --git a/README.md b/README.md index 4866313e..882b642e 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ - [Modules](#modules) - [`cookies`](#cookies) + - [Countries](#countries) - [`getLocale`](#getlocale) - [`getSwitches`](#getswitches) - [`isBoolean`](#isboolean) @@ -39,6 +40,10 @@ The following modules are available: API over `document.cookies`. +### [Countries](./src/countries.README.md) + +Country data and methods to access it. + ### [`getLocale`](./src/getLocale.README.md) Get the user’s current location. diff --git a/package.json b/package.json index 9bb1257c..adac5e45 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "@guardian/eslint-config-typescript": "0.6.0", "@guardian/pkgu": "0.6.0", "@guardian/prettier": "0.6.0", - "@guardian/types": "^6.1.0", "@rollup/plugin-typescript": "8.2.1", "@semantic-release/github": "7.2.3", "@types/jest": "26.0.23", @@ -57,9 +56,6 @@ "typescript": "4.3.4", "wcag-contrast": "^3.0.0" }, - "peerDependencies": { - "@guardian/types": "^6.1.0" - }, "publishConfig": { "access": "public" } diff --git a/src/@types/countries.ts b/src/@types/countries.ts new file mode 100644 index 00000000..3ec95e37 --- /dev/null +++ b/src/@types/countries.ts @@ -0,0 +1,501 @@ +// https://en.wikipedia.org/wiki/ISO_3166-1 +export type CountryCode = + | 'AD' + | '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' + | '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' + | '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' + | 'VI' + | 'VN' + | 'VU' + | 'WF' + | 'WS' + | 'YE' + | 'YT' + | 'ZA' + | 'ZM' + | 'ZW'; + +export type CountryKey = + | 'afghanistan' + | 'åland_islands' + | 'albania' + | 'algeria' + | 'american_samoa' + | 'andorra' + | 'angola' + | 'anguilla' + | 'antarctica' + | 'antigua_and_barbuda' + | 'argentina' + | 'armenia' + | 'aruba' + | 'australia' + | 'austria' + | 'azerbaijan' + | 'bahamas' + | 'bahrain' + | 'bangladesh' + | 'barbados' + | 'belarus' + | 'belgium' + | 'belize' + | 'benin' + | 'bermuda' + | 'bhutan' + | 'bolivia' + | 'bosnia_and_herzegovina' + | 'botswana' + | 'bouvet_island' + | 'brazil' + | 'british_indian_ocean_territory' + | 'brunei_darussalam' + | 'bulgaria' + | 'burkina_faso' + | 'burundi' + | 'cabo_verde' + | 'cambodia' + | 'cameroon' + | 'canada' + | 'caribbean_netherlands' + | 'cayman_islands' + | 'central_african_republic' + | 'chad' + | 'chile' + | 'china' + | 'christmas_island' + | 'cocos_keeling_islands' + | 'colombia' + | 'comoros' + | 'congo' + | 'cook_islands' + | 'costa_rica' + | 'croatia' + | 'cuba' + | 'curaçao' + | 'cyprus' + | 'czechia' + | 'democratic_republic_of_the_congo' + | 'denmark' + | 'djibouti' + | 'dominica' + | 'dominican_republic' + | 'ecuador' + | 'egypt' + | 'el_salvador' + | 'equatorial_guinea' + | 'eritrea' + | 'estonia' + | 'eswatini' + | 'ethiopia' + | 'falkland_islands' + | 'faroe_islands' + | 'federated_states_of_micronesia' + | 'fiji' + | 'finland' + | 'france' + | 'french_guiana' + | 'french_polynesia' + | 'french_southern_territories' + | 'gabon' + | 'gambia' + | 'georgia' + | 'germany' + | 'ghana' + | 'gibraltar' + | 'greece' + | 'greenland' + | 'grenada' + | 'guadeloupe' + | 'guam' + | 'guatemala' + | 'guernsey' + | 'guinea' + | 'guinea_bissau' + | 'guyana' + | 'haiti' + | 'heard_island_and_mcdonald_islands' + | 'holy_see' + | 'honduras' + | 'hong_kong' + | 'hungary' + | 'iceland' + | 'india' + | 'indonesia' + | 'iran' + | 'iraq' + | 'ireland' + | 'isle_of_man' + | 'israel' + | 'italy' + | 'ivory_coast' + | 'jamaica' + | 'japan' + | 'jersey' + | 'jordan' + | 'kazakhstan' + | 'kenya' + | 'kiribati' + | 'kuwait' + | 'kyrgyzstan' + | 'laos' + | 'latvia' + | 'lebanon' + | 'lesotho' + | 'liberia' + | 'libya' + | 'liechtenstein' + | 'lithuania' + | 'luxembourg' + | 'macao' + | 'madagascar' + | 'malawi' + | 'malaysia' + | 'maldives' + | 'mali' + | 'malta' + | 'marshall_islands' + | 'martinique' + | 'mauritania' + | 'mauritius' + | 'mayotte' + | 'mexico' + | 'moldova' + | 'monaco' + | 'mongolia' + | 'montenegro' + | 'montserrat' + | 'morocco' + | 'mozambique' + | 'myanmar' + | 'namibia' + | 'nauru' + | 'nepal' + | 'netherlands' + | 'new_caledonia' + | 'new_zealand' + | 'nicaragua' + | 'niger' + | 'nigeria' + | 'niue' + | 'norfolk_island' + | 'north_korea' + | 'north_macedonia' + | 'northern_mariana_islands' + | 'norway' + | 'oman' + | 'pakistan' + | 'palau' + | 'panama' + | 'papua_new_guinea' + | 'paraguay' + | 'peru' + | 'philippines' + | 'pitcairn' + | 'poland' + | 'portugal' + | 'puerto_rico' + | 'qatar' + | 'romania' + | 'russia' + | 'rwanda' + | 'réunion' + | 'saint_barthélemy' + | 'saint_helena_ascension_and_tristan_da_cunha' + | 'saint_kitts_and_nevis' + | 'saint_lucia' + | 'saint_martin' + | 'saint_pierre_and_miquelon' + | 'saint_vincent_and_the_grenadines' + | 'samoa' + | 'san_marino' + | 'sao_tome_and_principe' + | 'saudi_arabia' + | 'senegal' + | 'serbia' + | 'seychelles' + | 'sierra_leone' + | 'singapore' + | 'sint_maarten' + | 'slovakia' + | 'slovenia' + | 'solomon_islands' + | 'somalia' + | 'south_africa' + | 'south_georgia_and_the_south_sandwich_islands' + | 'south_korea' + | 'south_sudan' + | 'spain' + | 'sri_lanka' + | 'state_of_palestine' + | 'sudan' + | 'suriname' + | 'svalbard_and_jan_mayen' + | 'sweden' + | 'switzerland' + | 'syria' + | 'taiwan' + | 'tajikistan' + | 'tanzania' + | 'thailand' + | 'timor_leste' + | 'togo' + | 'tokelau' + | 'tonga' + | 'trinidad_and_tobago' + | 'tunisia' + | 'turkey' + | 'turkmenistan' + | 'turks_and_caicos_islands' + | 'tuvalu' + | 'uganda' + | 'ukraine' + | 'united_arab_emirates' + | 'united_kingdom' + | 'united_states_minor_outlying_islands' + | 'united_states_of_america' + | 'uruguay' + | 'uzbekistan' + | 'vanuatu' + | 'venezuela' + | 'vietnam' + | 'virgin_islands' + | 'wallis_and_futuna' + | 'western_sahara' + | 'yemen' + | 'zambia' + | 'zimbabwe'; diff --git a/src/countries.README.md b/src/countries.README.md new file mode 100644 index 00000000..e7681680 --- /dev/null +++ b/src/countries.README.md @@ -0,0 +1,64 @@ +# Countries + +Country data and methods to access it. + +## Usage + +```js +import { countries, getCountryByCountryCode } from '@guardian/libs'; + +const countryA = countries.afghanistan; +// { countryCode: 'AF', name: 'Afghanistan' } + +const countryB = getCountryByCountryCode('AX'); +// { countryCode: 'AX', name: 'Åland Islands' } +``` + +## `countries` + +Type: `Record` + +A config object of country metadata. + +```typescript +{ + afghanistan: { + countryCode: 'AF', + name: 'Afghanistan', + }, + åland_islands: { + countryCode: 'AX', + name: 'Åland Islands', + }, + // etc +} +``` + +## `getCountryByCountryCode(countryCode)` + +Returns: `Country` + +Gets a country config object for the country with the passed country code. + +### `countryCode` + +Type: `CountryCode` + +## Types + +### `Country` + +```typescript +type Country = { + countryCode: CountryCode; + name: string; +}; +``` + +### `CountryCode` + +ISO 3166-1 alpha-2 two-letter country code. See [the type definition](./src/@types/countries.ts) for the full list. + +```typescript +type CountryCode = 'AF' | 'AX' /* etc */; +``` diff --git a/src/countries.ts b/src/countries.ts new file mode 100644 index 00000000..c18d4bf1 --- /dev/null +++ b/src/countries.ts @@ -0,0 +1,1010 @@ +import type { CountryCode, CountryKey } from './@types/countries'; + +export type Country = { + countryCode: CountryCode; + name: string; +}; + +export const countries: Record = { + afghanistan: { + countryCode: 'AF', + name: 'Afghanistan', + }, + åland_islands: { + countryCode: 'AX', + name: 'Åland Islands', + }, + albania: { + countryCode: 'AL', + name: 'Albania', + }, + algeria: { + countryCode: 'DZ', + name: 'Algeria', + }, + american_samoa: { + countryCode: 'AS', + name: 'American Samoa', + }, + andorra: { + countryCode: 'AD', + name: 'Andorra', + }, + angola: { + countryCode: 'AO', + name: 'Angola', + }, + anguilla: { + countryCode: 'AI', + name: 'Anguilla', + }, + antarctica: { + countryCode: 'AQ', + name: 'Antarctica', + }, + antigua_and_barbuda: { + countryCode: 'AG', + name: 'Antigua and Barbuda', + }, + argentina: { + countryCode: 'AR', + name: 'Argentina', + }, + armenia: { + countryCode: 'AM', + name: 'Armenia', + }, + aruba: { + countryCode: 'AW', + name: 'Aruba', + }, + australia: { + countryCode: 'AU', + name: 'Australia', + }, + austria: { + countryCode: 'AT', + name: 'Austria', + }, + azerbaijan: { + countryCode: 'AZ', + name: 'Azerbaijan', + }, + bahamas: { + countryCode: 'BS', + name: 'Bahamas', + }, + bahrain: { + countryCode: 'BH', + name: 'Bahrain', + }, + bangladesh: { + countryCode: 'BD', + name: 'Bangladesh', + }, + barbados: { + countryCode: 'BB', + name: 'Barbados', + }, + belarus: { + countryCode: 'BY', + name: 'Belarus', + }, + belgium: { + countryCode: 'BE', + name: 'Belgium', + }, + belize: { + countryCode: 'BZ', + name: 'Belize', + }, + benin: { + countryCode: 'BJ', + name: 'Benin', + }, + bermuda: { + countryCode: 'BM', + name: 'Bermuda', + }, + bhutan: { + countryCode: 'BT', + name: 'Bhutan', + }, + bolivia: { + countryCode: 'BO', + name: 'Bolivia (Plurinational State of)', + }, + bosnia_and_herzegovina: { + countryCode: 'BA', + name: 'Bosnia and Herzegovina', + }, + botswana: { + countryCode: 'BW', + name: 'Botswana', + }, + bouvet_island: { + countryCode: 'BV', + name: 'Bouvet Island', + }, + brazil: { + countryCode: 'BR', + name: 'Brazil', + }, + british_indian_ocean_territory: { + countryCode: 'IO', + name: 'British Indian Ocean Territory', + }, + brunei_darussalam: { + countryCode: 'BN', + name: 'Brunei Darussalam', + }, + bulgaria: { + countryCode: 'BG', + name: 'Bulgaria', + }, + burkina_faso: { + countryCode: 'BF', + name: 'Burkina Faso', + }, + burundi: { + countryCode: 'BI', + name: 'Burundi', + }, + cabo_verde: { + countryCode: 'CV', + name: 'Cabo Verde', + }, + cambodia: { + countryCode: 'KH', + name: 'Cambodia', + }, + cameroon: { + countryCode: 'CM', + name: 'Cameroon', + }, + canada: { + countryCode: 'CA', + name: 'Canada', + }, + caribbean_netherlands: { + countryCode: 'BQ', + name: 'Bonaire, Sint Eustatius and Saba', + }, + cayman_islands: { + countryCode: 'KY', + name: 'Cayman Islands', + }, + central_african_republic: { + countryCode: 'CF', + name: 'Central African Republic', + }, + chad: { + countryCode: 'TD', + name: 'Chad', + }, + chile: { + countryCode: 'CL', + name: 'Chile', + }, + china: { + countryCode: 'CN', + name: 'China', + }, + christmas_island: { + countryCode: 'CX', + name: 'Christmas Island', + }, + cocos_keeling_islands: { + countryCode: 'CC', + name: 'Cocos (Keeling) Islands', + }, + colombia: { + countryCode: 'CO', + name: 'Colombia', + }, + comoros: { + countryCode: 'KM', + name: 'Comoros', + }, + congo: { + countryCode: 'CG', + name: 'Congo', + }, + cook_islands: { + countryCode: 'CK', + name: 'Cook Islands', + }, + costa_rica: { + countryCode: 'CR', + name: 'Costa Rica', + }, + croatia: { + countryCode: 'HR', + name: 'Croatia', + }, + cuba: { + countryCode: 'CU', + name: 'Cuba', + }, + curaçao: { + countryCode: 'CW', + name: 'Curaçao', + }, + cyprus: { + countryCode: 'CY', + name: 'Cyprus', + }, + czechia: { + countryCode: 'CZ', + name: 'Czechia', + }, + democratic_republic_of_the_congo: { + countryCode: 'CD', + name: 'Democratic Republic of the Congo', + }, + denmark: { + countryCode: 'DK', + name: 'Denmark', + }, + djibouti: { + countryCode: 'DJ', + name: 'Djibouti', + }, + dominica: { + countryCode: 'DM', + name: 'Dominica', + }, + dominican_republic: { + countryCode: 'DO', + name: 'Dominican Republic', + }, + ecuador: { + countryCode: 'EC', + name: 'Ecuador', + }, + egypt: { + countryCode: 'EG', + name: 'Egypt', + }, + el_salvador: { + countryCode: 'SV', + name: 'El Salvador', + }, + equatorial_guinea: { + countryCode: 'GQ', + name: 'Equatorial Guinea', + }, + eritrea: { + countryCode: 'ER', + name: 'Eritrea', + }, + estonia: { + countryCode: 'EE', + name: 'Estonia', + }, + eswatini: { + countryCode: 'SZ', + name: 'Eswatini', + }, + ethiopia: { + countryCode: 'ET', + name: 'Ethiopia', + }, + falkland_islands: { + countryCode: 'FK', + name: 'Falkland Islands (Malvinas)', + }, + faroe_islands: { + countryCode: 'FO', + name: 'Faroe Islands', + }, + federated_states_of_micronesia: { + countryCode: 'FM', + name: 'Federated States of Micronesia', + }, + fiji: { + countryCode: 'FJ', + name: 'Fiji', + }, + finland: { + countryCode: 'FI', + name: 'Finland', + }, + france: { + countryCode: 'FR', + name: 'France', + }, + french_guiana: { + countryCode: 'GF', + name: 'French Guiana', + }, + french_polynesia: { + countryCode: 'PF', + name: 'French Polynesia', + }, + french_southern_territories: { + countryCode: 'TF', + name: 'French Southern Territories', + }, + gabon: { + countryCode: 'GA', + name: 'Gabon', + }, + gambia: { + countryCode: 'GM', + name: 'Gambia', + }, + georgia: { + countryCode: 'GE', + name: 'Georgia', + }, + germany: { + countryCode: 'DE', + name: 'Germany', + }, + ghana: { + countryCode: 'GH', + name: 'Ghana', + }, + gibraltar: { + countryCode: 'GI', + name: 'Gibraltar', + }, + greece: { + countryCode: 'GR', + name: 'Greece', + }, + greenland: { + countryCode: 'GL', + name: 'Greenland', + }, + grenada: { + countryCode: 'GD', + name: 'Grenada', + }, + guadeloupe: { + countryCode: 'GP', + name: 'Guadeloupe', + }, + guam: { + countryCode: 'GU', + name: 'Guam', + }, + guatemala: { + countryCode: 'GT', + name: 'Guatemala', + }, + guernsey: { + countryCode: 'GG', + name: 'Guernsey', + }, + guinea: { + countryCode: 'GN', + name: 'Guinea', + }, + guinea_bissau: { + countryCode: 'GW', + name: 'Guinea-Bissau', + }, + guyana: { + countryCode: 'GY', + name: 'Guyana', + }, + haiti: { + countryCode: 'HT', + name: 'Haiti', + }, + heard_island_and_mcdonald_islands: { + countryCode: 'HM', + name: 'Heard Island and McDonald Islands', + }, + holy_see: { + countryCode: 'VA', + name: 'Holy See', + }, + honduras: { + countryCode: 'HN', + name: 'Honduras', + }, + hong_kong: { + countryCode: 'HK', + name: 'Hong Kong', + }, + hungary: { + countryCode: 'HU', + name: 'Hungary', + }, + iceland: { + countryCode: 'IS', + name: 'Iceland', + }, + india: { + countryCode: 'IN', + name: 'India', + }, + indonesia: { + countryCode: 'ID', + name: 'Indonesia', + }, + iran: { + countryCode: 'IR', + name: 'Iran (Islamic Republic of)', + }, + iraq: { + countryCode: 'IQ', + name: 'Iraq', + }, + ireland: { + countryCode: 'IE', + name: 'Ireland', + }, + isle_of_man: { + countryCode: 'IM', + name: 'Isle of Man', + }, + israel: { + countryCode: 'IL', + name: 'Israel', + }, + italy: { + countryCode: 'IT', + name: 'Italy', + }, + ivory_coast: { + countryCode: 'CI', + name: "Côte d'Ivoire", + }, + jamaica: { + countryCode: 'JM', + name: 'Jamaica', + }, + japan: { + countryCode: 'JP', + name: 'Japan', + }, + jersey: { + countryCode: 'JE', + name: 'Jersey', + }, + jordan: { + countryCode: 'JO', + name: 'Jordan', + }, + kazakhstan: { + countryCode: 'KZ', + name: 'Kazakhstan', + }, + kenya: { + countryCode: 'KE', + name: 'Kenya', + }, + kiribati: { + countryCode: 'KI', + name: 'Kiribati', + }, + kuwait: { + countryCode: 'KW', + name: 'Kuwait', + }, + kyrgyzstan: { + countryCode: 'KG', + name: 'Kyrgyzstan', + }, + laos: { + countryCode: 'LA', + name: "Lao People's Democratic Republic", + }, + latvia: { + countryCode: 'LV', + name: 'Latvia', + }, + lebanon: { + countryCode: 'LB', + name: 'Lebanon', + }, + lesotho: { + countryCode: 'LS', + name: 'Lesotho', + }, + liberia: { + countryCode: 'LR', + name: 'Liberia', + }, + libya: { + countryCode: 'LY', + name: 'Libya', + }, + liechtenstein: { + countryCode: 'LI', + name: 'Liechtenstein', + }, + lithuania: { + countryCode: 'LT', + name: 'Lithuania', + }, + luxembourg: { + countryCode: 'LU', + name: 'Luxembourg', + }, + macao: { + countryCode: 'MO', + name: 'Macao', + }, + madagascar: { + countryCode: 'MG', + name: 'Madagascar', + }, + malawi: { + countryCode: 'MW', + name: 'Malawi', + }, + malaysia: { + countryCode: 'MY', + name: 'Malaysia', + }, + maldives: { + countryCode: 'MV', + name: 'Maldives', + }, + mali: { + countryCode: 'ML', + name: 'Mali', + }, + malta: { + countryCode: 'MT', + name: 'Malta', + }, + marshall_islands: { + countryCode: 'MH', + name: 'Marshall Islands', + }, + martinique: { + countryCode: 'MQ', + name: 'Martinique', + }, + mauritania: { + countryCode: 'MR', + name: 'Mauritania', + }, + mauritius: { + countryCode: 'MU', + name: 'Mauritius', + }, + mayotte: { + countryCode: 'YT', + name: 'Mayotte', + }, + mexico: { + countryCode: 'MX', + name: 'Mexico', + }, + moldova: { + countryCode: 'MD', + name: 'Republic of Moldova', + }, + monaco: { + countryCode: 'MC', + name: 'Monaco', + }, + mongolia: { + countryCode: 'MN', + name: 'Mongolia', + }, + montenegro: { + countryCode: 'ME', + name: 'Montenegro', + }, + montserrat: { + countryCode: 'MS', + name: 'Montserrat', + }, + morocco: { + countryCode: 'MA', + name: 'Morocco', + }, + mozambique: { + countryCode: 'MZ', + name: 'Mozambique', + }, + myanmar: { + countryCode: 'MM', + name: 'Myanmar', + }, + namibia: { + countryCode: 'NA', + name: 'Namibia', + }, + nauru: { + countryCode: 'NR', + name: 'Nauru', + }, + nepal: { + countryCode: 'NP', + name: 'Nepal', + }, + netherlands: { + countryCode: 'NL', + name: 'Netherlands', + }, + new_caledonia: { + countryCode: 'NC', + name: 'New Caledonia', + }, + new_zealand: { + countryCode: 'NZ', + name: 'New Zealand', + }, + nicaragua: { + countryCode: 'NI', + name: 'Nicaragua', + }, + niger: { + countryCode: 'NE', + name: 'Niger', + }, + nigeria: { + countryCode: 'NG', + name: 'Nigeria', + }, + niue: { + countryCode: 'NU', + name: 'Niue', + }, + norfolk_island: { + countryCode: 'NF', + name: 'Norfolk Island', + }, + north_korea: { + countryCode: 'KP', + name: "Democratic People's Republic of Korea", + }, + north_macedonia: { + countryCode: 'MK', + name: 'North Macedonia', + }, + northern_mariana_islands: { + countryCode: 'MP', + name: 'Northern Mariana Islands', + }, + norway: { + countryCode: 'NO', + name: 'Norway', + }, + oman: { + countryCode: 'OM', + name: 'Oman', + }, + pakistan: { + countryCode: 'PK', + name: 'Pakistan', + }, + palau: { + countryCode: 'PW', + name: 'Palau', + }, + panama: { + countryCode: 'PA', + name: 'Panama', + }, + papua_new_guinea: { + countryCode: 'PG', + name: 'Papua New Guinea', + }, + paraguay: { + countryCode: 'PY', + name: 'Paraguay', + }, + peru: { + countryCode: 'PE', + name: 'Peru', + }, + philippines: { + countryCode: 'PH', + name: 'Philippines', + }, + pitcairn: { + countryCode: 'PN', + name: 'Pitcairn', + }, + poland: { + countryCode: 'PL', + name: 'Poland', + }, + portugal: { + countryCode: 'PT', + name: 'Portugal', + }, + puerto_rico: { + countryCode: 'PR', + name: 'Puerto Rico', + }, + qatar: { + countryCode: 'QA', + name: 'Qatar', + }, + romania: { + countryCode: 'RO', + name: 'Romania', + }, + russia: { + countryCode: 'RU', + name: 'Russian Federation', + }, + rwanda: { + countryCode: 'RW', + name: 'Rwanda', + }, + réunion: { + countryCode: 'RE', + name: 'Réunion', + }, + saint_barthélemy: { + countryCode: 'BL', + name: 'Saint Barthélemy', + }, + saint_helena_ascension_and_tristan_da_cunha: { + countryCode: 'SH', + name: 'Saint Helena, Ascension and Tristan da Cunha', + }, + saint_kitts_and_nevis: { + countryCode: 'KN', + name: 'Saint Kitts and Nevis', + }, + saint_lucia: { + countryCode: 'LC', + name: 'Saint Lucia', + }, + saint_martin: { + countryCode: 'MF', + name: 'Saint Martin (French part)', + }, + saint_pierre_and_miquelon: { + countryCode: 'PM', + name: 'Saint Pierre and Miquelon', + }, + saint_vincent_and_the_grenadines: { + countryCode: 'VC', + name: 'Saint Vincent and the Grenadines', + }, + samoa: { + countryCode: 'WS', + name: 'Samoa', + }, + san_marino: { + countryCode: 'SM', + name: 'San Marino', + }, + sao_tome_and_principe: { + countryCode: 'ST', + name: 'Sao Tome and Principe', + }, + saudi_arabia: { + countryCode: 'SA', + name: 'Saudi Arabia', + }, + senegal: { + countryCode: 'SN', + name: 'Senegal', + }, + serbia: { + countryCode: 'RS', + name: 'Serbia', + }, + seychelles: { + countryCode: 'SC', + name: 'Seychelles', + }, + sierra_leone: { + countryCode: 'SL', + name: 'Sierra Leone', + }, + singapore: { + countryCode: 'SG', + name: 'Singapore', + }, + sint_maarten: { + countryCode: 'SX', + name: 'Sint Maarten (Dutch part)', + }, + slovakia: { + countryCode: 'SK', + name: 'Slovakia', + }, + slovenia: { + countryCode: 'SI', + name: 'Slovenia', + }, + solomon_islands: { + countryCode: 'SB', + name: 'Solomon Islands', + }, + somalia: { + countryCode: 'SO', + name: 'Somalia', + }, + south_africa: { + countryCode: 'ZA', + name: 'South Africa', + }, + south_georgia_and_the_south_sandwich_islands: { + countryCode: 'GS', + name: 'South Georgia and the South Sandwich Islands', + }, + south_korea: { + countryCode: 'KR', + name: 'Republic of Korea', + }, + south_sudan: { + countryCode: 'SS', + name: 'South Sudan', + }, + spain: { + countryCode: 'ES', + name: 'Spain', + }, + sri_lanka: { + countryCode: 'LK', + name: 'Sri Lanka', + }, + state_of_palestine: { + countryCode: 'PS', + name: 'State of Palestine', + }, + sudan: { + countryCode: 'SD', + name: 'Sudan', + }, + suriname: { + countryCode: 'SR', + name: 'Suriname', + }, + svalbard_and_jan_mayen: { + countryCode: 'SJ', + name: 'Svalbard and Jan Mayen', + }, + sweden: { + countryCode: 'SE', + name: 'Sweden', + }, + switzerland: { + countryCode: 'CH', + name: 'Switzerland', + }, + syria: { + countryCode: 'SY', + name: 'Syrian Arab Republic', + }, + taiwan: { + countryCode: 'TW', + name: 'Taiwan, Province of China', + }, + tajikistan: { + countryCode: 'TJ', + name: 'Tajikistan', + }, + tanzania: { + countryCode: 'TZ', + name: 'United Republic of Tanzania', + }, + thailand: { + countryCode: 'TH', + name: 'Thailand', + }, + timor_leste: { + countryCode: 'TL', + name: 'Timor-Leste', + }, + togo: { + countryCode: 'TG', + name: 'Togo', + }, + tokelau: { + countryCode: 'TK', + name: 'Tokelau', + }, + tonga: { + countryCode: 'TO', + name: 'Tonga', + }, + trinidad_and_tobago: { + countryCode: 'TT', + name: 'Trinidad and Tobago', + }, + tunisia: { + countryCode: 'TN', + name: 'Tunisia', + }, + turkey: { + countryCode: 'TR', + name: 'Turkey', + }, + turkmenistan: { + countryCode: 'TM', + name: 'Turkmenistan', + }, + turks_and_caicos_islands: { + countryCode: 'TC', + name: 'Turks and Caicos Islands', + }, + tuvalu: { + countryCode: 'TV', + name: 'Tuvalu', + }, + uganda: { + countryCode: 'UG', + name: 'Uganda', + }, + ukraine: { + countryCode: 'UA', + name: 'Ukraine', + }, + united_arab_emirates: { + countryCode: 'AE', + name: 'United Arab Emirates', + }, + united_kingdom: { + countryCode: 'GB', + name: 'United Kingdom of Great Britain and Northern Ireland', + }, + united_states_minor_outlying_islands: { + countryCode: 'UM', + name: 'United States Minor Outlying Islands', + }, + united_states_of_america: { + countryCode: 'US', + name: 'United States of America', + }, + uruguay: { + countryCode: 'UY', + name: 'Uruguay', + }, + uzbekistan: { + countryCode: 'UZ', + name: 'Uzbekistan', + }, + vanuatu: { + countryCode: 'VU', + name: 'Vanuatu', + }, + venezuela: { + countryCode: 'VE', + name: 'Bolivarian Republic of Venezuela', + }, + vietnam: { + countryCode: 'VN', + name: 'Viet Nam', + }, + virgin_islands: { + countryCode: 'VI', + name: 'Virgin Islands (U.S.)', + }, + wallis_and_futuna: { + countryCode: 'WF', + name: 'Wallis and Futuna', + }, + western_sahara: { + countryCode: 'EH', + name: 'Western Sahara', + }, + yemen: { + countryCode: 'YE', + name: 'Yemen', + }, + zambia: { + countryCode: 'ZM', + name: 'Zambia', + }, + zimbabwe: { + countryCode: 'ZW', + name: 'Zimbabwe', + }, +} as const; + +const getCountryByCountryCodeCache: { + [prop in CountryCode]?: Country; +} = {}; + +export const getCountryByCountryCode = (countryCode: CountryCode): Country => + (getCountryByCountryCodeCache[countryCode] ||= Object.values( + countries, + ).find((country) => country.countryCode === countryCode)) as Country; diff --git a/src/coutries.test.ts b/src/coutries.test.ts new file mode 100644 index 00000000..d760390e --- /dev/null +++ b/src/coutries.test.ts @@ -0,0 +1,22 @@ +import { countries, getCountryByCountryCode } from './countries'; + +describe('The getCountryByCountryCode', () => { + it('returns a country object', () => { + expect(getCountryByCountryCode('GB')).toEqual({ + name: 'United Kingdom of Great Britain and Northern Ireland', + countryCode: 'GB', + }); + }); +}); + +describe('The countries object', () => { + it('only contains unique country codes', () => { + const codes = Object.values(countries).map((c) => c.countryCode); + expect(codes.length).toBe(new Set(codes).size); + }); + + it('only contains unique country names', () => { + const names = Object.values(countries).map((c) => c.name); + expect(names.length).toBe(new Set(names).size); + }); +}); diff --git a/src/getLocale.ts b/src/getLocale.ts index 1e852b28..78451301 100644 --- a/src/getLocale.ts +++ b/src/getLocale.ts @@ -1,4 +1,4 @@ -import type { CountryCode } from '@guardian/types'; +import type { CountryCode } from './@types/countries'; import { getCookie, setSessionCookie } from './cookies'; import { isString } from './isString'; import { storage } from './storage'; diff --git a/src/index.ts b/src/index.ts index 6075c392..d070efd4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,9 @@ export { setCookie, setSessionCookie, } from './cookies'; +export type { Country } from './countries'; +export { countries, getCountryByCountryCode } from './countries'; +export type { CountryCode } from './@types/countries'; export { getLocale } from './getLocale'; export { getSwitches } from './getSwitches'; export { isBoolean } from './isBoolean';