Skip to content

Instantly share code, notes, and snippets.

@ariedro
Last active June 18, 2020 23:35
Show Gist options
  • Save ariedro/3388966c2057f9101b0e39f9438a603e to your computer and use it in GitHub Desktop.
Save ariedro/3388966c2057f9101b0e39f9438a603e to your computer and use it in GitHub Desktop.
const rp = require("request-promise");
const csv = require("csvtojson/v2");
const REPO_URL =
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
const limit = process.argv[2];
const offset = process.argv[3] || 0;
if (!limit || isNaN(limit) || limit < 1)
return console.log(
"Usage: npm run start [number limit] [optional number offset]"
);
Array.prototype.cut = function(cutLength, cutOffset) {
let result = this.splice(this.length - cutLength - cutOffset, Infinity);
if (cutOffset) result = result.splice(0, cutLength);
return result;
};
Number.prototype.divideBy = function(divisor) {
return this / divisor;
};
rp(REPO_URL)
.then((res) => csv().fromString(res))
.then((jsonObj) =>
Object.values(jsonObj.find((res) => res["Country/Region"] === "Argentina"))
.splice(4)
.map((_e, i, a) => (i ? a[i] - a[i - 1] : 0))
.map((_e, i, a) => (i ? a[i] / a[i - 1] : 0))
.cut(limit, offset)
.reduce((a, c) => a + c, 0)
.divideBy(limit)
)
.then(console.log)
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment