Skip to content

Commit

Permalink
Add GetISO3166ByMobileNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
dongri committed Aug 31, 2024
1 parent 8d83bc2 commit a440087
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 38 deletions.
18 changes: 18 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"

"github.com/dongri/phonenumber"
)

func main() {
countries := phonenumber.GetISO3166ByMobileNumber("07933846223")
fmt.Println(countries[0].CountryName)

countries = phonenumber.GetISO3166ByMobileNumber("09061353368")
fmt.Println(countries[0].CountryName)

countries = phonenumber.GetISO3166ByMobileNumber("14855512329")
fmt.Println(countries[0].CountryName)
}
8 changes: 4 additions & 4 deletions iso3166.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ func populateISO3166() {
i.Alpha3 = "GBR"
i.CountryCode = "44"
i.CountryName = "United Kingdom"
i.MobileBeginWith = []string{"7"}
i.PhoneNumberLengths = []int{10}
i.MobileBeginWith = []string{"7", "07"}
i.PhoneNumberLengths = []int{10, 11}
iso3166Datas = append(iso3166Datas, i)

i.Alpha2 = "GE"
Expand Down Expand Up @@ -886,8 +886,8 @@ func populateISO3166() {
i.Alpha3 = "JPN"
i.CountryCode = "81"
i.CountryName = "Japan"
i.MobileBeginWith = []string{"70", "80", "90"}
i.PhoneNumberLengths = []int{10}
i.MobileBeginWith = []string{"70", "80", "90", "070", "080", "090"}
i.PhoneNumberLengths = []int{10, 11}
iso3166Datas = append(iso3166Datas, i)

i.Alpha2 = "KZ"
Expand Down
79 changes: 46 additions & 33 deletions phonenumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,50 @@ func ParseWithLandLine(number string, country string) string {
return parseInternal(number, country, true)
}

// GetISO3166ByNumber ...
func GetISO3166ByNumber(number string, withLandLine bool) ISO3166 {
iso3166 := ISO3166{}
for _, i := range GetISO3166() {
r := getRegexpByCountryCode(i.CountryCode)
for _, l := range i.PhoneNumberLengths {
if r.MatchString(number) && len(number) == len(i.CountryCode)+l {
// Check match with mobile codes
for _, w := range i.MobileBeginWith {
rm := getRegexpByCountryCode(i.CountryCode + w)
if rm.MatchString(number) {
// Match by mobile codes
return i
}
}

// Match by country code only for landline numbers only
if withLandLine {
iso3166 = i
break
}
}
}
}
return iso3166
}

// GetISO3166ByMobileNumber ...
func GetISO3166ByMobileNumber(number string) []ISO3166 {
result := []ISO3166{}
for _, i := range GetISO3166() {
for _, l := range i.PhoneNumberLengths {
if len(number) == l {
for _, w := range i.MobileBeginWith {
if w != "" && strings.HasPrefix(number, w) {
result = append(result, i)
}
}
}
}
}
return result
}

func parseInternal(number string, country string, landLineInclude bool) string {
number = strings.Replace(number, " ", "", -1)
country = strings.Replace(country, " ", "", -1)
Expand All @@ -40,7 +84,7 @@ func parseInternal(number string, country string, landLineInclude bool) string {
number = leadZeroRegexp.ReplaceAllString(number, "")
}

if iso3166.Alpha3 == "RUS" && len(number) == 11 && rusLocaleMobPrefixRegexp.MatchString(number) == true {
if iso3166.Alpha3 == "RUS" && len(number) == 11 && rusLocaleMobPrefixRegexp.MatchString(number) {
number = rusLocalePrefixRegexp.ReplaceAllString(number, "")
}
if plusSign {
Expand All @@ -62,58 +106,27 @@ func getISO3166ByCountry(country string) ISO3166 {
switch len(country) {
case 0:
iso3166 = GetISO3166()[0]
break
case 2:
for _, i := range GetISO3166() {
if i.Alpha2 == uppperCaseCountry {
iso3166 = i
break
}
}
break
case 3:
for _, i := range GetISO3166() {
if i.Alpha3 == uppperCaseCountry {
iso3166 = i
break
}
}
break
default:
for _, i := range GetISO3166() {
if strings.ToUpper(i.CountryName) == uppperCaseCountry {
iso3166 = i
break
}
}
break
}
return iso3166
}

// GetISO3166ByNumber ...
func GetISO3166ByNumber(number string, withLandLine bool) ISO3166 {
iso3166 := ISO3166{}
for _, i := range GetISO3166() {
r := getRegexpByCountryCode(i.CountryCode)
for _, l := range i.PhoneNumberLengths {
if r.MatchString(number) && len(number) == len(i.CountryCode)+l {
// Check match with mobile codes
for _, w := range i.MobileBeginWith {
rm := getRegexpByCountryCode(i.CountryCode + w)
if rm.MatchString(number) {
// Match by mobile codes
return i
}
}

// Match by country code only for landline numbers only
if withLandLine == true {
iso3166 = i
break
}
}
}
}
return iso3166
}
Expand All @@ -139,7 +152,7 @@ func validatePhoneISO3166(number string, iso3166 ISO3166, withLandLine bool) boo
if l == len(number) {
for _, w := range iso3166.MobileBeginWith {
rm := getRegexpByCountryCode(w)
if rm.MatchString(number) == true {
if rm.MatchString(number) {
return true
}
}
Expand Down
29 changes: 28 additions & 1 deletion phonenumber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestFormatForLandLineIsEmpty(t *testing.T) {
for _, tt := range mobFormatTestsNegative {
number := Parse(tt.input, tt.country)
if number != "" {
t.Errorf("Parse(number=`%s`, country=`%s`) for landline number miust be empty, actual `%s`", tt.input, tt.country, number)
t.Errorf("Parse(number=`%s`, country=`%s`) for landline number must be empty, actual `%s`", tt.input, tt.country, number)
}
}
}
Expand Down Expand Up @@ -338,3 +338,30 @@ func TestGetCountryForMobileNumber(t *testing.T) {

}
}

// Get country by mobile number only
var mobileNumbers = []struct {
input string
expected string
}{
// Mobile numbers
{"39339638066", "IT"},
{"07933846223", "GB"},
{"14855512329", "CN"},
}

func TestGetISO3166ByMobileNumber(t *testing.T) {
for _, tt := range mobileNumbers {
tt := tt
t.Run(tt.input, func(t *testing.T) {
t.Parallel()
countries := GetISO3166ByMobileNumber(tt.input)
expected := getISO3166ByCountry(tt.expected)
for _, country := range countries {
if country.CountryName != expected.CountryName {
t.Errorf("GetISO3166ByMobileNumber(number=`%s`): expected `%s`, actual `%s`", tt.input, expected.CountryName, country.CountryName)
}
}
})
}
}

0 comments on commit a440087

Please sign in to comment.