UNPKG

fips

Version:

A lookup table for FIPS and GeoIP region codes

106 lines (97 loc) 3.65 kB
var fs = require('fs'); var path = require('path'); var standardFipsFilename = path.join(__dirname, 'fips-414.txt'); var usStatesFilename = path.join(__dirname, 'us_postal.txt'); var geoipFipsFilename = path.join(__dirname, 'geoip_fips_20120229.txt'); var shortToLong = null; var longToShort = null; // Read from text file once on module load. if (shortToLong === null || longToShort === null) { try { var _short = {}; var _long = {}; /* "geoip" data taken from http://www.maxmind.com/app/fips_include on 29 Feb 2012. Note that the reason this is necessary is that GeoIP uses ISO 3166 country codes. */ var data = fs.readFileSync(geoipFipsFilename, 'utf-8'); var lines = data.split('\n'); for (index in lines) { var line = lines[index]; var pieces = line.split(','); // We could do this, which would keep "Bristol, City of" //pieces[2] = pieces.slice(2).join(',') //var l = JSON.parse(pieces[2]); // But I think this is more useful for our purposes, since showing "Bristol, City of" // to a regular human is pretty weird. pieces[2] = pieces[2].replace(/\b"/, '').replace(/"\b/, ''); var s = pieces[0] + pieces[1]; var l = pieces[2]; _short[s] = l; _long[l] = s; } /* "standard" data taken from http://efele.net/maps/fips-10/data/fips-414.txt on 29 Feb 2012. According to http://efele.net/maps/fips-10/data/, "All the files are in UTF-8, one record per line, fields delimited by _ (underscore): FIPS code. For countries, it is of the form xx00. first fips version. The first version in which the code is present, with the same designation and names. last fips version. The last version in which the code is present, with the same designation and names. designation, English. "country" for countries, "county", "state", ... for the others designation, other language. designation, other language. designation, other language. name convential name former name" */ var data = fs.readFileSync(standardFipsFilename, 'utf-8'); var lines = data.split('\n'); for (index in lines) { var line = lines[index]; var pieces = line.split('_'); // Prefer conventional long name, then name, then former name. var shortToLongAlreadyMapped = false; for (piece = 7; piece <= 9; piece++) { if (pieces[piece] !== '') { if (!shortToLongAlreadyMapped) { _short[pieces[0]] = pieces[piece]; shortToLongAlreadyMapped = true; } _long[pieces[piece]] = pieces[0]; } } } /* This is a cheat so you can use United States state abbreviations as if they were region codes. */ var data = fs.readFileSync(usStatesFilename, 'utf-8'); var lines = data.split('\n'); for (index in lines) { var line = lines[index]; var pieces = line.split(','); var l = JSON.parse(pieces[2]); var s = pieces[0] + pieces[1]; _short[s] = l; _long[l] = s; } shortToLong = _short; longToShort = _long; } catch (error) { console.log(error); if (error['stack']) { console.log(error['stack']); } } }; module.exports.longform = function (shortform, callback) { var result = shortToLong[shortform]; if (callback) { return callback(null, result); } else { return result; } }; module.exports.shortform = function (longform, callback) { var result = longToShort[longform]; if (callback) { return callback(null, result); } else { return result; } };