forked from okfde/spk-inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
by-department.js
48 lines (39 loc) · 1.29 KB
/
by-department.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
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { createObjectCsvWriter } from 'csv-writer';
import csv from 'csv-parser';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const processed = path.join(__dirname, 'processed');
const file = path.join(processed, 'all.csv');
const departments = new Map();
fs.createReadStream(file)
.pipe(csv())
.on('data', item => {
const departmentName = item.Bereich;
if (!departments.has(departmentName)) departments.set(departmentName, []);
const department = departments.get(departmentName);
department.push(item);
})
.on('end', async () => {
console.log('done sorting.');
for (const [_departmentName, department] of departments) {
const departmentName =
_departmentName?.replace(/\//g, '-').slice(0, 20) || 'no-department';
const csvWriter = createObjectCsvWriter({
path: path.join(processed, 'by-department', `${departmentName}.csv`),
header: [
'ID',
'IdentNr',
'Titel',
'Beteiligte',
'Bereich',
'MatTech',
'Objekttyp',
'Datierung'
].map(title => ({ id: title, title }))
});
await csvWriter.writeRecords(department);
}
console.log('Done.');
});