-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventAdapter.ts
44 lines (36 loc) · 1.48 KB
/
EventAdapter.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
35
36
37
38
39
40
41
42
43
44
import { CalendarEvent } from "./Google/EventManager";
export module EventAdapter {
const LOCATION_RE = /@(.+)$/g;
const DATE_RE = /\D?(\d+\D\d+\D\d+)[^\d ]?/g;
export function mapLscEventToCalendarEvent(event: LscEvent) {
let date: Date;
let title: string;
try {
let matched = Array.from(event.title.matchAll(DATE_RE));
if(matched.length < 1)
throw new TypeError(`Unfortunately, /u/${event.user} doesn't understand what [DD/MM/YY] means`);
let [[datePart, dateStr]] = matched;
let [day, month, year] = dateStr.split('/').map(x => parseInt(x));
if([day, month, year].some(Number.isNaN))
throw new TypeError(`Date could not be parsed ${dateStr} for event ${event.link}`);
date = new Date(year < 100 ? 2000 + year : year, month - 1, day, 0, 0, 0, 0);
title = event.title.trim().substr(datePart.length).trim();
}
catch(err) {
throw new TypeError(`Event could not be parsed ${event.title}\nError: ${err}`);
}
let location = Array.from(event.title.matchAll(LOCATION_RE))?.[0]?.[1] ?? null;
return {
date,
description: `Posted by ${event.user}: ${event.link}\n${event.description}`,
title,
location
} as CalendarEvent;
}
}
export interface LscEvent {
user: string;
title: string;
description: string;
link: string;
}