forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberUtils.ts
More file actions
35 lines (30 loc) · 1.17 KB
/
numberUtils.ts
File metadata and controls
35 lines (30 loc) · 1.17 KB
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
import { ObjectIface } from "../../types/types";
export const convertPercentToNonPercent = (
data: ObjectIface<number>
): ObjectIface<number> => {
const toBeRemoved = ["casesPerOneMillion", "criticalPerOneMillion"];
let cases = data["casesPerOneMillion"]; // be the number of covid 19 cases
let response: ReturnType<typeof convertPercentToNonPercent> = {};
Object.keys(data).forEach((key) => {
if (toBeRemoved.includes(key)) return;
let tempValue: number;
if (key !== "testsPerOneMillion") tempValue = data[key] / cases;
else tempValue = data[key] / 1000000;
tempValue = tempValue * 100;
let value = Math.round(tempValue * 1000) / 1000; // round to 3 decimal places
response[key] = value;
});
return response;
};
/**
* from https://stackoverflow.com/questions/1053843/get-the-element-with-the-highest-occurrence-in-an-array
* @param arr array of any type
* @returns the most common element in the array
*/
// prettier-ignore
export const getMostCommonElement = (arr: any[]) => {
return arr.sort((a,b) =>
arr.filter(v => v===a).length
- arr.filter(v => v===b).length
).pop();
}