/
eventsToObject.js
57 lines (49 loc) · 1.64 KB
/
eventsToObject.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
'use strict';
const beans = require('simple-configure').get('beans');
const misc = beans.get('misc');
function contentsToObject(contents, year) {
if (!contents) { return {}; }
function titleAndLinkToObject(element) {
const titleAndLink = element.replace(/[[)]/g, '').split(/\]\s*\(/);
if (titleAndLink.length === 2) {
return titleAndLink;
}
}
function dates(element) {
function toDate(dayMonthString, plusMillis) {
const dayMonth = dayMonthString ? dayMonthString.split('.') : [];
if (dayMonth.length < 2) {
return null;
}
return new Date(Date.UTC(year, parseInt(dayMonth[1]) - 1, parseInt(dayMonth[0])) + plusMillis);
}
if (element.trim()) {
const fromAndUntil = misc.compact(element.split('-').map(each => each.trim()));
const from = toDate(fromAndUntil[0], 0);
const until = toDate(fromAndUntil[1] || fromAndUntil[0], 79200000); // 22 hours
if (from && until) {
return [from.toISOString(), until.toISOString()];
}
return null;
}
}
function lineToObject(line) {
const elements = line.split('|');
if (elements.length === 3) {
const titleAndLink = titleAndLinkToObject(elements[0]);
const fromUntil = dates(elements[2]);
if (titleAndLink && fromUntil) {
return {
start: fromUntil[0],
end: fromUntil[1],
url: titleAndLink[1].trim(),
title: titleAndLink[0].trim() + ' (' + elements[1].trim() + ')',
color: '#999'
};
}
}
}
const lines = contents.split(/[\n\r]/);
return misc.compact(lines.map(lineToObject));
}
module.exports = contentsToObject;