Last active
April 24, 2024 22:18
-
-
Save calebgrove/c285a9510948b633aa47 to your computer and use it in GitHub Desktop.
Convert state name to abbreviation in JavaScript. There's some better solutions in the comments, so scroll down!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// There's some better solutions in the comments, so scroll down and see how other folks have improved this! | |
// USAGE: | |
// abbrState('ny', 'name'); | |
// --> 'New York' | |
// abbrState('New York', 'abbr'); | |
// --> 'NY' | |
function abbrState(input, to){ | |
var states = [ | |
['Arizona', 'AZ'], | |
['Alabama', 'AL'], | |
['Alaska', 'AK'], | |
['Arkansas', 'AR'], | |
['California', 'CA'], | |
['Colorado', 'CO'], | |
['Connecticut', 'CT'], | |
['Delaware', 'DE'], | |
['Florida', 'FL'], | |
['Georgia', 'GA'], | |
['Hawaii', 'HI'], | |
['Idaho', 'ID'], | |
['Illinois', 'IL'], | |
['Indiana', 'IN'], | |
['Iowa', 'IA'], | |
['Kansas', 'KS'], | |
['Kentucky', 'KY'], | |
['Louisiana', 'LA'], | |
['Maine', 'ME'], | |
['Maryland', 'MD'], | |
['Massachusetts', 'MA'], | |
['Michigan', 'MI'], | |
['Minnesota', 'MN'], | |
['Mississippi', 'MS'], | |
['Missouri', 'MO'], | |
['Montana', 'MT'], | |
['Nebraska', 'NE'], | |
['Nevada', 'NV'], | |
['New Hampshire', 'NH'], | |
['New Jersey', 'NJ'], | |
['New Mexico', 'NM'], | |
['New York', 'NY'], | |
['North Carolina', 'NC'], | |
['North Dakota', 'ND'], | |
['Ohio', 'OH'], | |
['Oklahoma', 'OK'], | |
['Oregon', 'OR'], | |
['Pennsylvania', 'PA'], | |
['Rhode Island', 'RI'], | |
['South Carolina', 'SC'], | |
['South Dakota', 'SD'], | |
['Tennessee', 'TN'], | |
['Texas', 'TX'], | |
['Utah', 'UT'], | |
['Vermont', 'VT'], | |
['Virginia', 'VA'], | |
['Washington', 'WA'], | |
['West Virginia', 'WV'], | |
['Wisconsin', 'WI'], | |
['Wyoming', 'WY'], | |
]; | |
if (to == 'abbr'){ | |
input = input.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); | |
for(i = 0; i < states.length; i++){ | |
if(states[i][0] == input){ | |
return(states[i][1]); | |
} | |
} | |
} else if (to == 'name'){ | |
input = input.toUpperCase(); | |
for(i = 0; i < states.length; i++){ | |
if(states[i][1] == input){ | |
return(states[i][0]); | |
} | |
} | |
} | |
} |
Thanks @davidteather, @nandorojo, @rinogo! Very helpful.
stateNameToAbbreviation.ts
export default function stateNameToAbbreviation(name: string): string {
const lowerCaseAbbreviationKey = name.trim().replace(/[^\w ]/g, "").toLowerCase(); // Trim, lowercase and remove all non-word characters with the exception of spaces
return states[lowerCaseAbbreviationKey] ?? name; // Return name if record is not found
}
const states: Record<string, string> = {
"arizona": "AZ",
"alabama": "AL",
"alaska": "AK",
"arkansas": "AR",
"california": "CA",
"colorado": "CO",
"connecticut": "CT",
"district of columbia": "DC",
"delaware": "DE",
"florida": "FL",
"georgia": "GA",
"hawaii": "HI",
"idaho": "ID",
"illinois": "IL",
"indiana": "IN",
"iowa": "IA",
"kansas": "KS",
"kentucky": "KY",
"louisiana": "LA",
"maine": "ME",
"maryland": "MD",
"massachusetts": "MA",
"michigan": "MI",
"minnesota": "MN",
"mississippi": "MS",
"missouri": "MO",
"montana": "MT",
"nebraska": "NE",
"nevada": "NV",
"new hampshire": "NH",
"new jersey": "NJ",
"new mexico": "NM",
"new york": "NY",
"north carolina": "NC",
"north dakota": "ND",
"ohio": "OH",
"oklahoma": "OK",
"oregon": "OR",
"pennsylvania": "PA",
"rhode island": "RI",
"south carolina": "SC",
"south dakota": "SD",
"tennessee": "TN",
"texas": "TX",
"utah": "UT",
"vermont": "VT",
"virginia": "VA",
"washington": "WA",
"west virginia": "WV",
"wisconsin": "WI",
"wyoming": "WY",
"american samoa": "AS",
"guam": "GU",
"northern mariana islands": "MP",
"puerto rico": "PR",
"us virgin islands": "VI",
"us minor outlying islands": "UM",
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
another ts version:
/**
*/
export default function convertUsStateAbbrAndName(input: string): string | null {
const toAbbr = input.length !== 2;
const states = [
['Alabama', 'AL'],
['Alaska', 'AK'],
['American Samoa', 'AS'],
['Arizona', 'AZ'],
['Arkansas', 'AR'],
['Armed Forces Americas', 'AA'],
['Armed Forces Europe', 'AE'],
['Armed Forces Pacific', 'AP'],
['California', 'CA'],
['Colorado', 'CO'],
['Connecticut', 'CT'],
['Delaware', 'DE'],
['District Of Columbia', 'DC'],
['Florida', 'FL'],
['Georgia', 'GA'],
['Guam', 'GU'],
['Hawaii', 'HI'],
['Idaho', 'ID'],
['Illinois', 'IL'],
['Indiana', 'IN'],
['Iowa', 'IA'],
['Kansas', 'KS'],
['Kentucky', 'KY'],
['Louisiana', 'LA'],
['Maine', 'ME'],
['Marshall Islands', 'MH'],
['Maryland', 'MD'],
['Massachusetts', 'MA'],
['Michigan', 'MI'],
['Minnesota', 'MN'],
['Mississippi', 'MS'],
['Missouri', 'MO'],
['Montana', 'MT'],
['Nebraska', 'NE'],
['Nevada', 'NV'],
['New Hampshire', 'NH'],
['New Jersey', 'NJ'],
['New Mexico', 'NM'],
['New York', 'NY'],
['North Carolina', 'NC'],
['North Dakota', 'ND'],
['Northern Mariana Islands', 'NP'],
['Ohio', 'OH'],
['Oklahoma', 'OK'],
['Oregon', 'OR'],
['Pennsylvania', 'PA'],
['Puerto Rico', 'PR'],
['Rhode Island', 'RI'],
['South Carolina', 'SC'],
['South Dakota', 'SD'],
['Tennessee', 'TN'],
['Texas', 'TX'],
['US Virgin Islands', 'VI'],
['Utah', 'UT'],
['Vermont', 'VT'],
['Virginia', 'VA'],
['Washington', 'WA'],
['West Virginia', 'WV'],
['Wisconsin', 'WI'],
['Wyoming', 'WY'],
];
// So happy that Canada and the US have distinct abbreviations
const provinces = [
['Alberta', 'AB'],
['British Columbia', 'BC'],
['Manitoba', 'MB'],
['New Brunswick', 'NB'],
['Newfoundland', 'NF'],
['Northwest Territory', 'NT'],
['Nova Scotia', 'NS'],
['Nunavut', 'NU'],
['Ontario', 'ON'],
['Prince Edward Island', 'PE'],
['Quebec', 'QC'],
['Saskatchewan', 'SK'],
['Yukon', 'YT'],
];
const regions = states.concat(provinces);
let i; // Reusable loop variable
if (toAbbr) {
input = input.replace(/\w\S*/g, function (txt: string) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
for (i = 0; i < regions.length; i++) {
if (regions[i][0] === input) {
return regions[i][1];
}
}
} else {
input = input.toUpperCase();
for (i = 0; i < regions.length; i++) {
if (regions[i][1] === input) {
return regions[i][0];
}
}
}
return null;
}