-
Notifications
You must be signed in to change notification settings - Fork 216
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
Comments
Use a combination of getCountries() and getCountryCallingCode() to build an
initial map of calling code to countries.
It’s not clear what exactly you request.
…On Thu, 3 Feb 2022 at 23:39, Bill Barry ***@***.***> wrote:
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...
—
Reply to this email directly, view it on GitHub
<#420>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADUP355LY5KMD7KZS6GYIDUZLRZFANCNFSM5NP7KXGQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
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: And in countryList we would have : |
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, There is a one to many mapping The best we can do as 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']) |
@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? |
In any case, one could also use the |
It actually says it at the end of the readme section:
|
So if someone provides a sensible use case for a |
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 Repopulating the input from the final phone number is where the problem arises, as the parsed Classic problem of reversing a |
If a phone number belongs to any country, its ‘country’ will be set.
Otherwise, the country is unknown
…On Wed, 21 Dec 2022 at 23:23, rwev ***@***.***> wrote:
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)
—
Reply to this email directly, view it on GitHub
<#420 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADUP33P6VVDTL3EPR234WLWONRL5ANCNFSM5NP7KXGQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
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. |
No. "Country is unknown" means exactly that.
No it can't |
Metadata is perfectly typed and accessing it is in no way random. |
Published Added |
There is a function exposed
getCountryCallingCode(countryCode: CountryCode): CountryCallingCode)
but no inverse of this function: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:
to write the above function...
The text was updated successfully, but these errors were encountered: