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
124
125
126
127
128
129
|
var moment = require('moment');
function ParsedResult(result){
result = result || {};
this.ref = result.ref;
this.index = result.index;
this.text = result.text;
this.tags = result.tags || {};
this.start = new ParsedComponents(result.start, result.ref)
if(result.end){
this.end = new ParsedComponents(result.end, result.ref)
}
}
ParsedResult.prototype.clone = function() {
var result = new ParsedResult(this);
result.tags = JSON.parse(JSON.stringify(this.tags));
result.start = this.start.clone();
if (this.end) {
result.end = this.end.clone();
}
return result
}
ParsedResult.prototype.hasPossibleDates = function() {
return this.start.isPossibleDate() && (!this.end || this.end.isPossibleDate());
}
function ParsedComponents (components, ref){
this.knownValues = {};
this.impliedValues = {};
if (components) {
for (key in components) {
this.knownValues[key] = components[key];
}
}
if (ref) {
ref = moment(ref);
this.imply('day', ref.date())
this.imply('month', ref.month() + 1)
this.imply('year', ref.year())
}
this.imply('hour', 12);
this.imply('minute', 0);
this.imply('second', 0);
this.imply('millisecond', 0);
}
ParsedComponents.prototype.clone = function () {
var component = new ParsedComponents();
component.knownValues = JSON.parse(JSON.stringify(this.knownValues));
component.impliedValues = JSON.parse(JSON.stringify(this.impliedValues));
return component;
};
ParsedComponents.prototype.get = function(component, value) {
if (component in this.knownValues) return this.knownValues[component];
if (component in this.impliedValues) return this.impliedValues[component];
};
ParsedComponents.prototype.assign = function(component, value) {
this.knownValues[component] = value;
delete this.impliedValues[component];
};
ParsedComponents.prototype.imply = function(component, value) {
if (component in this.knownValues) return;
this.impliedValues[component] = value;
};
ParsedComponents.prototype.isCertain = function(component) {
return component in this.knownValues;
};
ParsedComponents.prototype.isPossibleDate = function() {
var dateMoment = this.moment();
if (this.isCertain('timezoneOffset')) {
dateMoment.utcOffset(this.get('timezoneOffset'))
}
if (dateMoment.get('year') != this.get('year')) return false;
if (dateMoment.get('month') != this.get('month')-1) return false;
if (dateMoment.get('date') != this.get('day')) return false;
if (dateMoment.get('hour') != this.get('hour')) return false;
if (dateMoment.get('minute') != this.get('minute')) return false;
return true;
};
ParsedComponents.prototype.date = function() {
var dateMoment = this.moment();
return dateMoment.toDate();
};
ParsedComponents.prototype.moment = function() {
var dateMoment = moment();
dateMoment.set('year', this.get('year'));
dateMoment.set('month', this.get('month')-1);
dateMoment.set('date', this.get('day'));
dateMoment.set('hour', this.get('hour'));
dateMoment.set('minute', this.get('minute'));
dateMoment.set('second', this.get('second'));
dateMoment.set('millisecond', this.get('millisecond'));
// Javascript Date Object return minus timezone offset
var currentTimezoneOffset = dateMoment.utcOffset();
var targetTimezoneOffset = this.get('timezoneOffset') !== undefined ?
this.get('timezoneOffset') : currentTimezoneOffset;
var adjustTimezoneOffset = targetTimezoneOffset - currentTimezoneOffset;
dateMoment.add(-adjustTimezoneOffset, 'minutes');
return dateMoment;
};
exports.ParsedComponents = ParsedComponents;
exports.ParsedResult = ParsedResult;
|