nyaruka / phonenumbers

GoLang port of Google's libphonenumber library
MIT License
1.25k stars 148 forks source link

Partial number formatting #52

Closed nathan-fiscaletti closed 6 months ago

nathan-fiscaletti commented 4 years ago

Is there a way to format a number that is not complete?

For example, the following COMPLETE number works fine

    iso := "US"
    phone := "2345678901"
    num, _ := phonenumbers.Parse(phone, iso)
    formatted := phonenumbers.Format(num, phonenumbers.NATIONAL)
    fmt.Printf("%s\n", formatted)
(234) 567-8901

However, if I instead pass a PARTIAL number, it loses all formatting.

    iso := "US"
    phone := "23456789"
    num, _ := phonenumbers.Parse(phone, iso)
    formatted := phonenumbers.Format(num, phonenumbers.NATIONAL)
    fmt.Printf("%s\n", formatted)
23456789

I would like it if that would spit out:

(234) 567-89

Since I want to use this for as the user types their phone number in, displaying it in a proper format.

Is this doable with this library?

nicpottier commented 4 years ago

This isn't currently supported, but if you want to put together a PR we'd probably accept it. Would need a port of: https://github.com/google/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java

nathan-fiscaletti commented 3 years ago

I've created the initial port of the AsYouTypeFormatter, however, it's not functional in its current state. Would love someone else to take a look if they have the time.

https://github.com/nathan-fiscaletti/phonenumbers/blob/master/asyoutypeformatter.go

nathan-fiscaletti commented 3 years ago

Managed to work through the errors, just need to figure out why none of the test cases succeed :(