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

Case insensitive search in getCountryCode function #131

Closed
Tracked by #135
astronomyf opened this issue Dec 29, 2023 · 1 comment · Fixed by #135
Closed
Tracked by #135

Case insensitive search in getCountryCode function #131

astronomyf opened this issue Dec 29, 2023 · 1 comment · Fixed by #135

Comments

@astronomyf
Copy link

astronomyf commented Dec 29, 2023

Use case for the feature

The getCountryCode function currently performs a case-sensitive search for country names, which can limit its effectiveness. Users might input country names in various cases (e.g., "United States", "united states", "UNITED STATES"), expecting a valid response. However, the current implementation only matches exact cases.

I've looked into the past issues and I noticed that this was never reported before. I understand it's easy enough to implement my own search function, but since it comes bundled in the library I think it would be beneficial to all users.

Examples or links

This is the function in question:

import { TCountryCode } from './types.ts'
import { getCountryDataList } from './getCountryData.ts'

const countryDataList = getCountryDataList()

export const getCountryCode = (countryName: string): TCountryCode | false =>
  countryDataList.find(({ name, native }) => countryName === name || countryName === native)
    ?.iso2 || false

A potential, quick solution could just be to compare both strings as lower case:

export const getCountryCode = (countryName: string): TCountryCode | false =>
  countryDataList.find(({ name, native }) => 
    countryName.toLowerCase() === name.toLowerCase() || 
    countryName.toLowerCase() === native.toLowerCase()
  )?.iso2 || false

It could also be interesting to think about implementing a fuzzy search, even though it might increase bundle size and there would be the need to mitigate performance issues.

I'm open to make a PR for this, if it makes sense to you 😄

@dmythro
Copy link
Member

dmythro commented Dec 31, 2023

Yes, I agree it makes total sense!

I'll check options, maybe creating a RegExp with /i flag would perform better than a lot of transformations to lower case.

@dmythro dmythro linked a pull request Mar 4, 2024 that will close this issue
4 tasks
dmythro added a commit that referenced this issue Mar 4, 2024
* Bump modules

* Update types and data for cases like #36 and #132

* Update comments in types.ts

* Update phone code for Kazakhstan to just `7` #134

* Case insensitive search in getCountryCode function #131

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

Successfully merging a pull request may close this issue.

2 participants