Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sundowndev
GitHub Repository: sundowndev/phoneinfoga
Path: blob/master/lib/number/number.go
994 views
1
package number
2
3
import (
4
"github.com/nyaruka/phonenumbers"
5
)
6
7
// Number is a phone number
8
type Number struct {
9
Valid bool
10
RawLocal string
11
Local string
12
E164 string
13
International string
14
CountryCode int32
15
Country string
16
Carrier string
17
}
18
19
func NewNumber(number string) (res *Number, err error) {
20
n := "+" + FormatNumber(number)
21
country := ParseCountryCode(n)
22
23
num, err := phonenumbers.Parse(n, country)
24
if err != nil {
25
return nil, err
26
}
27
28
res = &Number{
29
Valid: phonenumbers.IsValidNumber(num),
30
RawLocal: FormatNumber(phonenumbers.Format(num, phonenumbers.NATIONAL)),
31
Local: phonenumbers.Format(num, phonenumbers.NATIONAL),
32
E164: phonenumbers.Format(num, phonenumbers.E164),
33
International: FormatNumber(phonenumbers.Format(num, phonenumbers.E164)),
34
CountryCode: num.GetCountryCode(),
35
Country: country,
36
Carrier: num.GetPreferredDomesticCarrierCode(),
37
}
38
39
return res, nil
40
}
41
42