Skip to content

Commit

Permalink
mod phone updated
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Nov 9, 2023
1 parent b38f094 commit a09f89a
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/thiagozs/go-xutils
go 1.21

require (
github.com/dongri/phonenumber v0.1.1
github.com/google/uuid v1.3.1
github.com/stretchr/testify v1.8.4
github.com/xuri/excelize/v2 v2.8.0
Expand All @@ -22,6 +21,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/thiagozs/go-phonegen v1.0.2
github.com/ttacon/libphonenumber v1.2.1
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca // indirect
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dongri/phonenumber v0.1.1 h1:D/1Od5Ekl+qLIzNjwWg8u8Lp41ZdlwTpHC5Sx/XPqTk=
github.com/dongri/phonenumber v0.1.1/go.mod h1:G6eIK4UOT7iAcInROB6it2kRqlpR1U1GD2gqR9U5bGs=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand All @@ -24,6 +23,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/thiagozs/go-phonegen v1.0.2 h1:gux+eU1AssnqbegjYWZCIatVnltmohp0qj0MpS8pWFk=
github.com/thiagozs/go-phonegen v1.0.2/go.mod h1:Lz7IJJfCeNs6xVmJGytLg5bUy+6Hg5DgVed1vGGl0UA=
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 h1:5u+EJUQiosu3JFX0XS0qTf5FznsMOzTjGqavBGuCbo0=
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2/go.mod h1:4kyMkleCiLkgY6z8gK5BkI01ChBtxR0ro3I1ZDcGM3w=
github.com/ttacon/libphonenumber v1.2.1 h1:fzOfY5zUADkCkbIafAed11gL1sW+bJ26p6zWLBMElR4=
Expand Down Expand Up @@ -79,6 +80,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
Expand Down
34 changes: 32 additions & 2 deletions phone/phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import (
"fmt"
"strings"

"github.com/thiagozs/go-phonegen"
"github.com/ttacon/libphonenumber"
)

type Phone struct{}
type Phone struct {
phonegen *phonegen.PhoneGen
}

func New() *Phone {
return &Phone{}
return &Phone{
phonegen: phonegen.New(),
}
}

func (p *Phone) Normalize(phone, country string) (string, error) {
Expand All @@ -33,3 +38,28 @@ func (p *Phone) Normalize(phone, country string) (string, error) {

return normalizedPhone, nil
}

func (p *Phone) IsValid(phone, country string) bool {
_, err := p.Normalize(phone, country)
return err == nil
}

func (p *Phone) Generate(limit int) []string {
return p.phonegen.Random(limit)
}

func (p *Phone) GenMobile(country string, limit int) []string {
return p.phonegen.RandomMobile(limit)
}

func (p *Phone) GenLandline(country string, limit int) []string {
return p.phonegen.RandomLandline(limit)
}

func (p *Phone) GenMobileWithMask(limit int) []string {
return p.phonegen.RandomMobileWithMask(limit)
}

func (p *Phone) GenLandlineWithMask(limit int) []string {
return p.phonegen.RandomLandlineWithMask(limit)
}
255 changes: 255 additions & 0 deletions phone/phones_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,258 @@ func TestNormalize(t *testing.T) {
})
}
}

func TestIsValid(t *testing.T) {
tests := []struct {
name string
phone string
want bool
wantErr bool
}{
{
name: "Valid mobile phone number with country code",
phone: "+55 11 98765-4321",
want: true,
wantErr: false,
},
{
name: "Valid mobile phone number without country code",
phone: "11 98765-4321",
want: true,
wantErr: false,
},
{
name: "Valid mobile phone number with country code parantheses",
phone: "+55 (11) 98765-4321",
want: true,
wantErr: false,
},
{
name: "Valid mobile phone number without country code parantheses and dash",
phone: "(11) 98765-4321",
want: true,
wantErr: false,
},
{
name: "Invalid phone number",
phone: "00000000",
want: false,
wantErr: true,
},
{
name: "Invalid phone number with country code",
phone: "+55 00 000000000",
want: false,
wantErr: true,
},
{
name: "Invalid phone number with country code and parantheses",
phone: "+55 (00) 000000000",
want: false,
wantErr: true,
},
{
name: "Invalid phone number with country code and parantheses and dash",
phone: "+55 (00) 00000-0000",
want: false,
wantErr: true,
},
{
name: "Valid a residential phone number with country code",
phone: "+55 19 3611-4444",
want: true,
wantErr: false,
},
{
name: "Valid a residential phone number with country code and parantheses and dash",
phone: "+55 (19) 3611-4444",
want: true,
wantErr: false,
},
}

p := New()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := p.IsValid(tt.phone, "BR")
if got != tt.want {
t.Errorf("IsValid() = %v, want %v", got, tt.want)
}
})
}
}

func TestGenerate(t *testing.T) {
tests := []struct {
name string
limit int
}{
{
name: "Generate 10 random phone numbers",
limit: 10,
},
{
name: "Generate 100 random phone numbers",
limit: 100,
},
{
name: "Generate 1000 random phone numbers",
limit: 1000,
},
{
name: "Generate 10000 random phone numbers",
limit: 10000,
},
}

p := New()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := p.Generate(tt.limit)
if len(got) != tt.limit {
t.Errorf("Generate() = %v, want %v", len(got), tt.limit)
}
})
}
}

func TestGenMobile(t *testing.T) {
tests := []struct {
name string
country string
limit int
}{
{
name: "Generate 10 random mobile phone numbers",
country: "BR",
limit: 10,
},
{
name: "Generate 100 random mobile phone numbers",
country: "BR",
limit: 100,
},
{
name: "Generate 1000 random mobile phone numbers",
country: "BR",
limit: 1000,
},
{
name: "Generate 10000 random mobile phone numbers",
country: "BR",
limit: 10000,
},
}

p := New()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := p.GenMobile(tt.country, tt.limit)
if len(got) != tt.limit {
t.Errorf("GenMobile() = %v, want %v", len(got), tt.limit)
}
})
}
}

func TestGenLandline(t *testing.T) {
tests := []struct {
name string
country string
limit int
}{
{
name: "Generate 10 random landline phone numbers",
country: "BR",
limit: 10,
},
{
name: "Generate 100 random landline phone numbers",
country: "BR",
limit: 100,
},
{
name: "Generate 1000 random landline phone numbers",
country: "BR",
limit: 1000,
},
{
name: "Generate 10000 random landline phone numbers",
country: "BR",
limit: 10000,
},
}

p := New()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := p.GenLandline(tt.country, tt.limit)
if len(got) != tt.limit {
t.Errorf("GenLandline() = %v, want %v", len(got), tt.limit)
}
})
}
}

func TestGenMobileWithMask(t *testing.T) {
tests := []struct {
name string
limit int
}{
{
name: "Generate 10 random mobile phone numbers with mask",
limit: 10,
},
{
name: "Generate 100 random mobile phone numbers with mask",
limit: 100,
},
{
name: "Generate 1000 random mobile phone numbers with mask",
limit: 1000,
},
{
name: "Generate 10000 random mobile phone numbers with mask",
limit: 10000,
},
}

p := New()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := p.GenMobileWithMask(tt.limit)
if len(got) != tt.limit {
t.Errorf("GenMobileWithMask() = %v, want %v", len(got), tt.limit)
}
})
}
}

func BenchmarkNormalize(b *testing.B) {
p := New()

for i := 0; i < b.N; i++ {
p.Normalize("+55 11 98765-4321", "BR")
}
}

func BenchmarkIsValid(b *testing.B) {
p := New()

for i := 0; i < b.N; i++ {
p.IsValid("+55 11 98765-4321", "BR")
}
}

func BenchmarkGenerate(b *testing.B) {
p := New()

for i := 0; i < b.N; i++ {
p.Generate(1000)
}
}

0 comments on commit a09f89a

Please sign in to comment.