-
Notifications
You must be signed in to change notification settings - Fork 20
/
misc.go
49 lines (40 loc) · 932 Bytes
/
misc.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
43
44
45
46
47
48
49
package utils
import (
"reflect"
"github.com/nyaruka/phonenumbers"
)
// IsNil returns whether the given object is nil or an interface to a nil
func IsNil(v interface{}) bool {
// if v doesn't have a type or value then v == nil
if v == nil {
return true
}
val := reflect.ValueOf(v)
// if v is a typed nil pointer then v != nil but the value is nil
if val.Kind() == reflect.Ptr {
return val.IsNil()
}
return false
}
// MaxInt returns the maximum of two integers
func MaxInt(x, y int) int {
if x > y {
return x
}
return y
}
// MinInt returns the minimum of two integers
func MinInt(x, y int) int {
if x < y {
return x
}
return y
}
// DeriveCountryFromTel attempts to derive a country code (e.g. RW) from a phone number
func DeriveCountryFromTel(number string) string {
parsed, err := phonenumbers.Parse(number, "")
if err != nil {
return ""
}
return phonenumbers.GetRegionCodeForNumber(parsed)
}