-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateParser.tests.js
297 lines (247 loc) · 11.2 KB
/
dateParser.tests.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
var T = (function() {
// Use utc:true by default so that we don't have to worry
// about timezones and test breaking depending on time of
// year due to daylight savings.
const defaultOptions = { utc: true };
const tests = [];
const runTestCase = function(t, c) {
let input = c[0];
let expected = c[1];
let options = { ... defaultOptions, ... c[2] };
let result = { name: t.name, "case": c };
try{
let actual = dateParser.parse(input, options).toString();
if(expected === undefined) {
// expected exception but didn't get one
result.status = "fail";
} else {
result.status = (actual === expected) ? "pass" : "fail";
}
result.actual = actual;
} catch(e) {
result.status = (expected === undefined)
? "pass" // exception expected
: "fail";
result.error = e;
}
return result;
};
const runTest = function(t) {
let cases = t.cases;
if(typeof cases === 'function') {
cases = cases();
}
return cases.map(c => runTestCase(t, c));
};
return {
test: function(name, cases){
tests.push({ name, cases });
},
runTests: function(listener) {
const allResults = [];
let i = 0;
const runNext = function() {
if(!tests[i]) {
if(listener.onAllTestsCompleted) {
listener.onAllTestsCompleted(allResults);
}
return;
}
const t = tests[i++];
const testResults = runTest(t);
allResults.push(... testResults);
if(listener.onTestCompleted) {
listener.onTestCompleted(testResults);
}
setTimeout(runNext);
};
setTimeout(runNext);
}
};
}());
var yr = (new Date()).getFullYear().toString();
var pad = function(number) {
if (number < 10) {
return '0' + number;
}
return number;
};
T.test("parse date", [
// day first
["24 08 2017", "start=2017-08-24"],
["24/08/2017", "start=2017-08-24"],
["24-08-2017", "start=2017-08-24"],
["24.08.2017", "start=2017-08-24"],
["24th Aug 2017", "start=2017-08-24"],
// month first
["08 24 2017", "start=2017-08-24"],
["08/24/2017", "start=2017-08-24"],
["08-24-2017", "start=2017-08-24"],
["08.24.2017", "start=2017-08-24"],
["Aug 24th 2017", "start=2017-08-24"],
// year first
["2017 08 24", "start=2017-08-24"],
["2017/08/24", "start=2017-08-24"],
["2017-08-24", "start=2017-08-24"],
["2017.08.24", "start=2017-08-24"],
["2017 Aug 24th", "start=2017-08-24"],
// year first
// (succeeds despite potential ambiguity of day/month, because when year is
// placed first, Y-M-D format is assumed, as opposed to Y-D-M)
["2017 08 10", "start=2017-08-10"],
["2017/08/10", "start=2017-08-10"],
["2017-08-10", "start=2017-08-10"],
["2017.08.10", "start=2017-08-10"],
// with day-of-week
["Thursday, Aug 24th 2017", "start=2017-08-24"],
["Thursday, 24th Aug 2017", "start=2017-08-24"],
]);
T.test("parse date - ambiguous", [
// day first
["10 08 2017", undefined],
["10/08/2017", undefined],
["10-08-2017", undefined],
["10.08.2017", undefined],
// month first
["08 10 2017", undefined],
["08/10/2017", undefined],
["08-10-2017", undefined],
["08.10.2017", undefined],
// two-digit year first
["17 08 10", undefined],
["17/08/10", undefined],
["17-08-10", undefined],
["17.08.10", undefined],
// If we force it to choose 'first', it chooses the M-D-Y interpretation
["10 08 2017", "start=2017-10-08", { ambiguityHandling: 'first' }],
["08 10 2017", "start=2017-08-10", { ambiguityHandling: 'first' }],
]);
T.test("parse datetime", [
["Aug 24 2017 at 8 pm", "start=2017-08-24T20:00:00Z"],
["Aug 24 2017 @ 8 pm", "start=2017-08-24T20:00:00Z"],
["Aug 24 2017 8 pm", "start=2017-08-24T20:00:00Z"],
["Aug 24 2017 8:15 pm", "start=2017-08-24T20:15:00Z"],
["Aug 24 2017 8:15 am", "start=2017-08-24T08:15:00Z"],
["Aug 24 2017 8:15", "start=2017-08-24T08:15:00Z"],
["Aug 24 2017 8:00p", "start=2017-08-24T20:00:00Z"],
["2017 08 24 8:00pm", "start=2017-08-24T20:00:00Z"],
]);
T.test("parse date-range", [
["Aug 24th 2017 - Aug 27th 2017", "start=2017-08-24, end=2017-08-27"],
["Aug 24th - 27th 2017", "start=2017-08-24, end=2017-08-27"],
["24th - 27th Aug 2017", "start=2017-08-24, end=2017-08-27"],
["Aug 24th 2017 - Sep 27th 2017", "start=2017-08-24, end=2017-09-27"],
["Aug 24th - Sep 27th 2017", "start=2017-08-24, end=2017-09-27"],
["Aug 24th 2016 - Sep 27th 2017", "start=2016-08-24, end=2017-09-27"],
]);
T.test("parse date + time-range", [
["Aug 24 2017 8:00 pm - 10:00 pm",
"start=2017-08-24T20:00:00Z, end=2017-08-24T22:00:00Z"],
["Aug 24 2017 @ 8:00 pm - 10:00 pm",
"start=2017-08-24T20:00:00Z, end=2017-08-24T22:00:00Z"],
["Aug 24 2017 from 8:00 pm - 10:00 pm",
"start=2017-08-24T20:00:00Z, end=2017-08-24T22:00:00Z"],
["Aug 24 2017 8:00 - 10:00 pm",
"start=2017-08-24T20:00:00Z, end=2017-08-24T22:00:00Z"],
["Aug 24 2017 8 - 10 pm",
"start=2017-08-24T20:00:00Z, end=2017-08-24T22:00:00Z"],
["Aug 24 2017 8 am - 10 pm",
"start=2017-08-24T08:00:00Z, end=2017-08-24T22:00:00Z"],
// examples taken from real website
["5:30pm - 6:00pm Saturday 30th December",
"start="+yr+"-12-30T17:30:00Z, end="+yr+"-12-30T18:00:00Z"],
["05:30 PM - 06:00 PM Saturday, December 30",
"start="+yr+"-12-30T17:30:00Z, end="+yr+"-12-30T18:00:00Z"],
]);
T.test("parse datetime range", [
["May 27, 2017 5pm - May 30, 2017 12pm",
"start=2017-05-27T17:00:00Z, end=2017-05-30T12:00:00Z"],
]);
T.test("parse date-range + time", [
["Aug 28th-29th 2017 5pm",
"start=2017-08-28T17:00:00Z, recurDailyUntil=2017-08-29"],
["Aug 24th 2016 - Sep 27th 2017 9am",
"start=2016-08-24T09:00:00Z, recurDailyUntil=2017-09-27"],
]);
T.test("parse date-range + time-range", [
["May 27 2017 - December 31 2017 12:00 PM - 5:00 PM",
"start=2017-05-27T12:00:00Z, end=2017-05-27T17:00:00Z, recurDailyUntil=2017-12-31"],
]);
T.test("year is inferred", [
["Aug 24th", "start="+yr+"-08-24"],
["thursday, august 24 8 pm", "start="+yr+"-08-24T20:00:00Z"],
["thursday, august 24 8:00 pm - 10:00 pm",
"start="+yr+"-08-24T20:00:00Z, end="+yr+"-08-24T22:00:00Z"],
["May 27 - December 31 from 12:00 PM - 5:00 PM",
"start="+yr+"-05-27T12:00:00Z, end="+yr+"-05-27T17:00:00Z, recurDailyUntil="+yr+"-12-31"],
["May 27 - December 31, 2017 12:00 PM - 5:00 PM",
"start=2017-05-27T12:00:00Z, end=2017-05-27T17:00:00Z, recurDailyUntil=2017-12-31"],
// two-digit year
["May 27 - December 31, 17 12:00 PM - 5:00 PM",
"start=2017-05-27T12:00:00Z, end=2017-05-27T17:00:00Z, recurDailyUntil=2017-12-31"],
]);
T.test("date is inferred from dayOfWeek", function() {
var days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
var d = new Date();
var d2 = new Date();
d2.setDate(d.getDate() + 4);
return [
// If the day specified has the same name as the current day, it's perhaps ambiguous whether the
// parser should interpret it as "today" or "today + 7", but it chooses "today".
[days[d.getDay()], "start=" + d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate())],
// For any day name that is not the same as the current day's name, the date is chosen from
// the coming week.
[days[d2.getDay()], "start=" + d2.getFullYear() + '-' + pad(d2.getMonth() + 1) + '-' + pad(d2.getDate())],
];
});
T.test("relative day", function() {
var d = new Date();
var d2 = new Date();
d2.setDate(d.getDate() + 1);
return [
["today", "start=" + d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate())],
["today 8pm", "start=" + d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) + "T20:00:00Z"],
["8pm today", "start=" + d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) + "T20:00:00Z"],
["tomorrow", "start=" + d2.getFullYear() + '-' + pad(d2.getMonth() + 1) + '-' + pad(d2.getDate())],
["tomorrow 8pm", "start=" + d2.getFullYear() + '-' + pad(d2.getMonth() + 1) + '-' + pad(d2.getDate()) + "T20:00:00Z"],
["8pm tomorrow", "start=" + d2.getFullYear() + '-' + pad(d2.getMonth() + 1) + '-' + pad(d2.getDate()) + "T20:00:00Z"],
];
});
T.test("date-time separators are optional", [
["Aug 28th-29th @ 5pm", "start="+yr+"-08-28T17:00:00Z, recurDailyUntil="+yr+"-08-29"],
["Aug 28th-29th 5pm", "start="+yr+"-08-28T17:00:00Z, recurDailyUntil="+yr+"-08-29"],
["May 27 - December 31 from 12:00 PM - 5:00 PM",
"start="+yr+"-05-27T12:00:00Z, end="+yr+"-05-27T17:00:00Z, recurDailyUntil="+yr+"-12-31"],
["May 27 - December 31 12:00 PM - 5:00 PM",
"start="+yr+"-05-27T12:00:00Z, end="+yr+"-05-27T17:00:00Z, recurDailyUntil="+yr+"-12-31"],
]);
T.test("midnight start", [
["august 24 2017 12 am", "start=2017-08-24T00:00:00Z"],
["august 24 2017 12 am - 2 am", "start=2017-08-24T00:00:00Z, end=2017-08-24T02:00:00Z"],
["august 24 2017 12 am - 2 pm", "start=2017-08-24T00:00:00Z, end=2017-08-24T14:00:00Z"],
]);
T.test("midnight end", [
["august 24 2017 8 pm - 12 am", "start=2017-08-24T20:00:00Z, end=2017-08-25T00:00:00Z"],
["august 24 2017 8 am - 12 am", "start=2017-08-24T08:00:00Z, end=2017-08-25T00:00:00Z"],
["august 24 2017 12 pm - 12 am", "start=2017-08-24T12:00:00Z, end=2017-08-25T00:00:00Z"],
]);
T.test("noon start", [
["august 24 2017 12 pm", "start=2017-08-24T12:00:00Z"],
["august 24 2017 12 pm - 2 pm", "start=2017-08-24T12:00:00Z, end=2017-08-24T14:00:00Z"],
["august 24 2017 12 pm - 2 am", "start=2017-08-24T12:00:00Z, end=2017-08-25T02:00:00Z"],
]);
T.test("noon end", [
["august 24 2017 10 am - 12 pm", "start=2017-08-24T10:00:00Z, end=2017-08-24T12:00:00Z"],
["august 24 2017 12 am - 12 pm", "start=2017-08-24T00:00:00Z, end=2017-08-24T12:00:00Z"],
]);
T.test("crossover timerange", [
["august 24 2017 8:00 pm - 4:00 am", "start=2017-08-24T20:00:00Z, end=2017-08-25T04:00:00Z"],
["august 24 2017 10:00 am - 4:00 am", "start=2017-08-24T10:00:00Z, end=2017-08-25T04:00:00Z"],
["august 24 2017 12:00 pm - 4:00 am", "start=2017-08-24T12:00:00Z, end=2017-08-25T04:00:00Z"],
]);
T.test("timerange infer meridiem", [
["dec 25 2023 4-8pm", "start=2023-12-25T16:00:00Z, end=2023-12-25T20:00:00Z"],
["dec 25 2023 9-11am", "start=2023-12-25T09:00:00Z, end=2023-12-25T11:00:00Z"],
["dec 25 2023 12-3pm", "start=2023-12-25T12:00:00Z, end=2023-12-25T15:00:00Z"],
["dec 25 2023 12-3am", "start=2023-12-25T00:00:00Z, end=2023-12-25T03:00:00Z"],
]);