-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (46 loc) · 1.88 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
'use strict';
const stateInformation = require('./data/usa_states.json');
const retrieveStateInformationConditionally = (textInput, condition= null) => {
if (typeof textInput === 'string' || textInput instanceof String){
const formattedInput = textInput.trim().toUpperCase();
if(condition){
return stateInformation.find(state => formattedInput === state[condition].toUpperCase());
}else{
return stateInformation.find(state => formattedInput === state.name.toUpperCase() || formattedInput === state.abbreviation.toUpperCase() || formattedInput === state.capital.toUpperCase());
}
}
return undefined;
};
const isValidStateAbbreviation = (stateAbbreviation) => {
return !!retrieveStateInformationConditionally(stateAbbreviation, 'abbreviation');
};
const isValidStateCapital= (stateCapital) => {
return !!retrieveStateInformationConditionally(stateCapital, 'capital');
};
const isValidStateName = (stateName) => {
return !!retrieveStateInformationConditionally(stateName, 'name');
};
const isValidStateInput = (textInput) => {
return isValidStateAbbreviation(textInput) || isValidStateName(textInput);
};
const retrieveStateInformation = (textInput) => {
const result = retrieveStateInformationConditionally(textInput);
return result ? result : null;
};
const retrieveStateCapitalByName = (stateName) => {
const result = retrieveStateInformationConditionally(stateName, 'name');
return result ? result.capital : null;
};
const retrieveStateNameByCapital = (stateCapital) => {
const result = retrieveStateInformationConditionally(stateCapital, 'capital');
return result ? result.name : null;
};
module.exports = {
isValidStateAbbreviation,
isValidStateName,
isValidStateInput,
retrieveStateInformation,
isValidStateCapital,
retrieveStateCapitalByName,
retrieveStateNameByCapital
};