-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.gs
81 lines (77 loc) · 2.01 KB
/
Parser.gs
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
function getEventObject(text) {
var textArr = text.replace(/\s\s+/,' ').split(' ')
var dateArr = [];
var alarmArr = [];
var title ='';
var pointer='date'
textArr.forEach(function(item){
switch(pointer){
case 'date':
if(isNaN(item)){
pointer = 'alarm'
} else {
dateArr.push(+item);
break;
}
case 'alarm':
if(!/^\d+[mMhHdDwW]$/.test(item)){
pointer = 'title'
} else {
alarmArr.push(item);
break;
}
case 'title':
title += title ? ' ' + item : item;
break
default:
throw new Error('bad text parsing')
}
})
var event = getDateObject(dateArr);
event.alarms = getAlarms(alarmArr);
event.title = title;
return event;
}
function getAlarms(args){
return args
.map(function(item){
return item.toLowerCase();
})
.map(function(item){
if(~item.indexOf('m')) return parseInt(item,10);
if(~item.indexOf('h')) return parseInt(item,10)*60;
if(~item.indexOf('d')) return parseInt(item,10)*60*24;
if(~item.indexOf('w')) return parseInt(item,10)*60*24*7;
})
}
function getDateObject(args){
var today = new Date();
if(!args.length){
var start = new Date(today.getFullYear(),today.getMonth(),today.getDate()+1);
} else if(args.length === 1){
var start = new Date(today.getFullYear(),today.getMonth(),today.getDate()+args[0]);
} else{
var start = new Date(today.getFullYear(),args[0]-1,args[1]);
}
var end = new Date(start);
if(args.length===3){
start.setHours(args[2],0,0,0)
end.setHours(args[2]+1,0,0,0)
}
if(args.length===4){
start.setHours(args[2],0,0,0)
end.setHours(args[3],0,0,0)
}
if(args.length>4){
start.setHours(args[2],args[3],0,0)
end.setHours(args[4],args[5] || 0,0,0)
}
if(today.valueOf() > start.valueOf()){
start.setFullYear(start.getFullYear()+1)
end.setFullYear(end.getFullYear()+1)
}
return {
start: start,
end: args.length>2 ? end : undefined
}
}