-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone_number.go
175 lines (147 loc) · 4.95 KB
/
phone_number.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Package phonenumber used to clean up NANP (North American Numbering Plan) phone numbers.
package phonenumber
import (
"errors"
"regexp"
)
// ErrNumOfDigits is an error used when the phone number has less than 10 digits.
var ErrNumOfDigits = errors.New("incorrect number of digits")
// ErrElevenNotStartWithOne is an error used when the phone number has 11 digits and it doesn't start with one.
var ErrElevenNotStartWithOne = errors.New("11 digits must start with 1")
// ErrTooManyNumbers is an error used when the phone number has more than 11 digits.
var ErrTooManyNumbers = errors.New("more than 11 digits")
// ErrContainsLetters is an error used when the phone number has letters in it.
var ErrContainsLetters = errors.New("letters not permitted")
// ErrContainsPunctuations is an error used when the phone number has punctuation in it.
var ErrContainsPunctuations = errors.New("punctuations not permitted")
// ErrAreaCodeStartsWithZero is an error used when area codes start with zero.
var ErrAreaCodeStartsWithZero = errors.New("area code cannot start with zero")
// ErrAreaCodeStartsWithOne is an error used when area codes start with one.
var ErrAreaCodeStartsWithOne = errors.New("area code cannot start with one")
// ErrExchangeCodeStartsWithZero is an error used when exchange codes start with zero.
var ErrExchangeCodeStartsWithZero = errors.New("exchange code cannot start with zero")
// ErrExchangeCodeStartsWithOne is an error used when exchange codes start with one.
var ErrExchangeCodeStartsWithOne = errors.New("exchange code cannot start with one")
// ValidateInput returns an error if the phone number has invalid data in it.
func ValidateInput(phoneNumber string) error {
// Check the string for invalid characters.
// Pointless since we can easily just remove them.
reStrPunct := `[\]\[\|:;><,?/\\"'!@#$%&*^]`
match, e := regexp.MatchString(reStrPunct, phoneNumber)
if e != nil {
panic(e)
}
if match {
return ErrContainsPunctuations
}
// Check to see if the phone number has letters in it.
reStrLetters := `[[:alpha:]]`
match, e = regexp.MatchString(reStrLetters, phoneNumber)
if e != nil {
panic(e)
}
if match {
return ErrContainsLetters
}
// Clean up the string.
reStr := `([[:space:]]|[[:punct:]]|[[:alpha:]]|[[:cntrl:]])`
re, e := regexp.Compile(reStr)
if e != nil {
panic(e)
}
digits := re.ReplaceAllString(phoneNumber, ``)
// Check the lenght of the remaining string.
if len(digits) < 10 {
return ErrNumOfDigits
}
if len(digits) > 11 {
return ErrTooManyNumbers
}
// If it's an eleven digit number, make sure it starts with 1.
if len(digits) == 11 && string(digits[0]) != "1" {
return ErrElevenNotStartWithOne
}
// Check the starting digit of the area code.
reStr = `^[[:digit:]]?([[:digit:]])[[:digit:]]{9}$`
re, e = regexp.Compile(reStr)
if e != nil {
panic(e)
}
digit := re.ReplaceAllString(digits, `$1`)
if digit == "0" {
return ErrAreaCodeStartsWithZero
}
if digit == "1" {
return ErrAreaCodeStartsWithOne
}
// Check the starting digit of the exchange code.
reStr = `^[[:digit:]]?[[:digit:]]{3}([[:digit:]])[[:digit:]]{6}$`
re, e = regexp.Compile(reStr)
if e != nil {
panic(e)
}
digit = re.ReplaceAllString(digits, `$1`)
if digit == "0" {
return ErrExchangeCodeStartsWithZero
}
if digit == "1" {
return ErrExchangeCodeStartsWithOne
}
// No errors found.
return nil
}
// Number returns just the digits of the passed phone number.
func Number(phoneNumber string) (string, error) {
// Validate the input.
eVal := ValidateInput(phoneNumber)
if eVal != nil {
return "", eVal
}
// Extract all the digits from the number.
reStr := `([[:space:]]|[[:punct:]]|[[:alpha:]]|[[:cntrl:]])`
re, e := regexp.Compile(reStr)
if e != nil {
panic(e)
}
digits := re.ReplaceAllString(phoneNumber, ``)
// Return the 10-dgit number (minus the contry code).
if len(digits) == 11 {
return digits[1:], nil
}
// Return the 10-dgit number.
return digits, nil
}
// AreaCode returns the area code from a phone number.
func AreaCode(phoneNumber string) (string, error) {
// Get the digits from the input and input validation.
digits, eNum := Number(phoneNumber)
if eNum != nil {
return "", eNum
}
// Get the first digit from the area code.
reStr := `^([[:digit:]]?)([[:digit:]]{3})([[:digit:]]{7})$`
re, e := regexp.Compile(reStr)
if e != nil {
panic(e)
}
areaCode := re.ReplaceAllString(digits, `$2`)
// Return the area code without an error.
return areaCode, nil
}
// Format returns a pretty phone number.
func Format(phoneNumber string) (string, error) {
// Get the digits from the input and input validation.
digits, eNum := Number(phoneNumber)
if eNum != nil {
return "", eNum
}
// Get the phone number groups and format the number.
reStr := `^([[:digit:]]?)([[:digit:]]{3})([[:digit:]]{3})([[:digit:]]{4})$`
re, e := regexp.Compile(reStr)
if e != nil {
panic(e)
}
fmtStr := re.ReplaceAllString(digits, `(${2}) ${3}-${4}`)
// Return the formatted string without an error code.
return fmtStr, nil
}