Merge pull request #89 from dendd1/master

US number support
This commit is contained in:
Pavel 2025-02-17 16:07:43 +03:00 committed by GitHub
commit e0f94c8beb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 4 deletions

View File

@ -8,8 +8,8 @@ import (
"strconv"
"strings"
pn "github.com/nyaruka/phonenumbers"
phoneiso3166 "github.com/onlinecity/go-phone-iso3166"
pn "github.com/ttacon/libphonenumber"
)
const (
@ -22,6 +22,7 @@ const (
CountryPhoneCodeUZ = 998
PalestineRegion = "PS"
BangladeshRegion = "BD"
MexicanNationalSize = 11
)
var (
@ -30,7 +31,7 @@ var (
ErrCannotParsePhone = errors.New("cannot parse phone number")
TrimmedPhoneRegexp = regexp.MustCompile(`\D+`)
UndefinedUSCodes = []string{"1445", "1945", "1840", "1448", "1279", "1839"}
UndefinedUSCodes = []string{"1445", "1945", "1840", "1448", "1279", "1839", "1555"}
)
// FormatNumberForWA forms in the format according to the rules https://faq.whatsapp.com/1294841057948784
@ -182,6 +183,9 @@ func getUzbekistanNationalNumber(phone string, parsedPhone *pn.PhoneNumber) (uin
func getMexicanNationalNumber(parsedPhone *pn.PhoneNumber) (uint64, error) {
phoneWithDigit := fmt.Sprintf("1%d", parsedPhone.GetNationalNumber())
if len(phoneWithDigit) > MexicanNationalSize {
return parsedPhone.GetNationalNumber(), nil
}
num, err := strconv.Atoi(phoneWithDigit)

View File

@ -9,7 +9,7 @@ import (
)
func TestParsePhone(t *testing.T) {
t.Run("russian numers", func(t *testing.T) {
t.Run("russian numbers", func(t *testing.T) {
n := "+88002541213"
pn, err := ParsePhone(n)
require.NoError(t, err)
@ -37,6 +37,14 @@ func TestParsePhone(t *testing.T) {
})
t.Run("us numbers", func(t *testing.T) {
n := "15557043340"
pn, err := ParsePhone(n)
require.NoError(t, err)
assert.Equal(t, uint64(5557043340), pn.GetNationalNumber())
assert.Equal(t, int32(1), pn.GetCountryCode())
})
t.Run("german numbers", func(t *testing.T) {
n := "491736276098"
pn, err := ParsePhone(n)
@ -116,6 +124,7 @@ func TestFormatNumberForWA(t *testing.T) {
"19455009160": "+19455009160",
"19452381431": "+19452381431",
"12793006305": "+12793006305",
"15557043340": "+15557043340",
}
for orig, expected := range numbers {

3
go.mod
View File

@ -23,12 +23,12 @@ require (
github.com/jessevdk/go-flags v1.6.1
github.com/jinzhu/gorm v1.9.11
github.com/nicksnyder/go-i18n/v2 v2.4.1
github.com/nyaruka/phonenumbers v1.5.0
github.com/onlinecity/go-phone-iso3166 v0.0.1
github.com/retailcrm/api-client-go/v2 v2.1.17
github.com/retailcrm/mg-transport-api-client-go v1.3.19
github.com/retailcrm/zabbix-metrics-collector v1.0.0
github.com/stretchr/testify v1.10.0
github.com/ttacon/libphonenumber v1.2.1
go.uber.org/atomic v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/text v0.21.0
@ -88,6 +88,7 @@ require (
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect

4
go.sum
View File

@ -288,6 +288,8 @@ github.com/nicksnyder/go-i18n/v2 v2.4.1 h1:zwzjtX4uYyiaU02K5Ia3zSkpJZrByARkRB4V3
github.com/nicksnyder/go-i18n/v2 v2.4.1/go.mod h1:++Pl70FR6Cki7hdzZRnEEqdc2dJt+SAGotyFg/SvZMk=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nyaruka/phonenumbers v1.5.0 h1:0M+Gd9zl53QC4Nl5z1Yj1O/zPk2XXBUwR/vlzdXSJv4=
github.com/nyaruka/phonenumbers v1.5.0/go.mod h1:gv+CtldaFz+G3vHHnasBSirAi3O2XLqZzVWz4V1pl2E=
github.com/onlinecity/go-phone-iso3166 v0.0.1 h1:srN6o8NjxBWIrlK6Z+zD9wGMSGYi4itWA/fRyaxetqs=
github.com/onlinecity/go-phone-iso3166 v0.0.1/go.mod h1:n8+yIOCu9O63MH3WVwlWq1YVF6ZuAG5xlZ4mZ5ZzKF8=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@ -385,6 +387,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=