munged = pipe(
await d3.csv(
`https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv`
),
(arr) => {
const keepKeys = [`Country/Region`];
return arr
.map((row) => {
const isDateKey = (d) => d.match(/\d{1,2}\/\d{1,2}\/\d{1,2}/);
const dateKeys = Object.keys(row).filter(isDateKey);
const rest = Object.fromEntries(keepKeys.map((key) => [key, row[key]]));
return dateKeys.map((dateKey) => {
const value = row[dateKey];
const dateObject = d3.utcParse(`%m/%d/%y`)(dateKey);
const dateString = d3.utcFormat(`%Y-%m-%d`)(dateObject);
return { dateKey, dateString, value, ...rest };
});
return dateKeys;
})
.flat(2)
.map(d3.autoType)
.filter((d) => d[`Country/Region`] === `US`);
},
(arr) =>
d3
.rollups(
arr,
(arr) => d3.sum(arr, (d) => d.value),
(d) => d.dateString
)
.map(([dateString, value]) => ({ dateString, total: value }))
.sort((a, b) => d3.ascending(a[0], b[0]))
.map((d, index) => ({ ...d, index })),
(arr) => arr.map(getDelta({ key: `total`, outKey: `velocity` })),
(arr) => addTrends(arr, { yKey: `velocity`, bandwidth: 0.03 }),
(arr) => arr.map(getDelta({ key: `velocity`, outKey: `acceleration` })),
(arr) =>
addTrends(arr, { yKey: `acceleration`, bandwidth: 0.05, period: 14 }),
(arr) => arr.map(getDelta({ key: `accelerationLoess`, outKey: `jerk` })),
(arr) => addTrends(arr, { yKey: `jerk` })
)