Skip to content

Instantly share code, notes, and snippets.

@mattcuk
Last active February 18, 2022 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattcuk/0a78213be81a668855bb51b85b8033bd to your computer and use it in GitHub Desktop.
Save mattcuk/0a78213be81a668855bb51b85b8033bd to your computer and use it in GitHub Desktop.
Replicon iCalendar -> JSON Converter. Takes the iCal feed from Replicon and converts it into JSON as best it can. Deals with a bunch of edge cases. You can adapt this to your needs easily enough.
var t = `BEGIN:VCALENDAR
PRODID:-//Replicon Inc.//dev//EN
CALSCALE: GREGORIAN
BEGIN:VEVENT
DTSTART;VALUE=DATE:20220118
DTEND;VALUE=DATE:20220122
TRANSP:OPAQUE
UID:urn:replicon-tenant:11111:time-off:35
DTSTAMP:20220131T133700Z
SUMMARY:Bereavement - Doe, Jane
DESCRIPTION:Start Date : Full Day, End Date : Full Day
PRIORITY:3
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:20220204
DTEND;VALUE=DATE:20220205
TRANSP:OPAQUE
UID:urn:replicon-tenant:11111:time-off:51
DTSTAMP:20220131T162111Z
SUMMARY:Smith, John
DESCRIPTION:Full Day
PRIORITY:3
END:VEVENT
BEGIN:VEVENT
DTSTART:20220216T130000Z
DTEND:20220216T170000Z
TRANSP:OPAQUE
UID:urn:replicon-tenant:11111:time-off:531
DTSTAMP:20220217T142637Z
SUMMARY:Comp - Bloggs, Joe
DESCRIPTION:Half Day
PRIORITY:3
END:VEVENT
END:VCALENDAR`;
var inEventBlock = false;
var events = [];
var evt;
var match;
var lines = t.split('\n');
for (var i=0; i<lines.length; i++) {
var line = lines[i];
if (line=='BEGIN:VEVENT') {
evt = {};
inEventBlock = true;
}
if (line=='END:VEVENT') {
events.push(evt);
inEventBlock = false;
}
if (inEventBlock) {
line = line.replace('DTSTART;VALUE=DATE:', 'DTSTART:');
line = line.replace('DTEND;VALUE=DATE:', 'DTEND:');
match = 'DTSTART:';
if(line.indexOf(match)>-1) evt.start = line.replace(match, '');
match = 'DTEND:';
if(line.indexOf(match)>-1) evt.end = line.replace(match, '');
match = 'SUMMARY:';
if(line.indexOf(match)>-1) {
var linesplit = line.replace(match, '').split(' - ');
if (linesplit.length>1) {
evt.type = linesplit[0];
evt.user = linesplit[1];
} else {
evt.type = '';
evt.user = linesplit[0];
}
}
match = 'DESCRIPTION:';
if(line.indexOf(match)>-1) {
line = line.replace(match, '');
linesplit = line.split(', ');
if (linesplit.length>1) {
evt.startf = linesplit[0].replace('Start Date : ', '');
evt.endf = linesplit[1].replace('End Date : ', '');
} else {
evt.startf = line;
evt.endf = line;
}
}
}
}
console.log(events);
/* RESULTING OBJECT LOOKS LIKE THIS ...
[
{
"start": "20220118",
"end": "20220122",
"type": "Bereavement",
"user": "Doe, Jane",
"startf": "Full Day",
"endf": "Full Day"
},
{
"start": "20220204",
"end": "20220205",
"type": "",
"user": "Smith, John",
"startf": "Full Day",
"endf": "Full Day"
},
{
"start": "20220216T130000Z",
"end": "20220216T170000Z",
"type": "Comp",
"user": "Bloggs, Joe",
"startf": "Half Day",
"endf": "Half Day"
}
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment