-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-csv.js
67 lines (59 loc) · 1.6 KB
/
create-csv.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
const fs = require("fs").promises;
const { parse } = require("json2csv");
const transform = phenotype => {
const skip = ["summary"];
const stringArrays = [
"type",
"authors",
"age",
"data_modalities",
"gender",
"ethnicity",
"institution",
"network_associations",
"owner_groups",
"view_groups",
"race",
"data_models"
];
let transformed = {};
Object.keys(phenotype).forEach(key => {
if (stringArrays.includes(key)) {
transformed[key] = phenotype[key].join("\n");
} else if (key === "references") {
transformed[key] = phenotype[key].map(ref => ref.title).join("\n");
} else if (key === "files") {
transformed[key] = phenotype[key]
.map(file => file.url.split("/").pop())
.join("\n");
} else if (key == "data_dictionaries") {
transformed[key] = phenotype[key]
.map(dict => dict.url.split("/").pop())
.join("\n");
} else if (key == "date_created") {
transformed[key] = phenotype[key].substring(0, 10);
} else if (key == "implementations") {
transformed[key] = phenotype[key].map(impl => impl.title).join("\n");
} else if (skip.includes(key)) {
/* noop */
} else {
transformed[key] = phenotype[key];
}
});
return transformed;
};
const main = () => {
const all = [];
fs.readdir("data")
.then(listing => {
listing.forEach(item => {
let filename = `./data/${item}/${item}.json`;
all.push(transform(require(filename)));
});
})
.then(() => {
const csv = parse(all);
console.log(csv);
});
};
main();