ICal.js
2.24 KB
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
87
88
89
90
91
92
93
94
95
96
97
function parseICal(text) {
const schedules = [];
let sche = {};
let reading = false;
for (const line of text.replace(/\r\n /g, "").split("\r\n")) {
if (reading) {
if (line === "END:VEVENT") {
reading = false;
schedules.push(sche);
sche = {};
} else {
const i = line.indexOf(":");
process(sche, line.slice(0, i), line.slice(i + 1));
}
} else if (line === "BEGIN:VEVENT") reading = true;
}
return schedules;
}
const errArr = [];
function process(sche, k, v) {
switch (k) {
case "DTSTART":
sche.scheType = "time";
sche.date = getDatestr(v);
return;
case "DTEND":
k = "endTime";
v = getTimestr_z(v);
break;
case "DTSTART;VALUE=DATE":
sche.scheType = "date";
k = "date";
v = getDatestr(v);
break;
case "DTEND;VALUE=DATE":
return;
case "X-ALT-DESC;FMTTYPE=text/html":
k = "detail";
break;
case "SUMMARY":
k = "label";
v = v.substring(0, v.indexOf(" ["));
break;
case "UID":
k = "uid";
v = v.split("-")[2];
break;
case "DTSTAMP":
case "CLASS":
case "SEQUENCE":
return;
case "DESCRIPTION":
k = "description";
case "URL":
k = "url";
sche.subjectID = v.substring(v.indexOf("se_") + 3, v.indexOf("&m"));
break;
default:
errArr.push(k);
}
sche[k] = v;
}
function insert(str, idxs, char) {
let nstr = str.substring(0, idxs[0]);
for (let i = 0; i < idxs.length - 1; i++)
nstr += char + str.substring(idxs[i], idxs[i + 1]);
return nstr;
}
/**
* parse DateString
* @param {string} str "YYYYMMDDTHHMMSS"
* @returns {string} "YYYY-MM-DD"
*/
function getDatestr(str) {
const a = str.substring(0, str.indexOf("T"));
return insert(a, [4, 6, 8], "-");
}
/**
* parse ZDateString
* @param {string} zstr "YYYYMMDDTHHMMSSZ" (UTC)
* @returns {string} "HH:MM:SS" (UTC+9)
*/
function getTimestr_z(zstr) {
const it = zstr.indexOf("T");
const a = zstr.substring(0, it);
const b = zstr.substring(it + 1, zstr.length - 1);
const date = new Date(
insert(a, [4, 6, 8], "-") + "T" + insert(b, [2, 4, 6], ":") + "+00:00"
);
return date.toTimeString().substring(0, 8);
}
module.exports = parseICal;