normalizedData = rawData
.reduce((result, entry) => {
const prevEntry = result.find(
_entry => _entry['Country/Region'] === entry['Country/Region']
);
if (prevEntry) {
Object.entries(entry)
.filter(([key, value]) => key.match(/\d+\/\d+\/\d+/))
.forEach(([key, value]) => {
prevEntry[key] =
(parseInt(prevEntry[key]) || 0) + (parseInt(value) || 0);
});
prevEntry['Province/States'] = '';
return [
...result.filter(
_entry => _entry['Country/Region'] !== entry['Country/Region']
),
prevEntry
];
}
return [...result, entry];
}, [])
.reduce((result, entry) => {
const country = matchCountry(entry['Country/Region']);
const province = entry['Province/States'];
let prevTotal = 0;
const entries = Object.entries(entry)
.filter(([key, value]) => key.match(/\d+\/\d+\/\d+/))
.sort((a, b) => new Date(a[0]) - new Date(b[0]))
.map(([key, value]) => {
const date = new Date(key).toISOString().replace(/T.+/, '');
const total = parseInt(value) || 0;
let growth = 0;
if (total > 0) {
growth = ((total - prevTotal) / total).toFixed(2);
prevTotal = total;
} else {
prevTotal = 0;
}
return {
country,
province,
date,
growth,
total
};
});
return [...result, ...entries];
}, [])