Initial commit: runnable Ask a Plumber First server.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package geo
|
||||
|
||||
import "strings"
|
||||
|
||||
// States is the US states + DC allowlist (code -> name).
|
||||
var States = []struct {
|
||||
Code string
|
||||
Name string
|
||||
}{
|
||||
{"AL", "Alabama"}, {"AK", "Alaska"}, {"AZ", "Arizona"}, {"AR", "Arkansas"}, {"CA", "California"},
|
||||
{"CO", "Colorado"}, {"CT", "Connecticut"}, {"DE", "Delaware"}, {"DC", "District of Columbia"},
|
||||
{"FL", "Florida"}, {"GA", "Georgia"}, {"HI", "Hawaii"}, {"ID", "Idaho"}, {"IL", "Illinois"},
|
||||
{"IN", "Indiana"}, {"IA", "Iowa"}, {"KS", "Kansas"}, {"KY", "Kentucky"}, {"LA", "Louisiana"},
|
||||
{"ME", "Maine"}, {"MD", "Maryland"}, {"MA", "Massachusetts"}, {"MI", "Michigan"}, {"MN", "Minnesota"},
|
||||
{"MS", "Mississippi"}, {"MO", "Missouri"}, {"MT", "Montana"}, {"NE", "Nebraska"}, {"NV", "Nevada"},
|
||||
{"NH", "New Hampshire"}, {"NJ", "New Jersey"}, {"NM", "New Mexico"}, {"NY", "New York"},
|
||||
{"NC", "North Carolina"}, {"ND", "North Dakota"}, {"OH", "Ohio"}, {"OK", "Oklahoma"}, {"OR", "Oregon"},
|
||||
{"PA", "Pennsylvania"}, {"RI", "Rhode Island"}, {"SC", "South Carolina"}, {"SD", "South Dakota"},
|
||||
{"TN", "Tennessee"}, {"TX", "Texas"}, {"UT", "Utah"}, {"VT", "Vermont"}, {"VA", "Virginia"},
|
||||
{"WA", "Washington"}, {"WV", "West Virginia"}, {"WI", "Wisconsin"}, {"WY", "Wyoming"},
|
||||
}
|
||||
|
||||
var codes map[string]struct{}
|
||||
|
||||
func init() {
|
||||
codes = make(map[string]struct{}, len(States))
|
||||
for _, s := range States {
|
||||
codes[s.Code] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// ValidState reports whether state is empty or a known US code.
|
||||
func ValidState(state string) bool {
|
||||
state = strings.ToUpper(strings.TrimSpace(state))
|
||||
if state == "" {
|
||||
return true
|
||||
}
|
||||
_, ok := codes[state]
|
||||
return ok
|
||||
}
|
||||
|
||||
// NormalizeState returns "" or an uppercase 2-letter code.
|
||||
func NormalizeState(state string) string {
|
||||
return strings.ToUpper(strings.TrimSpace(state))
|
||||
}
|
||||
|
||||
// StateName returns the full name for a US state code, or "" if unknown.
|
||||
func StateName(code string) string {
|
||||
code = NormalizeState(code)
|
||||
for _, s := range States {
|
||||
if s.Code == code {
|
||||
return s.Name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user