-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
147 lines (125 loc) · 5.23 KB
/
index.js
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
// Local Includes
var STATES_DATA = require(__dirname + '/statesData.js');
// System Includes
var NODE_RATIFY = require('node-ratify');
var US_STATE_NAME_TO_CODE = STATES_DATA['US_STATE_NAME_TO_CODE'],
US_STATE_CODE_TO_NAME = { },
US_INHABITED_TERRITORIES_NAME_TO_CODE = STATES_DATA['US_INHABITED_TERRITORIES_NAME_TO_CODE'],
US_INHABITED_TERRITORIES_CODE_TO_NAME = { },
US_STATE_NAME_TO_CAPITAL = STATES_DATA['US_STATE_NAME_TO_CAPITAL'],
US_CAPITAL_TO_STATE_NAME = { },
US_INHABITED_TERRITORIES_NAME_TO_CAPITAL = STATES_DATA['US_INHABITED_TERRITORIES_NAME_TO_CAPITAL'],
US_CAPITAL_TO_INHABITED_TERRITORY_NAME = { };
var STATE_NAME_STRICT = [ ],
STATE_NAME_ALL = [ ],
STATE_CODE_STRICT = [ ],
STATE_CODE_ALL = [ ];
//
// Create reverse mapping
//
function _reverseAndPrecompute () {
STATE_NAME_STRICT = Object.keys(US_STATE_NAME_TO_CODE);
var territoryKeys = Object.keys(US_INHABITED_TERRITORIES_NAME_TO_CODE);
(STATE_NAME_STRICT || []).forEach(function(stateName) {
US_STATE_CODE_TO_NAME[US_STATE_NAME_TO_CODE[stateName]] = stateName;
US_CAPITAL_TO_STATE_NAME[US_STATE_NAME_TO_CAPITAL[stateName]] = stateName;
});
(territoryKeys || []).forEach(function(territoryName) {
US_INHABITED_TERRITORIES_CODE_TO_NAME[US_INHABITED_TERRITORIES_NAME_TO_CODE[territoryName]] = territoryName;
US_CAPITAL_TO_INHABITED_TERRITORY_NAME[US_INHABITED_TERRITORIES_NAME_TO_CAPITAL[territoryName]] = territoryName;
});
// Let us precompute now
STATE_CODE_STRICT = Object.keys(US_STATE_CODE_TO_NAME);
STATE_NAME_ALL = (STATE_NAME_STRICT || []).concat(territoryKeys);
STATE_CODE_ALL = (STATE_CODE_STRICT || []).concat(Object.keys(US_INHABITED_TERRITORIES_CODE_TO_NAME));
};
function _toTitleCase(str) {
if (str && NODE_RATIFY.isString(str)) {
str = str.trim();
if (str.length > 2)
return str.replace(/\b\w+/g, function(txt) {
if (txt.toLowerCase() === 'of') {
return 'of';
}
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
else
return str.toUpperCase();
}
return null;
}
//
// Is given state code or state name is a valid state
//
function isUSAState (state, strict) {
state = state && NODE_RATIFY.isString(state) ? _toTitleCase(state) : null;
var topLevel = (US_STATE_CODE_TO_NAME[state] || US_STATE_NAME_TO_CODE[state]);
if(strict) {
return topLevel ? true : false;
} else {
return (US_INHABITED_TERRITORIES_CODE_TO_NAME[state] || US_INHABITED_TERRITORIES_NAME_TO_CODE[state] || topLevel) ? true : false;
}
};
//
// Get all the USPS code
//
function getUSPSCodes(strict) {
return strict ? STATE_CODE_STRICT : STATE_CODE_ALL;
};
//
// Get name of all the states
//
function getStates(strict) {
return strict ? STATE_NAME_STRICT : STATE_NAME_ALL;
};
//
// Given a state USPS Code or state capital it finds the state name
//
function getStateName(searchKey, strict) {
searchKey = searchKey && NODE_RATIFY.isString(searchKey) ? _toTitleCase(searchKey) : null;
var topLevel = (US_STATE_CODE_TO_NAME[searchKey] || US_CAPITAL_TO_STATE_NAME[searchKey]);
if(strict) {
return topLevel;
} else {
return (US_INHABITED_TERRITORIES_CODE_TO_NAME[searchKey] || US_CAPITAL_TO_INHABITED_TERRITORY_NAME[searchKey] || topLevel);
}
};
//
// Given a state name or state capital it finds the USPS Code
//
function getUSPSCode(searchKey, strict) {
searchKey = searchKey && NODE_RATIFY.isString(searchKey) ? _toTitleCase(searchKey) : null;
var topLevel = (US_STATE_NAME_TO_CODE[searchKey] || US_STATE_NAME_TO_CODE[US_CAPITAL_TO_STATE_NAME[searchKey]]);
if(strict) {
return topLevel;
} else {
searchKey = (searchKey === 'HagåTñA') ? 'Hagatna' : searchKey;
return (US_INHABITED_TERRITORIES_NAME_TO_CODE[searchKey] ||
US_INHABITED_TERRITORIES_NAME_TO_CODE[US_CAPITAL_TO_INHABITED_TERRITORY_NAME[searchKey]] ||
topLevel);
}
};
//
// Given a state name or state USPS code it finds the state capital
//
function getStateCapital(searchKey, strict) {
searchKey = searchKey && NODE_RATIFY.isString(searchKey) ? _toTitleCase(searchKey) : null;
var topLevel = (US_STATE_NAME_TO_CAPITAL[searchKey] || US_STATE_NAME_TO_CAPITAL[US_STATE_CODE_TO_NAME[searchKey]]);
if(strict) {
return topLevel;
} else {
return (US_INHABITED_TERRITORIES_NAME_TO_CAPITAL[searchKey] ||
US_INHABITED_TERRITORIES_NAME_TO_CAPITAL[US_INHABITED_TERRITORIES_CODE_TO_NAME[searchKey]] ||
topLevel);
}
};
// Let us create state abbreviation and state name mapping
_reverseAndPrecompute();
exports = module.exports = {
isUSAState : isUSAState,
getStates : getStates,
getUSPSCodes : getUSPSCodes,
getStateName : getStateName,
getUSPSCode : getUSPSCode,
getStateCapital : getStateCapital
};