-
Notifications
You must be signed in to change notification settings - Fork 1
/
cleaner.js
73 lines (55 loc) · 1.55 KB
/
cleaner.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
import fs from 'fs/promises';
import { createReadStream } from 'fs';
import readline from 'readline';
import path from 'path';
import { fileURLToPath } from 'url';
import { createArrayCsvWriter } from 'csv-writer';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const raw = path.join(__dirname, 'raw');
const processed = path.join(__dirname, 'processed');
const store = [];
const broken = [];
const files = (await fs.readdir(raw)).map(f => path.join(raw, f));
// remove trailing quotes and semicolons
const cleanString = str => str.replace(/^"*(.*?)"*;*$/, '$1');
const promises = [];
for (const file of files) {
const promise = new Promise(resolve => {
const input = createReadStream(file);
const rl = readline.createInterface({
input,
crlfDelay: Infinity
});
let first = true;
rl.on('line', line => {
if (first) return (first = false);
const split = line.split(/,"{2,}/);
const clean = split.map(cleanString);
if (!/\d+/.test(clean[0])) {
return broken.push(line);
}
store.push(clean);
});
input.on('end', resolve);
});
promises.push(promise);
}
await Promise.all(promises);
const csvWriter = createArrayCsvWriter({
header: [
'ID',
'IdentNr',
'Titel',
'Beteiligte',
'Bereich',
'MatTech',
'Objekttyp',
'Datierung'
],
path: path.join(processed, 'all.csv')
});
await Promise.all([
csvWriter.writeRecords(store),
fs.writeFile(path.join(processed, 'broken.csv'), broken.join('\n'))
]);
console.log('Done.', store.length);