Created
April 17, 2020 19:18
-
-
Save deckarts/210235bf5c2d1ce8cfa16d44a0eda7e0 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 {StringStream} = require('scramjet'); | |
const request = require('request'); | |
// requires two arguments (county and state) which must be wrapped in quotes if a phrase i.e. 'new york city' 'new york' | |
let counter = 0; | |
let death = 0; | |
let cases = 0; | |
let args = process.argv; | |
do { | |
args.shift(); | |
counter++; | |
} while (counter < 2); | |
counter = 0; | |
// fetch csv (via request, not actually fetch) | |
request.get('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv') | |
// pass to stream | |
.pipe(new StringStream()) | |
// parse into objects | |
.CSVParse() | |
// handle incoming rows | |
.consume(obj => { | |
// log header | |
if (counter === 0) { | |
let header = ''; | |
obj.forEach(elem => header += elem + ' '); | |
console.log(header); } | |
counter++; | |
// log rows matching criteria | |
if ( | |
args.length >= 2 | |
&& obj[1].toLowerCase() === args[0].toLowerCase() // match county | |
&& obj[2].toLowerCase() === args[1].toLowerCase() // match state | |
) { | |
// build result string | |
let result = ''; | |
obj.forEach(elem => result += elem + ' '); | |
let caseItem = obj[obj.length -2]; | |
if (caseItem !== 0 && cases !== 0) { | |
diffc = caseItem - cases; | |
centc = Math.round(diffc / cases * 100); | |
if (centc > 0) { | |
result += 'Cases: ' + Math.round(diffc / cases * 100) + '% '; | |
} | |
} | |
cases = caseItem; | |
let lastItem = obj[obj.length -1]; | |
if (lastItem !== 0 && death !== 0) { | |
diffd = lastItem - death; | |
centd = Math.round(diffd / death * 100); | |
if (centd > 0) { | |
result += 'Death: ' + Math.round(diffd / death * 100) + '%'; | |
} | |
} | |
death = lastItem; | |
// log result | |
console.log(result); | |
} | |
}) | |
// .then(() => console.log('all done')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment