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
|
var chrono = require('../../src/chrono');
test("Test - Custom parser example", function() {
var christmasParser = new chrono.Parser();
// Provide search pattern
christmasParser.pattern = function () { return /Christmas/i }
// This function will be called when matched pattern is found
christmasParser.extract = function(text, ref, match, opt) {
// Return a parsed result, that is 25 December
return new chrono.ParsedResult({
ref: ref,
text: match[0],
index: match.index,
start: {
day: 25,
month: 12,
}
});
}
var custom = new chrono.Chrono();
custom.parsers.push(christmasParser);
var resultDate = custom.parseDate("I'll arrive at 2.30AM on Christmas night", new Date(2013, 11, 10))
var expectDate = new Date(2013, 12-1, 25, 2, 30, 0);
expect(expectDate.getTime()).toBeCloseTo(resultDate.getTime())
});
test("Test - Custom refiner example", function() {
var guessPMRefiner = new chrono.Refiner();
// If there is no AM/PM specified, all time between 1:00 - 4:00 will be guessed as PM (13.00 - 16.00)
guessPMRefiner.refine = function(text, results, opt) {
results.forEach(function (result) {
if (!result.start.isCertain('meridiem')
&& result.start.get('hour') >= 1 && result.start.get('hour') < 4) {
result.start.assign('meridiem', 1);
result.start.assign('hour', result.start.get('hour') + 12);
}
});
return results;
}
var custom = new chrono.Chrono();
custom.refiners.push(guessPMRefiner);
var resultDate = custom.parseDate("This is at 2.30", new Date(2013, 11, 10))
var expectDate = new Date(2013, 12-1, 10, 14, 30, 0);
expect(expectDate.getTime()).toBeCloseTo(resultDate.getTime())
var resultDate = custom.parseDate("This is at 2.30 AM", new Date(2013, 11, 10))
var expectDate = new Date(2013, 12-1, 10, 2, 30, 0);
expect(expectDate.getTime()).toBeCloseTo(resultDate.getTime())
});
|