-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionalities.ts
43 lines (37 loc) · 1.99 KB
/
functionalities.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { CovidData, PerCountryHemispheres } from './model';
import { filterInvalidAttributes, getNLargest } from './utils';
/** Find the names (sorted) of the three coutries with most confirmed cases in a dataset. */
export const threeCountriesMostConfirmed = (data: CovidData[]): string[] => {
data = filterInvalidAttributes({ confirmed: 'number', country: 'string' }, data);
return getNLargest(data, 3, (a, b) => a.confirmed - b.confirmed)
.map(element => element.country)
.sort();
};
/** Find the countries with most deaths for the each hemisphere. */
export const countryMostDeathsByHemisphere = (data: CovidData[]): PerCountryHemispheres<string> => {
data = filterInvalidAttributes({ latitude: 'number', deaths: 'number', country: 'string' }, data);
let northern: CovidData = { country: '', active: 0, deaths: 0, confirmed: 0, latitude: 0 };
let southern: CovidData = { country: '', active: 0, deaths: 0, confirmed: 0, latitude: 0 };
data.forEach(row => {
if (row.latitude >= 0 && row.deaths > northern.deaths) {
northern = row;
} else if (row.latitude < 0 && row.deaths > southern.deaths) {
southern = row;
}
});
return { northern: northern.country, southern: southern.country };
};
/** Sum "active" field of all countries with at least 1,000,000 confirmed cases. */
export const sumActiveForManyCases = (data: CovidData[]): number => {
data = filterInvalidAttributes({ active: 'number', confirmed: 'number' }, data);
return data.reduce((acc, cur) => acc + (cur.confirmed >= 1e6 ? cur.active : 0), 0);
};
/** Sum "death" fields for the 5 countries with the fewest active cases for the 10 with the most confirmed cases. */
export const sumFewestDeathsInMostActives = (data: CovidData[]): number => {
data = filterInvalidAttributes({ active: 'number', confirmed: 'number', deaths: 'number' }, data);
return getNLargest(
getNLargest(data, 10, (a, b) => a.active - b.active),
5,
(a, b) => b.confirmed - a.confirmed
).reduce((acc, cur) => acc + cur.deaths, 0);
};