-
Notifications
You must be signed in to change notification settings - Fork 20
/
phone.go
42 lines (33 loc) · 980 Bytes
/
phone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package utils
import (
"regexp"
"strings"
"github.com/nyaruka/phonenumbers"
)
var possiblePhone = regexp.MustCompile(`\+?[\d \.\-\(\)]{5,}`)
var onlyPhone = regexp.MustCompile(`^` + possiblePhone.String() + `$`)
// ParsePhoneNumber tries to parse the given string as a phone number. If successful, it returns it formatted as E164.
func ParsePhoneNumber(s, country string) string {
s = strings.TrimSpace(s)
if !onlyPhone.MatchString(s) {
return ""
}
phone, err := phonenumbers.Parse(s, country)
if err != nil {
return ""
}
if !phonenumbers.IsValidNumber(phone) {
return ""
}
return phonenumbers.Format(phone, phonenumbers.E164)
}
// FindPhoneNumbers finds phone numbers anywhere in the given string
func FindPhoneNumbers(s, country string) []string {
nums := make([]string, 0)
for _, candidate := range possiblePhone.FindAllString(s, -1) {
if num := ParsePhoneNumber(candidate, country); num != "" {
nums = append(nums, num)
}
}
return nums
}