forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_abbr.rb
More file actions
95 lines (86 loc) · 2.53 KB
/
Copy pathstate_abbr.rb
File metadata and controls
95 lines (86 loc) · 2.53 KB
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
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/object'
# Returns abbr as an uppercase symbol, stripping whitespace.
def get_uppercase_symbol(abbr)
return abbr.to_s.strip.upcase.to_sym
end
# Returns the state name associated with the state abbreviation abbr.
# abbr: A two character (uppercase or lowercase) symbol or string.
# include_dc: (default: false) Whether to include Washington DC as a state.
def get_us_state_from_abbr(abbr, include_dc = false)
abbr = get_uppercase_symbol(abbr)
return include_dc ? STATE_ABBR_WITH_DC_HASH[abbr] : STATE_ABBR_HASH[abbr]
end
# Returns the abbreviation for the supplied state name
# @param [String|Symbol] name - full state name, e.g. "Washington"
# @param [Boolean] include_dc - (default: false) Whether to include Washington DC as a state.
# @returns [String] state abbreviation, or nil
def get_us_state_abbr_from_name(name, include_dc = false)
titleized_name = name.to_s.strip.titleize
# special case for DC
titleized_name = 'Washington DC' if titleized_name == 'Washington Dc'
abbr_sym = include_dc ? STATE_ABBR_WITH_DC_HASH.key(titleized_name) : STATE_ABBR_HASH.key(titleized_name)
return abbr_sym.try(&:to_s)
end
# Returns whether the abbreviation is a state (including Washington DC)
# abbreviation.
def us_state_abbr?(abbr, include_dc = false)
return !get_us_state_from_abbr(abbr, include_dc).nil?
end
# Returns the entire list of states (including Washington DC)
def get_all_states_with_dc
return STATE_ABBR_WITH_DC_HASH.sort_by {|_code, name| name}
end
STATE_ABBR_HASH = {
AL: 'Alabama',
AK: 'Alaska',
AZ: 'Arizona',
AR: 'Arkansas',
CA: 'California',
CO: 'Colorado',
CT: 'Connecticut',
DE: 'Delaware',
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',
}.freeze
STATE_ABBR_WITH_DC_HASH = STATE_ABBR_HASH.merge({DC: 'Washington DC'})