-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticlesToCSV.js
40 lines (39 loc) · 1.19 KB
/
articlesToCSV.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
import fs from 'fs/promises';
export default async function articlesToCSV(fileName, outputFileName) {
try {
let parsedArticles = JSON.parse(await fs.readFile('./articles/' + fileName)).parsedArticles;
let keys = new Set();
for (const article of parsedArticles) {
for (const key in article) {
keys.add(key);
}
}
keys = Array.from(keys);
let authorKeyIndex = keys.indexOf('authors');
let separator = '\t';
let headers = keys.join(separator);
let rows = [headers];
for (const article of parsedArticles) {
let row = '';
let authors;
for (const key of keys) {
if (key !== 'authors') {
row += article[key] ? article[key] + separator : separator;
} else {
authors = article[key].split(', ');
row += authors.pop() + separator;
}
}
rows.push(row);
for (const author of authors) {
row = separator.repeat(authorKeyIndex);
row += author;
row += separator.repeat(keys.length - authorKeyIndex - 1);
rows.push(row);
}
}
await fs.writeFile('./articles/' + outputFileName, rows.join('\n'));
} catch (err) {
throw new Error(err);
}
}