Toggle navigation
Toggle navigation
This project
Loading...
Sign in
HyeonJun Jeon
/
Extended-Calendar
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
HyeonJun Jeon
2022-06-03 07:48:59 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
bce56861b1e66739d8859c9387363c8ac10e5ac8
bce56861
1 parent
72386e9c
[Add] ICal parser
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
0 deletions
server/libs/ICal.js
server/libs/ICal.js
0 → 100644
View file @
bce5686
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
"DTSTAMP"
:
k
=
"CREATED"
;
v
=
dateTimeToDate
(
v
);
break
;
case
"UID"
:
v
=
v
.
split
(
"-"
)[
2
];
break
;
case
"DTSTART"
:
k
=
"START"
;
v
=
dateTimeToDate
(
v
);
break
;
case
"DTEND"
:
k
=
"END"
;
v
=
dateTimeToDate
(
v
);
break
;
case
"DTSTART;VALUE=DATE"
:
case
"DTEND;VALUE=DATE"
:
k
=
"DATE"
;
v
=
dateToDate
(
v
);
break
;
case
"X-ALT-DESC;FMTTYPE=text/html"
:
k
=
"DETAIL"
;
break
;
case
"SUMMARY"
:
k
=
"LABEL"
;
let
b
;
[
v
,
b
]
=
v
.
split
(
" ["
);
sche
[
"SUBJECT"
]
=
b
.
substring
(
0
,
b
.
length
-
1
);
break
;
case
"CLASS"
:
case
"SEQUENCE"
:
return
;
case
"DESCRIPTION"
:
case
"URL"
:
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
dateTimeToDate
(
str
)
{
const
[
a
,
b
]
=
str
.
split
(
"T"
);
return
new
Date
(
insert
(
a
,
[
4
,
6
,
8
],
"-"
)
+
"T"
+
insert
(
b
,
[
2
,
4
,
6
],
":"
));
}
function
dateToDate
(
str
)
{
return
new
Date
(
insert
(
str
,
[
4
,
6
,
8
],
"-"
));
}
// let result;
// fs.readFile("C:/Users/teddy/Downloads/data.ics", "utf8", (err, data) => {
// if (err) console.log(err);
// else result = parseICal(data);
// });
export
{
parseICal
};
Please
register
or
login
to post a comment