Last active
October 8, 2020 21:33
-
-
Save buddyeorl/92126eaef81fe7ce9e8c077e68f39d3b to your computer and use it in GitHub Desktop.
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
//file path related imports | |
const path = require('path'); | |
const fs = require('fs'); | |
const util = require('util'); | |
//read file | |
const readline = require('readline'); | |
let zipCodes = {} | |
//census file to js object to file | |
const rawToZipCodeObj= async() =>{ | |
const fileStream = fs.createReadStream('./raw_zcta_cities_geoid_to_zipcodes.txt'); | |
const rl = readline.createInterface({ | |
input: fileStream, | |
crlfDelay: Infinity | |
}); | |
// Note: the crlfDelay option to recognize all instances of CR LF | |
for (const line of rl) { | |
// Each line in raw_zcta_cities_geoid_to_zipcodes.txt will be successively available here as `line`. | |
str = line.trim().split(',') | |
console.log(`Line from file: ${line}`); | |
console.log(`Line from file: ${str}`); | |
//add object keys with the first column value in the text file | |
zipCodes[str[0]] = { | |
state: { code: str[1] }, | |
// here I handle multiple cities that belong to same zipcode | |
place: (zipCodes[str[0]] && zipCodes[str[0]].place) ? [...zipCodes[str[0]].place, str[2]] : [str[2]], | |
geoID: (zipCodes[str[0]] && zipCodes[str[0]].geoID) ? [...zipCodes[str[0]].geoID, str[4]] : [str[4]], | |
} | |
} | |
//write the JSON Parsed Zipcodes Object to the fill allZipCodes | |
fs.writeFileSync('./allZipCodes.js', 'module.exports.allZipCodes = ' + util.inspect(JSON.stringify(zipCodes)), 'utf-8') | |
} | |
rawToZipCodeObj(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment