Skip to content

Commit

Permalink
Merge pull request #50 from dongri/fix-gb-parse
Browse files Browse the repository at this point in the history
Fix parse number
  • Loading branch information
dongri authored Nov 3, 2024
2 parents f67c258 + 636804c commit 56346e6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ func main() {

parsed := phonenumber.ParseWithLandLine("+1 289 2999", "US")
fmt.Println(parsed)

pn1 := "+44 07700900000"
fmt.Println(phonenumber.Parse(pn1, "GB"))

pn2 := "07700900000"
fmt.Println(phonenumber.Parse(pn2, "GB"))

pn3 := "447700900000"
fmt.Println(phonenumber.Parse(pn3, "GB"))

pn4 := "+8109012345678"
fmt.Println(phonenumber.Parse(pn4, "JP"))

}
9 changes: 9 additions & 0 deletions phonenumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ func parseInternal(number string, country string, landLineInclude bool) string {

iso3166 := getISO3166ByCountry(country)

// if number starts with country code and includes leading zero, remove the leading zero
if strings.HasPrefix(number, iso3166.CountryCode) {
withoutCountryCode := strings.Replace(number, iso3166.CountryCode, "", 1)
if strings.HasPrefix(withoutCountryCode, "0") {
withoutCountryCode = strings.Replace(withoutCountryCode, "0", "", 1)
}
number = iso3166.CountryCode + withoutCountryCode
}

if indexOfString(iso3166.Alpha3, []string{"GAB", "CIV", "COG"}) == -1 {
number = leadZeroRegexp.ReplaceAllString(number, "")
}
Expand Down

0 comments on commit 56346e6

Please sign in to comment.