-
-
Save swingley/1ee06f28069bb4eec5343f1f37c84c6c 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
const fs = require('fs') | |
const stream = require('stream') | |
const es = require('event-stream') | |
const parse = require('csv-parse') | |
const stringify = require('csv-stringify') | |
const fipsLookup = JSON.parse(fs.readFileSync('data/fips_lookup_by_state.json')) | |
const convertStream = new stream.Transform({ | |
objectMode: true, | |
transform: async (row, encoding, callback) => { | |
try { | |
const location = row[0] | |
let fips | |
row[1] = row[1].replace('%', '') | |
const pieces = location.split('(') | |
if (pieces.length > 1) { | |
const county = pieces[0].trim() | |
const state = pieces[1].replace(')', '') | |
fips = fipsLookup[state][county] | |
row.push(fips) | |
} else { | |
console.log('\nUH OH', row) | |
} | |
if (fips) { | |
callback(null, row) | |
} else { | |
callback() | |
} | |
} catch (err) { | |
callback(err) | |
} | |
} | |
}) | |
function go() { | |
const rawPath = 'PP-Data-Store-HPI-Impact-of-Tax-Plan.csv' | |
const rawStream = fs.createReadStream(rawPath, { flags: 'r', encoding: 'utf8' }) | |
const fipsPath = `${rawPath.replace('.csv', '')}-with-FIPS.csv` | |
const fipsStream = fs.createWriteStream(fipsPath, { encoding: 'utf8' }) | |
rawStream | |
.pipe(es.split(/(\r?\n)/)) | |
.pipe(parse()) | |
.pipe(convertStream) | |
.pipe(stringify()) | |
.pipe(fipsStream) | |
.on('end', () => console.log('finished')) | |
.on('error', err => console.log('error adding FIPS codes', error)) | |
} | |
go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment