Skip to content

Instantly share code, notes, and snippets.

@sysradium
Last active June 26, 2019 15:15
Show Gist options
  • Save sysradium/e275d42c803df9cc3f1c11e02e0a43dc to your computer and use it in GitHub Desktop.
Save sysradium/e275d42c803df9cc3f1c11e02e0a43dc to your computer and use it in GitHub Desktop.
package main
import (
"github.com/pkg/errors"
"github.com/ttacon/libphonenumber"
)
var (
InvalidPhoneNumber = errors.New("phone number is invalid")
)
func GetNormalisedPhoneNumber(number string) (string, error) {
num, err := libphonenumber.Parse(number, "")
if err != nil {
return "", errors.Wrap(err, "could not parse phone number")
}
if !libphonenumber.IsValidNumber(num) {
return "", errors.WithStack(InvalidPhoneNumber)
}
return libphonenumber.Format(num, libphonenumber.E164), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment