refactoring
This commit is contained in:
34
docs/SymptomsCausedByVaccines/js/PrrByKey2CsvConverter.js
Normal file
34
docs/SymptomsCausedByVaccines/js/PrrByKey2CsvConverter.js
Normal file
@@ -0,0 +1,34 @@
|
||||
class PrrByKey2CsvConverter {
|
||||
|
||||
static convertPrrByKey2Csv({ prrByKey, keyColumn, prrColumn }) {
|
||||
return PrrByKey2CsvConverter.#convert2Csv(
|
||||
{
|
||||
prrByKey: PrrByKey2CsvConverter.#quoteKeys(prrByKey),
|
||||
keyColumn: PrrByKey2CsvConverter.#quote(keyColumn),
|
||||
prrColumn: PrrByKey2CsvConverter.#quote(prrColumn)
|
||||
});
|
||||
}
|
||||
|
||||
static #quoteKeys(prrByKey) {
|
||||
return Object.fromEntries(
|
||||
Object
|
||||
.entries(prrByKey)
|
||||
.map(([key, prr]) => [PrrByKey2CsvConverter.#quote(key), prr]))
|
||||
}
|
||||
|
||||
static #quote(str) {
|
||||
return '"' + str + '"';
|
||||
}
|
||||
|
||||
static #convert2Csv({ prrByKey, keyColumn, prrColumn }) {
|
||||
const header = `${keyColumn},${prrColumn}`;
|
||||
return `${header}\n${PrrByKey2CsvConverter.#convertDict2Csv(prrByKey)}`;
|
||||
}
|
||||
|
||||
static #convertDict2Csv(dict) {
|
||||
return Object
|
||||
.entries(dict)
|
||||
.map(([key, value]) => `${key},${value}`)
|
||||
.join('\n');
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,14 @@ class PrrByVaccineTableView {
|
||||
UIUtils.downloadUrlAsFilename(
|
||||
window.URL.createObjectURL(
|
||||
new Blob(
|
||||
[Utils.convertDict2CSV(this.#prrByVaccine)],
|
||||
[
|
||||
PrrByKey2CsvConverter.convertPrrByKey2Csv(
|
||||
{
|
||||
prrByKey: this.#prrByVaccine,
|
||||
keyColumn: 'Vaccine',
|
||||
prrColumn: 'Proportional Reporting Ratio'
|
||||
})
|
||||
],
|
||||
{ type: 'text/csv' })),
|
||||
this.#symptom
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user