Skip to content

Commit

Permalink
Add function to parse string to phone format
Browse files Browse the repository at this point in the history
  • Loading branch information
dynastymasra committed Mar 31, 2020
1 parent ec52cea commit 79e7495
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/gorilla/mux v1.7.4
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
github.com/newrelic/go-agent v3.3.0+incompatible
github.com/nyaruka/phonenumbers v1.0.54
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.2.0
github.com/spf13/viper v1.6.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
Expand Down Expand Up @@ -75,6 +77,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/newrelic/go-agent v3.3.0+incompatible h1:2J3ssCWfOl/j/c+dV0Ps8BZaNStWPw8ApHA2AqVjEi8=
github.com/newrelic/go-agent v3.3.0+incompatible/go.mod h1:a8Fv1b/fYhFSReoTU6HDkTYIMZeSVNffmoS726Y0LzQ=
github.com/nyaruka/phonenumbers v1.0.54 h1:vU9IUfiHrpu+lZcCkjEzDsCIdurQV8lxjrAdqW2osAU=
github.com/nyaruka/phonenumbers v1.0.54/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down
16 changes: 16 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cookbook
import (
"encoding/json"
"strconv"

"github.com/nyaruka/phonenumbers"
)

// ParseStringToInt Parse string to int, error return default value
Expand Down Expand Up @@ -48,3 +50,17 @@ func ParsePtrString(s *string) string {
}
return *s
}

// ParseStringToPhone Function to parse string to formatted phone number
// See details documentation in https://github.com/nyaruka/phonenumbers
// defaultRegionCode using ISO 3166-Alpha 2
func ParseStringToPhone(phone, defaultRegionCode string, phoneFormat phonenumbers.PhoneNumberFormat) (string, error) {
num, err := phonenumbers.Parse(phone, defaultRegionCode)
if err != nil {
return "", err
}

formattedNum := phonenumbers.Format(num, phoneFormat)

return formattedNum, nil
}
34 changes: 34 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cookbook_test
import (
"testing"

"github.com/nyaruka/phonenumbers"

"github.com/dynastymasra/cookbook"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -91,3 +93,35 @@ func (p *ParseSuite) Test_ParsePtrString_Nil() {

assert.Empty(p.T(), result)
}

func (p *ParseSuite) Test_ParseStringToPhone_Success() {
res1, err1 := cookbook.ParseStringToPhone("+64 3 345 6789", "", phonenumbers.E164)
res2, err2 := cookbook.ParseStringToPhone("+64 21 345 687", "", phonenumbers.E164)
res3, err3 := cookbook.ParseStringToPhone("+64 021 345 687", "", phonenumbers.E164)
res4, err4 := cookbook.ParseStringToPhone("+64 021-345-687", "", phonenumbers.E164)

assert.Equal(p.T(), "+6433456789", res1)
assert.Equal(p.T(), "+6421345687", res2)
assert.Equal(p.T(), "+6421345687", res3)
assert.Equal(p.T(), "+6421345687", res4)
assert.NoError(p.T(), err1)
assert.NoError(p.T(), err2)
assert.NoError(p.T(), err3)
assert.NoError(p.T(), err4)
}

func (p *ParseSuite) Test_ParseStringToPhone_Failed() {
res1, err1 := cookbook.ParseStringToPhone("03 345 6789", "", phonenumbers.E164)
res2, err2 := cookbook.ParseStringToPhone("021 345 678", "", phonenumbers.E164)
res3, err3 := cookbook.ParseStringToPhone("64 03 345 6789", "", phonenumbers.E164)
res4, err4 := cookbook.ParseStringToPhone("64 21 345 678", "", phonenumbers.E164)

assert.Equal(p.T(), "", res1)
assert.Equal(p.T(), "", res2)
assert.Equal(p.T(), "", res3)
assert.Equal(p.T(), "", res4)
assert.Error(p.T(), err1)
assert.Error(p.T(), err2)
assert.Error(p.T(), err3)
assert.Error(p.T(), err4)
}

0 comments on commit 79e7495

Please sign in to comment.