ICal.js
2.04 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
const fs = require("fs");
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);
k = "startTime";
v = getTimestr(v);
break;
case "DTEND":
k = "endTime";
v = sche.startTime;
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;
}
function getDatestr(str) {
const a = str.substring(0, str.indexOf("T"));
return insert(a, [4, 6, 8], "-");
}
function getTimestr(str) {
const b = str.substring(str.indexOf("T") + 1);
return insert(b, [2, 4, 6], ":");
}
// const fdata = fs.readFileSync("C:/Users/teddy/Downloads/data.ics", "utf8");
// const jcal = parseICal(fdata);
// console("done");
module.exports = parseICal;