-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
86 lines (73 loc) · 2.04 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Calendar, Day, Event, EventConfig, RecurrenceRule } from "./deps.ts";
import { Lesson } from "./parser.ts";
const WEEK1_FIRST_DAY = [2023, 1, 13];
const getMondayOfWeek = (n: number) => {
const [year, month, date] = WEEK1_FIRST_DAY;
return new Date(year, month, (n - 1) * 7 + date);
};
const getDayCode = (day: Day) => {
switch (day) {
case "MO":
return 0;
case "TU":
return 1;
case "WE":
return 2;
case "TH":
return 3;
case "FR":
return 4;
case "SA":
return 5;
case "SU":
return 6;
}
};
export function genCalendar(lessons: { [title: string]: Lesson }) {
const events: Event[] = [];
for (const title in lessons) {
const lesson = lessons[title];
const desc = lesson.location;
const duration = lesson.time.length * 30 * 60;
const day = lesson.day;
const beginTime = lesson.time[0].split(":").map((t) => parseInt(t));
// eg. [11:00, 11:30, 12:00] => [11,0]
const timeSinceMonday =
(getDayCode(day) * 86400 + beginTime[0] * 3600 + beginTime[1] * 60) * 1e3;
const weeks = lesson.weeks
.slice(5)
.split(",")
.map((weekDuration) =>
weekDuration
.trim()
.split("-")
.map((week) => parseInt(week))
);
for (const weekDuration of weeks) {
const firstMonday = getMondayOfWeek(weekDuration[0]);
const rrule: RecurrenceRule = {
freq: "WEEKLY",
until: weekDuration.length >= 2
? new Date(
firstMonday.getTime() +
86400 * 7e3 * (weekDuration[1] - weekDuration[0] + 1),
)
: new Date(firstMonday.getTime() + 86400 * 7e3),
};
const cfg: EventConfig = {
title,
beginDate: new Date(firstMonday.getTime() + timeSinceMonday),
desc,
duration,
rrule,
alarm: {
desc: `${title} will start in 30 minutes`,
advance: 30,
},
};
const evt = new Event(cfg);
events.push(evt);
}
}
return new Calendar(events);
}