forked from SLYtiger16/caldavjs-nextcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ical-parser.js
123 lines (104 loc) · 2.56 KB
/
ical-parser.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
var async = require("async"),
Readable = require("stream").Readable,
readline = require("readline"),
lineReader,
stream;
function convert(fileData, logging, callback) {
if(typeof callback !== "function") {
throw new Error("iCAL-PARSER: No callback provided. Aborting!!");
return console.error("No callback provided. Aborting!!");
}
if(typeof fileData !== "string") {
return callback(new Error("iCAL-PARSER: Invalid file data passed. Aborting!!"));
}
prepare(fileData, logging, callback);
}
function prepare(fileData, logging, callback) {
var _obj = {},
SPACE = " ",
lines = fileData.split(/\r\n|\n|\r/),
prevKey = null,
startKeys = [],
objArr = [],
endKeys = [];
var parseLine = function(line, cb) {
var keys = line.split(":"),
isMultiLine = (line[0] === SPACE),
key = keys[0],
value = keys[1];
prevKey = isMultiLine && prevKey || key;
switch(key) {
case "BEGIN":
if(startKeys.length) {
objArr.push(_obj);
}
_obj = {};
startKeys.push(value);
break;
case "END":
endKeys.push(value);
break;
default:
if(isMultiLine) {
_obj[prevKey] += line.substring(1);
} else {
_obj[key] = value;
}
}
cb();
};
async.each(lines, async.ensureAsync(parseLine), function() {
if(startKeys.length) {
objArr.push(_obj);
}
process(startKeys, endKeys, objArr, logging, callback);
});
}
function process(startKeys, endKeys, objArr, logging, callback) {
var result = {},
len = startKeys.length,
startIdx = -1,
i = 0;
if (logging) console.log("processing..");
var iterator = function() {
return i < len;
};
var iteratee = function(cb) {
startIdx = startKeys.indexOf(endKeys[0]);
if(startIdx > 0) {
try {
result = objArr[startIdx - 1];
if(!result.hasOwnProperty(startKeys[startIdx])) {
result[startKeys[startIdx]] = [];
}
result[startKeys[startIdx]].push(objArr[startIdx]);
objArr.splice(startIdx, 1);
objArr[startIdx - 1] = result;
startKeys.splice(startIdx, 1);
endKeys.shift();
} catch(err) {
cb(err);
}
}
i++;
cb(null);
};
var onComplete = function(err) {
if(err) {
return callback(err);
}
result = {};
result[startKeys[0]] = [objArr[0]];
if(objArr.length && objArr[0].VEVENT && objArr[0].VEVENT.length) {
if (logging) console.info("iCAL-PARSER:", objArr[0].VEVENT.length, "events parsed");
} else {
if (logging) console.info("iCAL-PARSER: 0 events found");
}
return callback(err, result);
};
async.whilst(iterator, iteratee, onComplete);
}
module.exports = {
convert: convert
}