-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisimei.go
77 lines (61 loc) · 1.55 KB
/
isimei.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package validatorgo
import (
"regexp"
"strconv"
)
var (
isIMEIOptsDefaultAllowHyphens bool = false
)
// IsIMEIOpts is used to configure IsIMEI
type IsIMEIOpts struct {
AllowHyphens bool
}
// A validator that checks if the string is a valid [IMEI number]. IMEI should be of format ############### or ##-######-######-#.
//
// IsIMEIOpts is a struct which can contain the keys AllowHyphens. Defaults to first format.
//
// If AllowHyphens is set to true, the validator will validate the second format.
//
// ok := validatorgo.IsIMEI("490154203237518", &IsIMEIOpts{})
// fmt.Println(ok) // true
// ok := validatorgo.IsIMEI("359043377500085", &IsIMEIOpts{})
// fmt.Println(ok) // false
//
// [IMEI number]: https://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity
func IsIMEI(str string, opts *IsIMEIOpts) bool {
if opts == nil {
opts = setIsIMEIOptsToDefault()
}
var re *regexp.Regexp
if opts.AllowHyphens {
re = regexp.MustCompile(`^\d{2}-?\d{6}-?\d{6}-?\d$`)
} else {
re = regexp.MustCompile(`^\d{15}$`)
}
if !re.MatchString(str) {
return false
}
strWithoutHyphens := stripHyphens(str)
strLen := len(strWithoutHyphens)
isSecond := false
sum := 0
for i := strLen - 1; i >= 0; i-- {
dig, _ := strconv.Atoi(string(strWithoutHyphens[i]))
if isSecond {
dbDig := dig * 2
if dbDig > 9 {
dbDig = digitSum(dbDig)
}
sum += dbDig
} else {
sum += dig
}
isSecond = !isSecond
}
return sum%10 == 0
}
func setIsIMEIOptsToDefault() *IsIMEIOpts {
return &IsIMEIOpts{
AllowHyphens: isIMEIOptsDefaultAllowHyphens,
}
}