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

Feature request: expose a function to convert CountryCallingCode to CountryCode, or expose MetadataJson value #420

Open
bbarry opened this issue Feb 3, 2022 · 13 comments

Comments

@bbarry
Copy link

bbarry commented Feb 3, 2022

There is a function exposed getCountryCallingCode(countryCode: CountryCode): CountryCallingCode) but no inverse of this function:

export function getAllCountryCodes(countryCallingCode: CountryCallingCode): CountryCode[] | undefined {
    return metadata.country_calling_codes[countryCallingCode];
}

This would be useful if you are trying to use any of the functionality that uses CountryCode but your dataset happens to have a calling code separate from the numbers but no country data.

Alternatively exposing the built in metadata would allow users to do this ourselves. An implementation I am playing with today needs this function in order to figure out which parameters are needed to pass to parsePhoneNumber to handle a large amount of questionably formatted data. I've resorted to:

const phoneMetadata: Record<string, any> = new Metadata();
let metadataJson: MetadataJson | undefined = undefined;
if ('metadata' in phoneMetadata) {
  metadataJson = phoneMetadata.metadata;
}
if (!metadataJson) {
  throw new Error(
    'libphonenumber-js implementation changed; figure out new way to get country codes from country calling codes'
  );
}

to write the above function...

@catamphetamine
Copy link
Owner

catamphetamine commented Feb 3, 2022 via email

@awcs
Copy link

awcs commented Nov 29, 2022

I think he meant for cases like multiple countries for one country calling code we need a function to extract those countries (that's my case).

Example for country calling code +44, we could have a function like this:
const countryList = libPhoneNumber.getCountriesByCallingCode(44)

And in countryList we would have : ['GB', 'GG', 'IM', 'JE']

@rwev
Copy link

rwev commented Dec 21, 2022

Multiple countries can be associated with a given calling code.

Giving a calling code parsed from phone number string, the country may be ambiguous.

In this case, PhoneNumber.country is undefined.

There is a one to many mapping CountryCallingCode -> CountryCode.

The best we can do as awcs mentioned, is get the list of countries associated with that country.

In our case, we just default to the first country associated with the calling code; the metadata seems to be organized in such a way that the most prominent countries are first in the lists.

  const code =
    parsedPhoneNumber.country ??
    _.get(parsedPhoneNumber, ['metadata', 'country_calling_codes', parsedPhoneNumber.countryCallingCode, '0'])

@catamphetamine
Copy link
Owner

@rwev I don't really see a scenario in which a person would want to somehow use the list of countries corresponding to a specific country calling code. Could you provide one?

@catamphetamine
Copy link
Owner

In any case, one could also use the Metadata class and then inspect its methods.
For example, this one:
https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/26b50cdd5c61416ebd12bcbc77045bfe400bb28d/source/metadata.js#L99

@catamphetamine
Copy link
Owner

It actually says it at the end of the readme section:
https://gitlab.com/catamphetamine/libphonenumber-js#programmatic-access

As one can see, the Metadata class is not documented much. Partially, that's because its usage is not necessarily encouraged, but it's still used, for example, in react-phone-number-input to get "leading digits" for a country, or to get maximum phone number length for a country. Stick to the methods documented above, don't call any other methods. If you think there's a need to call any other methods, create a discussion issue.As one can see, the Metadata class is not documented much. Partially, that's because its usage is not necessarily encouraged, but it's still used, for example, in react-phone-number-input to get "leading digits" for a country, or to get maximum phone number length for a country. Stick to the methods documented above, don't call any other methods. If you think there's a need to call any other methods, create a discussion issue.

@catamphetamine
Copy link
Owner

So if someone provides a sensible use case for a getCountryCodesForCallingCode() method then I'd include it in the readme.

@rwev
Copy link

rwev commented Dec 21, 2022

A phone number input in which the user can select a country.

https://dribbble.com/shots/15474151-Phone-Number-Input-Field-Exploration

Determining the country code from the selected country is easily done via getCountryCallingCode

Repopulating the input from the final phone number is where the problem arises, as the parsed countryCallingCode does not determine the original country.

Classic problem of reversing a many -> one calculation (i.e. one -> many)

@catamphetamine
Copy link
Owner

catamphetamine commented Dec 21, 2022 via email

@awcs
Copy link

awcs commented Jan 6, 2023

There's a difference between "the country is unknown" and "there are multiple countries available"... Some phone number can belong to multiple country as i explained in my first comment.

Access metadata, which are not typed btw, seems a little bit random.

@catamphetamine
Copy link
Owner

There's a difference between "the country is unknown" and "there are multiple countries available"...

No. "Country is unknown" means exactly that.

Some phone number can belong to multiple country as i explained in my first comment.

No it can't

@catamphetamine
Copy link
Owner

Access metadata, which are not typed btw, seems a little bit random.

Metadata is perfectly typed and accessing it is in no way random.

@catamphetamine
Copy link
Owner

Published [email protected]:

Added PhoneNumber.getPossibleCountries() function. It returns a list of countries this phone number could possibly belong to. Can be used when parsing complete international phone numbers containing a "calling code" that is shared between several countries. If parsing such a phone number returns country: undefined then getPossibleCountries() function could be used to somehow speculate about what country could this phone number possibly belong to.

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

No branches or pull requests

4 participants