UNPKG

8.77 kBPlain TextView Raw
1import * as chrono from "../src/";
2import { testSingleCase, testUnexpectedResult } from "./test_util";
3import { Meridiem } from "../src";
4import UnlikelyFormatFilter from "../src/common/refiners/UnlikelyFormatFilter";
5import SlashDateFormatParser from "../src/common/parsers/SlashDateFormatParser";
6import ENWeekdayParser from "../src/locales/en/parsers/ENWeekdayParser";
7import ENTimeUnitCasualRelativeFormatParser from "../src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser";
8import { ParsingComponents } from "../src/";
9
10//-------------------------------------
11
12test("Test - Load modules", function () {
13 expect(chrono).toBeDefined();
14
15 expect(chrono.Chrono).toBeDefined();
16
17 expect(chrono.parse).toBeDefined();
18
19 expect(chrono.parseDate).toBeDefined();
20
21 expect(chrono.casual).toBeDefined();
22
23 expect(chrono.strict).toBeDefined();
24});
25
26test("Test - Basic parse date functions", function () {
27 expect(chrono.parseDate("7:00PM July 5th, 2020")).toStrictEqual(new Date(2020, 7 - 1, 5, 19));
28
29 expect(chrono.en.parseDate("7:00PM July 5th, 2020")).toStrictEqual(new Date(2020, 7 - 1, 5, 19));
30
31 expect(chrono.strict.parseDate("7:00PM July 5th, 2020")).toStrictEqual(new Date(2020, 7 - 1, 5, 19));
32
33 expect(chrono.casual.parseDate("7:00PM July 5th, 2020")).toStrictEqual(new Date(2020, 7 - 1, 5, 19));
34});
35
36test("Test - Add custom parser", () => {
37 const customParser = {
38 pattern: () => {
39 return /(\d{1,2})(st|nd|rd|th)/i;
40 },
41 extract: (context, match) => {
42 expect(match[0]).toBe("25th");
43 expect(context.refDate).toBeTruthy();
44
45 return {
46 day: parseInt(match[1]),
47 };
48 },
49 };
50
51 const custom = new chrono.Chrono();
52 custom.parsers.push(customParser);
53 testSingleCase(custom, "meeting on 25th", new Date(2017, 11 - 1, 19), (result) => {
54 expect(result.text).toBe("25th");
55 expect(result.start.get("month")).toBe(11);
56 expect(result.start.get("day")).toBe(25);
57 });
58});
59
60test("Test - Add custom parser example", () => {
61 const custom = chrono.casual.clone();
62 custom.parsers.push({
63 pattern: () => {
64 return /\bChristmas\b/i;
65 },
66 extract: () => {
67 return {
68 day: 25,
69 month: 12,
70 };
71 },
72 });
73
74 testSingleCase(custom, "I'll arrive at 2.30AM on Christmas", (result) => {
75 expect(result.text).toBe("at 2.30AM on Christmas");
76 expect(result.start.get("month")).toBe(12);
77 expect(result.start.get("day")).toBe(25);
78 expect(result.start.get("hour")).toBe(2);
79 expect(result.start.get("minute")).toBe(30);
80 });
81
82 testSingleCase(custom, "I'll arrive at Christmas night", (result) => {
83 expect(result.text).toBe("Christmas night");
84 expect(result.start.get("month")).toBe(12);
85 expect(result.start.get("day")).toBe(25);
86 expect(result.start.get("meridiem")).toBe(Meridiem.PM);
87 expect(result.start.get("meridiem")).toBe(1);
88 });
89
90 testSingleCase(custom, "Doing something tomorrow", (result) => {
91 expect(result.text).toBe("tomorrow");
92 });
93});
94
95test("Test - Add custom refiner example", () => {
96 const custom = chrono.casual.clone();
97 custom.refiners.push({
98 refine: (context, results) => {
99 // If there is no AM/PM (meridiem) specified,
100 // let all time between 1:00 - 4:00 be PM (13.00 - 16.00)
101 results.forEach((result) => {
102 if (
103 !result.start.isCertain("meridiem") &&
104 result.start.get("hour") >= 1 &&
105 result.start.get("hour") < 4
106 ) {
107 result.start.assign("meridiem", Meridiem.PM);
108 result.start.assign("hour", result.start.get("hour") + 12);
109 }
110 });
111 return results;
112 },
113 });
114
115 testSingleCase(custom, "This is at 2.30", (result) => {
116 expect(result.text).toBe("at 2.30");
117 expect(result.start.get("hour")).toBe(14);
118 expect(result.start.get("minute")).toBe(30);
119 });
120
121 testSingleCase(custom, "This is at 2.30 AM", (result) => {
122 expect(result.text).toBe("at 2.30 AM");
123 expect(result.start.get("hour")).toBe(2);
124 expect(result.start.get("minute")).toBe(30);
125 });
126});
127
128test("Test - Add custom parser with tags example", () => {
129 const custom = chrono.casual.clone();
130 custom.parsers.push({
131 pattern: () => {
132 return /\bChristmas\b/i;
133 },
134 extract: (context) => {
135 return context
136 .createParsingComponents({
137 day: 25,
138 month: 12,
139 })
140 .addTag("parser/ChristmasDayParser");
141 },
142 });
143
144 testSingleCase(custom, "Doing something tomorrow", (result) => {
145 expect(result.text).toBe("tomorrow");
146 expect(result.tags()).toContain("parser/ENCasualDateParser");
147 });
148
149 testSingleCase(custom, "I'll arrive at 2.30AM on Christmas", (result) => {
150 expect(result.text).toBe("at 2.30AM on Christmas");
151 expect(result.tags()).toContain("parser/ChristmasDayParser");
152 expect(result.tags()).toContain("parser/ENTimeExpressionParser");
153 });
154
155 testSingleCase(custom, "I'll arrive at Christmas night", (result) => {
156 expect(result.text).toBe("Christmas night");
157 expect(result.tags()).toContain("parser/ChristmasDayParser");
158 expect(result.tags()).toContain("parser/ENCasualTimeParser");
159 });
160
161 // TODO: Check if the merge date range combine tags correctly
162});
163
164test("Test - Remove a parser example", () => {
165 const custom = chrono.en.strict.clone();
166 custom.parsers = custom.parsers.filter((r) => !(r instanceof SlashDateFormatParser));
167 custom.parsers.push(new SlashDateFormatParser(true));
168
169 testSingleCase(custom, "6/10/2018", (result) => {
170 expect(result.text).toBe("6/10/2018");
171 expect(result.start.get("year")).toBe(2018);
172 expect(result.start.get("month")).toBe(10);
173 expect(result.start.get("day")).toBe(6);
174 });
175});
176
177test("Test - Remove a refiner example", () => {
178 const custom = chrono.casual.clone();
179 custom.refiners = custom.refiners.filter((r) => !(r instanceof UnlikelyFormatFilter));
180
181 testSingleCase(custom, "This is at 2.30", (result) => {
182 expect(result.text).toBe("at 2.30");
183 expect(result.start.get("hour")).toBe(2);
184 expect(result.start.get("minute")).toBe(30);
185 });
186});
187
188test("Test - Replace a parser example", () => {
189 const custom = chrono.en.casual.clone();
190 testSingleCase(custom, "next 5m", new Date(2016, 10 - 1, 1, 14, 52), (result, text) => {
191 expect(result.start.get("hour")).toBe(14);
192 expect(result.start.get("minute")).toBe(57);
193 });
194 testSingleCase(custom, "next 5 minutes", new Date(2016, 10 - 1, 1, 14, 52), (result, text) => {
195 expect(result.start.get("hour")).toBe(14);
196 expect(result.start.get("minute")).toBe(57);
197 });
198
199 const index = custom.parsers.findIndex((r) => r instanceof ENTimeUnitCasualRelativeFormatParser);
200 custom.parsers[index] = new ENTimeUnitCasualRelativeFormatParser(false);
201 testUnexpectedResult(custom, "next 5m");
202 testSingleCase(custom, "next 5 minutes", new Date(2016, 10 - 1, 1, 14, 52), (result, text) => {
203 expect(result.start.get("hour")).toBe(14);
204 expect(result.start.get("minute")).toBe(57);
205 });
206});
207
208test("Test - Compare with native js", () => {
209 const testByCompareWithNative = (text) => {
210 const expectedDate = new Date(text);
211 testSingleCase(chrono, text, (result) => {
212 expect(result.text).toBe(text);
213 expect(result).toBeDate(expectedDate);
214 });
215 };
216
217 testByCompareWithNative("1994-11-05T13:15:30Z");
218
219 testByCompareWithNative("1994-02-28T08:15:30-05:30");
220
221 testByCompareWithNative("1994-11-05T08:15:30-05:30");
222
223 testByCompareWithNative("1994-11-05T08:15:30+11:30");
224
225 testByCompareWithNative("2014-11-30T08:15:30-05:30");
226
227 testByCompareWithNative("Sat, 21 Feb 2015 11:50:48 -0500");
228
229 testByCompareWithNative("22 Feb 2015 04:12:00 -0000");
230
231 testByCompareWithNative("1900-01-01T00:00:00-01:00");
232
233 testByCompareWithNative("1900-01-01T00:00:00-00:00");
234
235 testByCompareWithNative("9999-12-31T23:59:00-00:00");
236
237 testByCompareWithNative("09/25/2017 10:31:50.522 PM");
238
239 testByCompareWithNative("Sat Nov 05 1994 22:45:30 GMT+0900 (JST)");
240
241 testByCompareWithNative("Fri, 31 Mar 2000 07:00:00 UTC");
242
243 testByCompareWithNative("2014-12-14T18:22:14.759Z");
244
245 testByCompareWithNative("2024-01-01T00:00");
246});