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

Closed
dezull opened this issue Mar 21, 2024 · 3 comments
Closed

getCountryCode may not return for some valid countries #137

dezull opened this issue Mar 21, 2024 · 3 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?

@osztenkurden
Copy link

Getting name of the Myanmar (Burma) is also impossible due to this issue unfortunately

@dmythro
Copy link
Member

dmythro commented Jul 30, 2024

Hi, forgot about this. Someone could've make a PR :) But looks like no volunteers. I'll check it out and update tests to make sure those countries are found properly.

@dmythro
Copy link
Member

dmythro commented Jul 31, 2024

@dezull @osztenkurden please check if v3.1.1 works for you. Increased test coverage so seems fine with any name now.

@dmythro dmythro closed this as completed Jul 31, 2024
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

3 participants