diff --git a/examples/main.go b/examples/main.go index e91268c..b3df481 100644 --- a/examples/main.go +++ b/examples/main.go @@ -15,4 +15,7 @@ func main() { countries = phonenumber.GetISO3166ByMobileNumber("14855512329") fmt.Println(countries[0].CountryName) + + parsed := phonenumber.ParseWithLandLine("+1 289 2999", "US") + fmt.Println(parsed) } diff --git a/phonenumber.go b/phonenumber.go index 74358b8..7c3a7f7 100644 --- a/phonenumber.go +++ b/phonenumber.go @@ -70,9 +70,11 @@ func GetISO3166ByMobileNumber(number string) []ISO3166 { func parseInternal(number string, country string, landLineInclude bool) string { number = strings.Replace(number, " ", "", -1) country = strings.Replace(country, " ", "", -1) - plusSign := false + if strings.HasPrefix(number, "+") { - plusSign = true + if country == "" { + return "" + } } // remove any non-digit character, included the + @@ -87,12 +89,8 @@ func parseInternal(number string, country string, landLineInclude bool) string { if iso3166.Alpha3 == "RUS" && len(number) == 11 && rusLocaleMobPrefixRegexp.MatchString(number) { number = rusLocalePrefixRegexp.ReplaceAllString(number, "") } - if plusSign { - iso3166 = GetISO3166ByNumber(number, landLineInclude) - } else { - if indexOfInt(len(number), iso3166.PhoneNumberLengths) != -1 { - number = iso3166.CountryCode + number - } + if indexOfInt(len(number), iso3166.PhoneNumberLengths) != -1 { + number = iso3166.CountryCode + number } if validatePhoneISO3166(number, iso3166, landLineInclude) { return number diff --git a/phonenumber_test.go b/phonenumber_test.go index 0fb82b4..f4d7785 100644 --- a/phonenumber_test.go +++ b/phonenumber_test.go @@ -143,6 +143,7 @@ var mobWithLLFormatTests = []struct { {"+51 999 400 500", "PE", "51999400500"}, {"+86 (16) 855-512-329", "CN", "8616855512329"}, {"+383 4 1234999", "XK", "38341234999"}, + {"+1 289 2999", "USA", ""}, } func TestFormatWithLandLine(t *testing.T) {