Skip to content

Instantly share code, notes, and snippets.

@jeyemwey
Last active November 21, 2016 13:43
Show Gist options
  • Save jeyemwey/528a57efb69574b9126ee1d0669d9c43 to your computer and use it in GitHub Desktop.
Save jeyemwey/528a57efb69574b9126ee1d0669d9c43 to your computer and use it in GitHub Desktop.
/**
* In jedem VEVENT gibt es eine Zeile "DESCRIPTION", in der der Kurs-Name drin
* steht. Ich bin in Kurs DAI 16/1, das bedeutet, dass alle Kurse im Array
* bleiben sollen, die den Substring "DAI" haben, aber jene mit "DAI 16/2"
* sollen rausfliegen. Das habe ich im Map-Teil mit den Regulären Ausdrücken
* versucht zu testen, es funktioniert aber noch nicht ganz, was man bei der
* Ausführung des Skriptes merkt.
*/
"use strict";
const https = require("https");
const url = "https://www1.hft-leipzig.de/hiscal/pub/hiscalStg.php?stg=dai16";
var req = https.get(url, (res) => {
var body;
res.on('data', (chunk) => {
body += chunk;
}).on('end', () => {
//Split the Calendar into dates
var calendar = body.split("BEGIN:VEVENT");
//Set the headers and shift the list by 1
var newCal = calendar.shift();
//Remove "bad" Items from the calendar Array
calendar = calendar.filter((vevent) => {
// get a oneliner
let asLine = vevent.split('\n').join('\\n');
return /DAI/.test(asLine) && !(/DAI\s?16\/2/.test(asLine));
}).join('BEGIN:VEVENT');
//Join it back together and write it out.
newCal += "BEGIN:VEVENT" + calendar;
// check if vcalendar is closed
if(!/END:VCALENDAR$/gm.test(newCal)) {
// close it if not
newCal += `\nEND:VCALENDAR`;
}
console.log(newCal);
})
});
req.on('error', (e) => {
console.log('ERROR: ' + e.message);
});
@NonPolynomial
Copy link

NonPolynomial commented Oct 30, 2016

  1. du kannst das erste item eines Arrays mit var newCal = calendar.shift(); speichern
  2. wenn du ein Array "filtern" möchtest, benutz .filter()
  3. du brauchst keine .* in deinem RegExp
  4. RegExp testet immer Zeile für Zeile, dein vevent String ist aber multiline

versuch mal das:

// ...
let calendar = body.split('BEGIN:VEVENT');

// get the "intro"
let newCal = calendar.shift();

// filter vevents
calendar = calendar.filter((vevent) => {
    // get a oneliner
    let asLine = vevent.split('\n').join('\\n');
    return /DAI/.test(asLine) && !(/DAI 16\/2/.test(asLine));
}).join('BEGIN:VEVENT');

// add vevents to new calendar
newCal += calendar;

// check if vcalendar is closed
if(!/END:VCALENDAR$/gm.test(newCal)) {
    // close it if not
    newCal += `\nEND:VCALENDAR`;
}
// ...

(kein Ahnung warum ich erst auf englisch geantwortet hab :D)

@jeyemwey
Copy link
Author

@NonPolynomial: Fehlt da nicht das Invers zum \\n => \n? Also vevent.split('\\n').join('\n'); ? Oder ist das nicht benötigt?

@NonPolynomial
Copy link

@jeyemwey das wird nur für die Filterung gesplittet, die Funktion in .filter() gibt nur ein boolschen Wert zurück und ändert nicht die Einträge im Array. deswegen brauch man das nicht nochmal invertieren.

damit das mit der Filterung klappt fehlt noch bei der Filterung die Zuweisung:

// falsch
calendar.filter(handler);

// richtig
calendar = calendar.filter(handler);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment