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

Unable to create phone number from country code + national number #196

Open
twocs opened this issue Feb 10, 2025 · 0 comments
Open

Unable to create phone number from country code + national number #196

twocs opened this issue Feb 10, 2025 · 0 comments

Comments

@twocs
Copy link

twocs commented Feb 10, 2025

For example, from my webform, I might have the country calling code "+1" and the national number "202456111". What is the expected way to go from that to a parsed phonenumber?

The Java version of libphonenumber allows creation of a number like this:

    PhoneNumber numberA = new PhoneNumber();
    numberA.setCountryCode(1).setNationalNumber(2024561111L);

But for using nyaruka/phonenumbers I don't see any pattern for creating a number from a separate country code and national number. There aren't any setters. Checking GitHub, I'm seeing code that basically concatenates the two strings together, but this involves some trimming and adding a + if not present.

countryCode := "+1"
nationalNumber := "2024561111"
concatenatedNumber := fmt.Sprintf("%s%s", countryCode, nationalNumber)
phonenumber, err := phonenumbers.Parse(concatenatedNumber, "")

Am I missing the intended way to create a phonenumber from countryCode + nationalNumber? Is it to convert country code to region instead of setting the values directly, e.g.

package main

import (
	"fmt"

	"github.com/nyaruka/phonenumbers"
)

func main() {
	countryCode := 1
	nationalNumber := "202 456 1111"
	regionCode := phonenumbers.GetRegionCodeForCountryCode(countryCode)
	p, err := phonenumbers.Parse(nationalNumber, regionCode)
	fmt.Println("Hello,", phonenumbers.IsValidNumber(p), err)
}

In other words, shouldn't there be something like the SetCountryCode and SetNationalNumber in this library?

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

1 participant