This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeridiem.test.ts
201 lines (173 loc) · 6.02 KB
/
meridiem.test.ts
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
import { parse } from "./src/index";
import { describe, expect, test } from "@jest/globals";
test("invalid date", () => {
expect(parse("invalid date")).toBeNull();
});
test("invalid date with date", () => {
expect(parse("invalid date", new Date("January 1, 2022"))).toBeNull();
});
test("slash format date range", () => {
expect(parse("1/1 - 1/2", new Date("January 5, 2022"))).toEqual({
start: new Date("January 1, 2022"),
end: new Date("January 2, 2022"),
});
});
test("slash format date range with year", () => {
expect(parse("1/1/2022 - 1/2/2022", new Date("January 5, 2022"))).toEqual({
start: new Date("January 1, 2022"),
end: new Date("January 2, 2022"),
});
});
describe("relative ranges", () => {
test("weird capitalization", () => {
expect(parse("lASt wEEk", new Date("January 5, 2022"))).toEqual({
start: new Date("December 29, 2021"),
end: new Date("January 5, 2022"),
});
});
test("last week", () => {
expect(parse("last week", new Date("January 5, 2022"))).toEqual({
start: new Date("December 29, 2021"),
end: new Date("January 5, 2022"),
});
});
test("yesterday", () => {
expect(parse("yesterday", new Date("January 5, 2022"))).toEqual({
start: new Date("January 4, 2022"),
end: new Date("January 5, 2022"),
});
});
test("today", () => {
const newDate = new Date("January 5, 2022");
newDate.setHours(0, 0, 0, 0);
expect(parse("today", new Date("January 5, 2022"))).toEqual({
start: newDate,
end: new Date("January 5, 2022"),
});
});
test("day before yesterday", () => {
expect(parse("day before yesterday", new Date("January 5, 2022"))).toEqual({
start: new Date("January 3, 2022"),
end: new Date("January 5, 2022"),
});
});
test("last month", () => {
expect(parse("last month", new Date("January 5, 2022"))).toEqual({
start: new Date("December 5, 2021"),
end: new Date("January 5, 2022"),
});
});
test("year to date", () => {
expect(parse("year to date", new Date("January 5, 2022"))).toEqual({
start: new Date("January 1, 2022"),
end: new Date("January 5, 2022"),
});
expect(parse("ytd", new Date("January 5, 2022"))).toEqual({
start: new Date("January 1, 2022"),
end: new Date("January 5, 2022"),
});
});
test("last hour", () => {
expect(parse("last hour", new Date("January 5, 2022"))).toEqual({
start: new Date("January 4, 2022 23:00"),
end: new Date("January 5, 2022 00:00"),
});
expect(parse("last hr", new Date("January 5, 2022"))).toEqual({
start: new Date("January 4, 2022 23:00"),
end: new Date("January 5, 2022 00:00"),
});
});
test("hours", () => {
expect(parse("last 12 hours", new Date("January 5, 2022"))).toEqual({
start: new Date("January 4, 2022 12:00"),
end: new Date("January 5, 2022 00:00"),
});
expect(parse("last 12 hrs", new Date("January 5, 2022"))).toEqual({
start: new Date("January 4, 2022 12:00"),
end: new Date("January 5, 2022 00:00"),
});
expect(parse("12h", new Date("January 5, 2022"))).toEqual({
start: new Date("January 4, 2022 12:00"),
end: new Date("January 5, 2022 00:00"),
});
expect(parse("12hrs", new Date("January 5, 2022"))).toEqual({
start: new Date("January 4, 2022 12:00"),
end: new Date("January 5, 2022 00:00"),
});
expect(
parse(
"detect the range $$$!!! hello last 12 hours other content power of regex 12312",
new Date("January 5, 2022")
)
).toEqual({
start: new Date("January 4, 2022 12:00"),
end: new Date("January 5, 2022 00:00"),
});
});
test("months", () => {
expect(parse("last 2 months", new Date("January 5, 2022"))).toEqual({
start: new Date("November 5, 2021"),
end: new Date("January 5, 2022"),
});
});
test("years", () => {
expect(parse("last 5 years", new Date("January 5, 2022"))).toEqual({
start: new Date("January 5, 2017"),
end: new Date("January 5, 2022"),
});
expect(parse("5y", new Date("January 5, 2022"))).toEqual({
start: new Date("January 5, 2017"),
end: new Date("January 5, 2022"),
});
expect(parse("5 yrs", new Date("January 5, 2022"))).toEqual({
start: new Date("January 5, 2017"),
end: new Date("January 5, 2022"),
});
});
test("weeks", () => {
expect(parse("last 2 weeks", new Date("January 5, 2022"))).toEqual({
start: new Date("December 22, 2021"),
end: new Date("January 5, 2022"),
});
expect(parse("2w", new Date("January 5, 2022"))).toEqual({
start: new Date("December 22, 2021"),
end: new Date("January 5, 2022"),
});
expect(parse("2 wk", new Date("January 5, 2022"))).toEqual({
start: new Date("December 22, 2021"),
end: new Date("January 5, 2022"),
});
});
test("minutes", () => {
expect(parse("last 5 minutes", new Date("January 5, 2022 00:05"))).toEqual({
start: new Date("January 5, 2022"),
end: new Date("January 5, 2022 00:05"),
});
expect(parse("5min", new Date("January 5, 2022 00:05"))).toEqual({
start: new Date("January 5, 2022"),
end: new Date("January 5, 2022 00:05"),
});
expect(parse("5 mins", new Date("January 5, 2022 00:05"))).toEqual({
start: new Date("January 5, 2022"),
end: new Date("January 5, 2022 00:05"),
});
expect(parse("5m", new Date("January 5, 2022 00:05"))).toEqual({
start: new Date("January 5, 2022"),
end: new Date("January 5, 2022 00:05"),
});
});
test("days", () => {
expect(parse("last 5 days", new Date("January 5, 2022"))).toEqual({
start: new Date("December 31, 2021"),
end: new Date("January 5, 2022"),
});
expect(parse("5d", new Date("January 5, 2022"))).toEqual({
start: new Date("December 31, 2021"),
end: new Date("January 5, 2022"),
});
expect(parse("5 day", new Date("January 5, 2022"))).toEqual({
start: new Date("December 31, 2021"),
end: new Date("January 5, 2022"),
});
});
});