-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.go
435 lines (368 loc) · 9.35 KB
/
states.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package postal
import (
"fmt"
"strconv"
)
type (
// State defines US State short code.
State string
// StateName defines full name of the US State.
StateName string
)
const (
Alaska State = "AK"
AlaskaName StateName = "Alaska"
Alabama State = "AL"
AlabamaName StateName = "Alabama"
Arkansas State = "AR"
ArkansasName StateName = "Arkansas"
Arizona State = "AZ"
ArizonaName StateName = "Arizona"
California State = "CA"
CaliforniaName StateName = "California"
Colorado State = "CO"
ColoradoName StateName = "Colorado"
Connecticut State = "CT"
ConnecticutName StateName = "Connecticut"
WashingtonDC State = "DC"
WashingtonDCName StateName = "Washington DC"
Delaware State = "DE"
DelawareName StateName = "Delaware"
Florida State = "FL"
FloridaName StateName = "Florida"
Georgia State = "GA"
GeorgiaName StateName = "Georgia"
Hawaii State = "HI"
HawaiiName StateName = "Hawaii"
Iowa State = "IA"
IowaName StateName = "Iowa"
Idaho State = "ID"
IdahoName StateName = "Idaho"
Illinois State = "IL"
IllinoisName StateName = "Illinois"
Indiana State = "IN"
IndianaName StateName = "Indiana"
Kansas State = "KS"
KansasName StateName = "Kansas"
Kentucky State = "KY"
KentuckyName StateName = "Kentucky"
Louisiana State = "LA"
LouisianaName StateName = "Louisiana"
Massachusetts State = "MA"
MassachusettsName StateName = "Massachusetts"
Maryland State = "MD"
MarylandName StateName = "Maryland"
Maine State = "ME"
MaineName StateName = "Maine"
Michigan State = "MI"
MichiganName StateName = "Michigan"
Minnesota State = "MN"
MinnesotaName StateName = "Minnesota"
Missouri State = "MO"
MissouriName StateName = "Missouri"
Mississippi State = "MS"
MississippiName StateName = "Mississippi"
Montana State = "MT"
MontanaName StateName = "Montana"
NorthCarolina State = "NC"
NorthCarolinaName StateName = "North Carolina"
NorthDakota State = "ND"
NorthDakotaName StateName = "North Dakota"
Nebraska State = "NE"
NebraskaName StateName = "Nebraska"
NewHampshire State = "NH"
NewHampshireName StateName = "New Hampshire"
NewJersey State = "NJ"
NewJerseyName StateName = "New Jersey"
NewMexico State = "NM"
NewMexicoName StateName = "New Mexico"
Nevada State = "NV"
NevadaName StateName = "Nevada"
NewYork State = "NY"
NewYorkName StateName = "New York"
Ohio State = "OH"
OhioName StateName = "Ohio"
Oklahoma State = "OK"
OklahomaName StateName = "Oklahoma"
Oregon State = "OR"
OregonName StateName = "Oregon"
Pennsylvania State = "PA"
PennsylvaniaName StateName = "Pennsylvania"
RhodeIsland State = "RI"
RhodeIslandName StateName = "Rhode Island"
SouthCarolina State = "SC"
SouthCarolinaName StateName = "South Carolina"
SouthDakota State = "SD"
SouthDakotaName StateName = "South Dakota"
Tennessee State = "TN"
TennesseeName StateName = "Tennessee"
Texas State = "TX"
TexasName StateName = "Texas"
Utah State = "UT"
UtahName StateName = "Utah"
Virginia State = "VA"
VirginiaName StateName = "Virginia"
Vermont State = "VT"
VermontName StateName = "Vermont"
Washington State = "WA"
WashingtonName StateName = "Washington"
Wisconsin State = "WI"
WisconsinName StateName = "Wisconsin"
WestVirginia State = "WV"
WestVirginiaName StateName = "West Virginia"
Wyoming State = "WY"
WyomingName StateName = "Wyoming"
)
// String returns string version of State.
func (s State) String() string {
return string(s)
}
// String returns string version of StateName.
func (s StateName) String() string {
return string(s)
}
// Is will compare the state.
func (s State) Is(state State) bool {
return s == state
}
// Name returns full state name from state code.
//
//nolint:gocyclo,cyclop,funlen // allow higher complexity
func (s State) Name() StateName {
switch s {
case Alaska:
return AlaskaName
case Alabama:
return AlabamaName
case Arkansas:
return ArkansasName
case Arizona:
return ArizonaName
case California:
return CaliforniaName
case Colorado:
return ColoradoName
case Connecticut:
return ConnecticutName
case WashingtonDC:
return WashingtonDCName
case Delaware:
return DelawareName
case Florida:
return FloridaName
case Georgia:
return GeorgiaName
case Hawaii:
return HawaiiName
case Iowa:
return IowaName
case Idaho:
return IdahoName
case Illinois:
return IllinoisName
case Indiana:
return IndianaName
case Kansas:
return KansasName
case Kentucky:
return KentuckyName
case Louisiana:
return LouisianaName
case Massachusetts:
return MassachusettsName
case Maryland:
return MarylandName
case Maine:
return MaineName
case Michigan:
return MichiganName
case Minnesota:
return MinnesotaName
case Missouri:
return MissouriName
case Mississippi:
return MississippiName
case Montana:
return MontanaName
case NorthCarolina:
return NorthCarolinaName
case NorthDakota:
return NorthDakotaName
case Nebraska:
return NebraskaName
case NewHampshire:
return NewHampshireName
case NewJersey:
return NewJerseyName
case NewMexico:
return NewMexicoName
case Nevada:
return NevadaName
case NewYork:
return NewYorkName
case Ohio:
return OhioName
case Oklahoma:
return OklahomaName
case Oregon:
return OregonName
case Pennsylvania:
return PennsylvaniaName
case RhodeIsland:
return RhodeIslandName
case SouthCarolina:
return SouthCarolinaName
case SouthDakota:
return SouthDakotaName
case Tennessee:
return TennesseeName
case Texas:
return TexasName
case Utah:
return UtahName
case Virginia:
return VirginiaName
case Vermont:
return VermontName
case Washington:
return WashingtonName
case Wisconsin:
return WisconsinName
case WestVirginia:
return WestVirginiaName
case Wyoming:
return WyomingName
}
return ""
}
// FindState returns the state by postal code.
// Returns ErrStateNotFound if state was not found.
// Returns ErrInvalidCode if the provided code was not valid.
func FindState(postal string) (State, error) {
if !IsValid(postal) {
return "", fmt.Errorf("invalid code '%s': %w", postal, ErrInvalidCode)
}
code, err := strconv.ParseInt(postal, 10, 64)
if err != nil {
return "", fmt.Errorf("code is not a valid number: %w", ErrInvalidCode)
}
if code < 0 {
return "", ErrStateNotFound
}
state := getStateFromCode(code)
if state == "" {
return "", ErrStateNotFound
}
return state, nil
}
//nolint:gocyclo,gomnd,cyclop,funlen // allow higher complexity & zip codes as integers
func getStateFromCode(code int64) State {
switch {
case rng(code, 99500, 99999):
return Alaska
case rng(code, 35000, 36999):
return Alabama
case rng(code, 71600, 72999):
return Arkansas
case rng(code, 85000, 86999):
return Arizona
case rng(code, 90000, 96699):
return California
case rng(code, 80000, 81999):
return Colorado
case rng(code, 6000, 6999):
return Connecticut
case rng(code, 20000, 20599):
return WashingtonDC
case rng(code, 19700, 19999):
return Delaware
case rng(code, 32000, 34999):
return Florida
case rng(code, 30000, 31999):
return Georgia
case rng(code, 96700, 96999):
return Hawaii
case rng(code, 50000, 52999):
return Iowa
case rng(code, 83200, 83999):
return Idaho
case rng(code, 60000, 62999):
return Illinois
case rng(code, 46000, 47999):
return Indiana
case rng(code, 66000, 67999):
return Kansas
case rng(code, 40000, 42999):
return Kentucky
case rng(code, 70000, 71599):
return Louisiana
case rng(code, 1000, 2799):
return Massachusetts
case rng(code, 20600, 21999):
return Maryland
case rng(code, 3900, 4999):
return Maine
case rng(code, 48000, 49999):
return Michigan
case rng(code, 55000, 56999):
return Minnesota
case rng(code, 63000, 65999):
return Missouri
case rng(code, 38600, 39999):
return Mississippi
case rng(code, 59000, 59999):
return Montana
case rng(code, 27000, 28999):
return NorthCarolina
case rng(code, 58000, 58999):
return NorthDakota
case rng(code, 68000, 69999):
return Nebraska
case rng(code, 3000, 3899):
return NewHampshire
case rng(code, 7000, 8999):
return NewJersey
case rng(code, 87000, 88499):
return NewMexico
case rng(code, 88900, 89999):
return Nevada
case rng(code, 10000, 14999):
return NewYork
case rng(code, 43000, 45999):
return Ohio
case rng(code, 73000, 74999):
return Oklahoma
case rng(code, 97000, 97999):
return Oregon
case rng(code, 15000, 19699):
return Pennsylvania
case rng(code, 2800, 2999):
return RhodeIsland
case rng(code, 29000, 29999):
return SouthCarolina
case rng(code, 57000, 57999):
return SouthDakota
case rng(code, 37000, 38599):
return Tennessee
case rng(code, 75000, 79999) || rng(code, 88500, 88599):
return Texas
case rng(code, 84000, 84999):
return Utah
case rng(code, 22000, 24699):
return Virginia
case rng(code, 5000, 5999):
return Vermont
case rng(code, 98000, 99499):
return Washington
case rng(code, 53000, 54999):
return Wisconsin
case rng(code, 24700, 26999):
return WestVirginia
case rng(code, 82000, 83199):
return Wyoming
}
return ""
}
func rng(target, min, max int64) bool {
return target >= min && target <= max
}