-
Notifications
You must be signed in to change notification settings - Fork 0
/
ical-parser.ts
34 lines (30 loc) · 1.13 KB
/
ical-parser.ts
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
import ical from 'node-ical';
import { EventStatus, EventTransparency, SourceEvent } from './models';
export const parseIcalFile = async (url: string): Promise<SourceEvent[]> => {
const response = await ical.fromURL(url);
const events = Object.values(response) as ical.VEvent[];
return events.map(e => mapIcalEventToSourceEvent(e)).filter(e => e.start && e.end && e.summary);
}
const mapIcalEventToSourceEvent = (event: ical.VEvent): SourceEvent => {
let status = EventStatus.CONFIRMED;
if (event.status === 'CANCELLED') {
status = EventStatus.CANCELLED;
} else if (event.status === 'TENTATIVE') {
status = EventStatus.TENTATIVE;
}
let transparency: EventTransparency;
if (event.transparency === 'TRANSPARENT') {
transparency = EventTransparency.TRANSPARENT;
} else if (event.transparency === 'OPAQUE') {
transparency = EventTransparency.OPAQUE;
}
return {
id: event.uid,
summary: event.summary,
description: event.description,
status,
transparency,
start: new Date(event.start),
end: new Date(event.end),
}
}