forked from MaJingRui-SH/Coronavirus-Dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
139 lines (114 loc) · 3.98 KB
/
utilities.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const fs = require("fs");
const globals = require("./globals");
exports.getJSONPath = region => {
return `./tmp/statistics_${region}.json`;
};
exports.getOverridesJSONPath = region => {
return `./overrides/statistics_${region}.json`;
};
exports.getCSVPath = region => {
return `./tmp/data_${region}.csv`;
};
exports.getExternalCSV = region => {
return `https://docs.google.com/spreadsheets/d/12Wi7QBVQ-s_5SaXlK9QwTZ9PMR_kYnBZVveCyF3kgfs/gviz/tq?tqx=out:csv&sheet=${region}`;
};
exports.addAllNumbers = numbers => {
if (numbers.length === 0) return 0;
numbers = numbers.map(number => {
return this.parseCommas(number);
});
return numbers.reduce((a, b) => a + b).toLocaleString();
};
exports.subtractTwoValues = (value1, value2) => {
return (
this.parseCommas(value1) - (this.parseCommas(value2) || 0)
).toLocaleString();
};
exports.parseCommas = number => {
number = ["", " ", "-", "N/A"].includes(number) ? "0" : `${number}`;
return parseInt(number.replace(/,/g, ""), 10);
};
exports.writeJSONFile = (region, data) => {
if (!!region.regions) {
if(!region.regions.length) {
return;
}
};
try {
fs.writeFileSync(this.getJSONPath(region), JSON.stringify(data));
} catch (err) {
console.error(err);
}
};
exports.remapKeys = (country, keyMapping) => {
let remappedCountry = { ...globals.countryStructure };
Object.keys(keyMapping).map(key => {
remappedCountry[key] = country[keyMapping[key]];
});
return remappedCountry;
};
exports.convertAllKeysToString = object => {
Object.keys(object).map(key => {
object[key] = isNaN(object[key])
? object[key]
: object[key].toLocaleString();
});
return object;
};
exports.calculateRegionTotal = regions => {
let regionTotalTemplate = { ...globals.countryStructure };
let allConfirmed = [];
let allDeaths = [];
let allRecovered = [];
let allSerious = [];
let allCritical = [];
regions.map(region => {
allConfirmed.push(region.cases);
allDeaths.push(region.deaths);
allRecovered.push(region.recovered);
allSerious.push(region.serious);
allCritical.push(region.critical);
});
regionTotalTemplate.cases = this.addAllNumbers(allConfirmed);
regionTotalTemplate.deaths = this.addAllNumbers(allDeaths);
regionTotalTemplate.recovered = this.addAllNumbers(allRecovered);
regionTotalTemplate.serious = this.addAllNumbers(allSerious);
regionTotalTemplate.critical = this.addAllNumbers(allCritical);
return regionTotalTemplate;
};
exports.getGreaterValue = (value1, value2) => {
value1 = ["", " ", "-"].includes(value1) ? "0" : `${value1}`;
value2 = ["", " ", "-"].includes(value2) ? "0" : `${value2}`;
if (typeof value1 === "string") value1 = this.parseCommas(value1);
if (typeof value2 === "string") value2 = this.parseCommas(value2);
return value1 >= value2 ? value1.toLocaleString() : value2.toLocaleString();
};
exports.syncTwoRegions = (regions1, regions2) => {
regions1.map((country1, country1Index) => {
regions2.map((country2, country2Index) => {
if (country1.country !== country2.country) return;
const countryName = country1.country;
const country1Data = country1;
const country2Data = country2;
let syncRegionData = {
country: countryName,
cases: this.getGreaterValue(country1.cases, country2.cases),
deaths: this.getGreaterValue(country1.deaths, country2.deaths),
serious: this.getGreaterValue(country1.serious, country2.serious),
recovered: this.getGreaterValue(country1.recovered, country2.recovered),
critical: this.getGreaterValue(country1.critical, country2.critical)
};
regions1[country1Index] = syncRegionData;
regions2[country2Index] = syncRegionData;
});
});
return regions1, regions2;
};
exports.renameCountryLabels = regions => {
regions.map((country, index) => {
if (!!globals.AlternativeLabelNames[country.country]) {
regions[index].country = globals.AlternativeLabelNames[country.country];
}
});
return regions;
};