Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dongri committed Aug 31, 2024
1 parent 06df42b commit aff54bc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
3 changes: 3 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 6 additions & 8 deletions phonenumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions phonenumber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit aff54bc

Please sign in to comment.