From a440087269155a130913afdc27753e81384b8506 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Sun, 1 Sep 2024 00:29:18 +0900 Subject: [PATCH 1/2] Add GetISO3166ByMobileNumber --- examples/main.go | 18 +++++++++++ iso3166.go | 8 ++--- phonenumber.go | 79 ++++++++++++++++++++++++++------------------- phonenumber_test.go | 29 ++++++++++++++++- 4 files changed, 96 insertions(+), 38 deletions(-) create mode 100644 examples/main.go diff --git a/examples/main.go b/examples/main.go new file mode 100644 index 0000000..e91268c --- /dev/null +++ b/examples/main.go @@ -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) +} diff --git a/iso3166.go b/iso3166.go index ab978dc..eff5362 100644 --- a/iso3166.go +++ b/iso3166.go @@ -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" @@ -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" diff --git a/phonenumber.go b/phonenumber.go index 1dec3c6..74358b8 100644 --- a/phonenumber.go +++ b/phonenumber.go @@ -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) @@ -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 { @@ -62,7 +106,6 @@ func getISO3166ByCountry(country string) ISO3166 { switch len(country) { case 0: iso3166 = GetISO3166()[0] - break case 2: for _, i := range GetISO3166() { if i.Alpha2 == uppperCaseCountry { @@ -70,7 +113,6 @@ func getISO3166ByCountry(country string) ISO3166 { break } } - break case 3: for _, i := range GetISO3166() { if i.Alpha3 == uppperCaseCountry { @@ -78,7 +120,6 @@ func getISO3166ByCountry(country string) ISO3166 { break } } - break default: for _, i := range GetISO3166() { if strings.ToUpper(i.CountryName) == uppperCaseCountry { @@ -86,34 +127,6 @@ func getISO3166ByCountry(country string) ISO3166 { 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 } @@ -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 } } diff --git a/phonenumber_test.go b/phonenumber_test.go index c91b009..0fb82b4 100644 --- a/phonenumber_test.go +++ b/phonenumber_test.go @@ -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) } } } @@ -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) + } + } + }) + } +} From c4c1d8ee9929050542d3ad966ee7c9e9de8fa99b Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sat, 31 Aug 2024 15:30:23 +0000 Subject: [PATCH 2/2] chore: Updated coverage badge. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1fa2b20..382af68 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # phonenumber -![Coverage](https://img.shields.io/badge/Coverage-99.4%25-brightgreen) +![Coverage](https://img.shields.io/badge/Coverage-99.5%25-brightgreen) [![Run Tests](https://github.com/dongri/phonenumber/actions/workflows/run-tests.yml/badge.svg)](https://github.com/dongri/phonenumber/actions?query=branch%3Amaster) ## Description