Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getCountryCode may not return for some valid countries #137

Open
dezull opened this issue Mar 21, 2024 · 0 comments
Open

getCountryCode may not return for some valid countries #137

dezull opened this issue Mar 21, 2024 · 0 comments

Comments

@dezull
Copy link

dezull commented Mar 21, 2024

Use case for the feature

For example, getCountryCode("Cocos (Keeling) Islands") won't return the correct country code, because of the parentheses in the country name.

Examples or links

The countryName used in the RegExp in the function is not escaped, so characters such as parentheses are interpreted as special characters:

const nameRegex = new RegExp('^' + countryName + '$', 'i')

The solution in #131 can fix this, or a slight modification:

export const getCountryCode = (countryName: string): TCountryCode | false => {
  // Match exact country name, but case insensitive
  const country = countryName.toLowerCase();

  return (
    countryDataList.find(({ name, native }) =>
       country === name.toLowerCase() ||
       country === native.toLowerCase()
    )?.iso2 || false
  )
}

Maybe allowing something like "Cocos Islands" or "Keeling Islands" also makes sense?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant