-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharea_codes.go
191 lines (164 loc) · 4.94 KB
/
area_codes.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package usps
import (
"bufio"
"strconv"
"strings"
"sync"
"git.tcp.direct/kayos/common/squish"
"github.com/google/shlex"
"github.com/yunginnanet/usps/internal/data"
)
const AreaCodeLength = 3
// AreaCode is a struct representing an area code.
type AreaCode struct {
Code, State, City, Country string
Latitude, Longitude float64
}
var (
areaCodeSetupOnce = &sync.Once{}
areaCodeToAreaCodes = make(map[string][]*AreaCode)
intAreaCodeToAreaCodes = make(map[int][]*AreaCode)
intAreaCodeToState = make(map[int]string)
areaCodeToState = make(map[string]string)
stateToAreaCodes = make(map[string][]*AreaCode)
cityToAreaCodes = make(map[string][]*AreaCode)
)
func resetAllAreaCodes() {
areaCodeToAreaCodes = make(map[string][]*AreaCode)
intAreaCodeToAreaCodes = make(map[int][]*AreaCode)
intAreaCodeToState = make(map[int]string)
areaCodeToState = make(map[string]string)
stateToAreaCodes = make(map[string][]*AreaCode)
cityToAreaCodes = make(map[string][]*AreaCode)
areaCodeSetupOnce = &sync.Once{}
}
var cleaner = strings.NewReplacer(
"'", "",
"\"", "", "-", "", ",",
"", ".", "", "+", "",
"*", "", "#", "", "(",
"", ")", "", "!", "",
)
func clean(s string) string {
return cleaner.Replace(
strings.ToUpper(
strings.TrimSpace(s),
),
)
}
func areaCodeInit() {
var dat string
// dataset is a constant, cannot fail
// so we do a lot of error yeeting here
dat, _ = squish.UnpackStr(data.XAreacodedata)
xerox := bufio.NewScanner(strings.NewReader(dat))
for xerox.Scan() {
var tokens []string
tokens, _ = shlex.Split(strings.ReplaceAll(xerox.Text(), ",", " "))
latitude, _ := strconv.ParseFloat(tokens[4], 32)
longitude, _ := strconv.ParseFloat(tokens[5], 32)
acode := &AreaCode{
Code: clean(tokens[0]), City: clean(tokens[1]),
State: clean(tokens[2]), Country: clean(tokens[3]),
Latitude: latitude, Longitude: longitude,
}
intAc, _ := strconv.Atoi(acode.Code)
intAreaCodeToAreaCodes[intAc] = append(intAreaCodeToAreaCodes[intAc], acode)
intAreaCodeToState[intAc] = acode.State
areaCodeToAreaCodes[acode.Code] = append(areaCodeToAreaCodes[acode.Code], acode)
stateToAreaCodes[acode.State] = append(stateToAreaCodes[acode.State], acode)
cityToAreaCodes[acode.City] = append(cityToAreaCodes[acode.City], acode)
areaCodeToState[acode.Code] = acode.State
}
// if we haven't panic'd at this point then our dataset is good
}
func InitAreaCodes() {
areaCodeSetupOnce.Do(areaCodeInit)
}
// LookupAreaCode returns a slice of AreaCode types that match the given area code.
func LookupAreaCode(code string) ([]*AreaCode, bool) {
InitAreaCodes()
res, ok := areaCodeToAreaCodes[code]
if !ok {
res, ok = areaCodeToAreaCodes[clean(code)]
}
return res, ok
}
// LookupStateAreaCodes returns a slice of AreaCode types that match the given state.
func LookupStateAreaCodes(state string) ([]*AreaCode, bool) {
InitAreaCodes()
if len(state) == 2 {
state = LongHand(state)
}
res, ok := stateToAreaCodes[clean(state)]
return res, ok
}
// LookupCityAreaCodes returns a slice of AreaCode types that match the given city.
// Note! This is not a perfect match. If a city is not found, it will attempt to find a city that contains the query..
func LookupCityAreaCodes(city string) ([]*AreaCode, bool) {
InitAreaCodes()
city = clean(city)
res, ok := cityToAreaCodes[city]
if !ok {
for k, v := range cityToAreaCodes {
if strings.Contains(k, city) {
res = append(res, v...)
ok = true
}
}
}
return res, ok
}
// PhoneNumberState returns the state that the given phone number is in.
// This is probably the most useful function in this package, and the one you're looking for.
// This function will return an empty string if the number is invalid.
func PhoneNumberState(number string) string {
InitAreaCodes()
ac := PhoneNumberAreaCode(clean(number))
if as, ok := areaCodeToState[ac]; ok {
return clean(as)
}
return ""
}
func PhoneNumberCity(number string) string {
InitAreaCodes()
ac := PhoneNumberAreaCode(clean(number))
a, ok := LookupAreaCode(ac)
if len(a) > 0 && ok {
return clean(a[0].City)
}
return ""
}
// GetAreaCodeInt returns the area code of the given phone number or string as an int. If invalid, returns 0.
func GetAreaCodeInt(number string) int {
switch {
case len(number) > AreaCodeLength:
number = PhoneNumberAreaCode(number)
case len(number) < AreaCodeLength:
return 0
case len(number) == AreaCodeLength:
//
}
n, ok := strconv.Atoi(number)
if ok != nil {
return 0
}
return n
}
// PhoneNumberAreaCode returns the area code of the given phone number.
// This function will return an empty string if the number is invalid or isn't a known USA area code.
func PhoneNumberAreaCode(number string) string {
if len(number) < 10 {
return ""
}
if len(number) > 10 {
number = clean(strings.ReplaceAll(number, " ", ""))
}
if len(number) == 11 && string(number[0]) == "1" {
number = number[1:]
}
if len(number) != 10 {
return ""
}
return number[:AreaCodeLength]
}