diff --git a/src/utils/getCountryId.ts b/src/utils/getCountryId.ts index 39c8f848f..e6458fc91 100644 --- a/src/utils/getCountryId.ts +++ b/src/utils/getCountryId.ts @@ -3,18 +3,18 @@ import countries from "../assets/countries.json"; /** * Get the country id base on the two letter id */ -export function getCountryId(country: string) { - if (country.length !== 2) { +export function getCountryId(country: string | null) { + if (!country || country.length !== 2) { return country; } const upperCountry = country.toUpperCase(); - const countryId = Object.keys(countries).find( - (id) => countries[id].countryCode === upperCountry, + const countryObject = countries.find( + ({ countryCode }) => countryCode === upperCountry, ); - if (countryId === undefined) { + if (!countryObject || !countryObject.id) { return country; } - return countryId; + return countryObject.id; }