const csv = require('fast-csv')
const fs = require('fs')
let records = []
/**
* Parse CSV file and call processor functions on every line.
*/
function start (dataFile) {
csv
.parseFile(dataFile, {
delimiter: ';'
})
.on("data", function (data) {
if (data[0] !== 'Bereich') processInput(...data)
})
.on("end", createHTML)
}
/**
* Generate one object for every line in the CSV file and push
* it to the records array.
*/
function processInput (section, chair, category, audience, abbreviation,
topic, description, deadline, link) {
records.push({
section, chair, category, audience, abbreviation, topic, description, deadline, link
})
}
/**
* Generate HTML from the records array.
* The resulting markup is written to ./result.txt.
*/
function createHTML() {
const recordsBySection = groupBy(records, x => x.section)
let resultString = ''
let sections = Array.from(['Informatik', 'Angewandte Informatik', 'Wirtschaftsinformatik'])
.filter(section => recordsBySection.has(section))
sections.forEach(function (section) {
resultString += `${section}\n`
})
sections.forEach(function (section) {
resultString += `
${section}
\n`
let recordsByChair = groupBy(recordsBySection.get(section), x => x.chair)
let chairs = [...recordsByChair.keys()].sort()
chairs.forEach(function (chair) {
resultString += `${chair}
\n`
let recordsByCategory = groupBy(recordsByChair.get(chair), x => x.category)
let categories = [...recordsByCategory.keys()].sort()
categories.forEach(function (category) {
recordsByCategory.get(category).forEach(function (cur) {
resultString += `${category}`
resultString += cur.abbreviation ? ` ${cur.abbreviation}` : ''
resultString += `: ${cur.topic} (${cur.audience})
\n`
resultString += `
Kurzbeschreibung: ${cur.description}
\n`
resultString += `
Anmeldefrist: ${cur.deadline}
\n`
resultString += `
Weitere Informationen und Anmeldung: `
resultString += cur.link.startsWith('http') ? `
${cur.link}` : cur.link
resultString += `
\n`
})
})
})
})
fs.writeFileSync('result.txt', resultString)
console.log(`Successfully parsed ${process.argv[2]}.`)
}
if (process.argv[2]) {
start(process.argv[2])
} else {
console.error("Missing argument. You have to pass the name of the data file.")
}
/**
* Group an array of records by a given key.
* Taken from the answer to this question:
* https: //stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects#38327540
*/
function groupBy(list, keyGetter) {
const map = new Map();
list.forEach((item) => {
const key = keyGetter(item);
const collection = map.get(key);
if (!collection) {
map.set(key, [item]);
} else {
collection.push(item);
}
});
return map;
}
/**
* Convert to ID-compatible string by replacing non-word characters with dashes.
*/
function convertToID(string) {
return string.replace(/\W/g, '-')
}