Skip to content

Instantly share code, notes, and snippets.

@brizandrew
Created August 28, 2018 19:51
Show Gist options
  • Save brizandrew/e964c7d3a03865cb5e55d0ffd281de03 to your computer and use it in GitHub Desktop.
Save brizandrew/e964c7d3a03865cb5e55d0ffd281de03 to your computer and use it in GitHub Desktop.
Converts state fips codes into postal codes and back again.
/**
* Available Exports:
* @export {array} postalFipsList – list of state objects with 'FIPS' and 'postal' keys
* @export {object} fipsByPostal – dictionary with postal codes as keys and corresponding FIPS code as value
* @export {object} postalByFips – dictionary with FIPS codes as keys and corresponding postal code as value
* @export {function} fips2Postal – function to convert a fips code to a postal code.
* @export {function} postal2Fips – function to convert a postal code to a fips code.
*/
const fipsByPostal = {
'AK': '02',
'AL': '01',
'AR': '05',
'AS': '60',
'AZ': '04',
'CA': '06',
'CO': '08',
'CT': '09',
'DC': '11',
'DE': '10',
'FL': '12',
'GA': '13',
'GU': '66',
'HI': '15',
'IA': '19',
'ID': '16',
'IL': '17',
'IN': '18',
'KS': '20',
'KY': '21',
'LA': '22',
'MA': '25',
'MD': '24',
'ME': '23',
'MI': '26',
'MN': '27',
'MO': '29',
'MS': '28',
'MT': '30',
'NC': '37',
'ND': '38',
'NE': '31',
'NH': '33',
'NJ': '34',
'NM': '35',
'NV': '32',
'NY': '36',
'OH': '39',
'OK': '40',
'OR': '41',
'PA': '42',
'PR': '72',
'RI': '44',
'SC': '45',
'SD': '46',
'TN': '47',
'TX': '48',
'UT': '49',
'VA': '51',
'VI': '78',
'VT': '50',
'WA': '53',
'WI': '55',
'WV': '54',
'WY': '56',
};
const postalFipsList = Object.keys(fipsByPostal).map(postal => ({postal, FIPS: fipsByPostal[postal]}));
const convertListToDict = (list, Keykey, valueKey) => {
let output = {};
list.forEach(item => { output[item[Keykey]] = item[valueKey]; });
return output;
};
const postalByFips = convertListToDict(postalFipsList, 'FIPS', 'postal');
const fips2Postal = (fips) => postalByFips[fips];
const postal2Fips = (postal) => fipsByPostal[postal];
module.exports = {
postalFipsList,
fipsByPostal,
postalByFips,
fips2Postal,
postal2Fips,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment