Skip to content

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

Open
@twocs

Description

@twocs

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions