-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathcronstrue.ts
769 lines (621 loc) · 26.2 KB
/
cronstrue.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
import 'mocha';
import chai = require("chai");
import cronstrue from "../src/cronstrue";
let assert = chai.assert;
describe("Cronstrue", function () {
describe("every", function () {
it("* * * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every second");
});
it("* * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute");
});
it("0 * * * * (verbose)", function () {
assert.equal(cronstrue.toString("0 * * * *", { verbose: true }), "Every hour, every day");
});
it("* * * * * (verbose)", function () {
assert.equal(cronstrue.toString("* * * * *", { verbose: true }), "Every minute, every hour, every day");
});
it("*/1 * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute");
});
it("*/5 * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every 5 minutes");
});
it("0 0/1 * * * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute");
});
it("0 0 * * * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every hour");
});
it("0 0 0/1 * * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every hour");
});
it("* * * 3 *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, only in March");
});
it("* * * 3,6 *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, only in March and June");
});
it("* * * * * * 2013", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every second, only in 2013");
});
it("* * * * * 2013", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, only in 2013");
});
it("* * * * * 2013,2014", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, only in 2013 and 2014");
});
});
describe("interval", function () {
it("*/45 * * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every 45 seconds");
});
it("*/5 * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every 5 minutes");
});
it("*/10 * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every 10 minutes");
});
it("0 */5 * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every 5 minutes");
});
it("0 9-17 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every hour, between 09:00 AM and 05:00 PM");
});
it("0 * ? * 2/1 *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every hour, Tuesday through Saturday");
});
it("0 * ? * 1/1", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every hour, Monday through Saturday");
});
it("0 * ? * 2/1", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every hour, Tuesday through Saturday");
});
it("0 52 13 ? * 3/1", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 01:52 PM, Wednesday through Saturday");
});
});
describe("ranges", function () {
it("0 23 ? * MON-FRI", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 11:00 PM, Monday through Friday");
});
it("30 11 * * 1-5", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 11:30 AM, Monday through Friday");
});
it("0-10 11 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute between 11:00 AM and 11:10 AM");
});
it("23 12 * Jan-Mar *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:23 PM, January through March");
});
it("23 12 * JAN-FEB *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:23 PM, January through February");
});
it("1 1,3-4 * * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 1 minutes past the hour, at 01:00 AM and 03:00 AM through 04:59 AM"
);
});
it("* 0 */4 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every second, at 0 minutes past the hour, every 4 hours");
});
it("*/10 0 * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every 10 seconds, at 0 minutes past the hour");
});
it("* 0 0 * * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"Every second, at 0 minutes past the hour, between 12:00 AM and 12:00 AM"
);
});
it("* 0 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, between 12:00 AM and 12:59 AM");
});
it("* 0 * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every second, at 0 minutes past the hour");
});
});
describe("at", function () {
it("30 11 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: 3
}), "At 02:30 PM");
});
it("31 10 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: 5.5
}), "At 04:01 PM");
});
it("29 10 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: 5.5
}), "At 03:59 PM");
});
it("30 10 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: 5.5
}), "At 04:00 PM");
});
it("31 10 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: -1.5
}), "At 09:01 AM");
});
it("29 10 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: -1.5
}), "At 08:59 AM");
});
it("30 10 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: -1.5
}), "At 09:00 AM");
});
it("30 11 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: 3,
use24HourTimeFormat: true
}), "At 14:30");
});
it("30 3 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: -5,
use24HourTimeFormat: true
}), "At 22:30");
});
it("10 11 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: -4,
verbose: true
}), "At 07:10 AM, every day");
});
it("30 22 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: 5,
use24HourTimeFormat: true
}), "At 03:30");
});
it("5 1 * * 1", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: -2,
}), "At 11:05 PM, only on Sunday");
});
it("5 23 * * 1", function () {
assert.equal(cronstrue.toString(this.test?.title as string, {
tzOffset: 2,
}), "At 01:05 AM, only on Tuesday");
});
it("30 11 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 11:30 AM");
});
it("23 12 * * SUN", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:23 PM, only on Sunday");
});
it("30 02 14 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 02:02:30 PM");
});
it("0 0 6 1/1 * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 06:00 AM");
});
it("0 5 0/1 * * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 5 minutes past the hour");
});
it("46 9 * * 1", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 09:46 AM, only on Monday");
});
it("46 9 * * 7", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 09:46 AM, only on Sunday", "7 should mean Sunday");
});
it("23 12 15 * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:23 PM, on day 15 of the month");
});
it("23 12 * JAN *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:23 PM, only in January");
});
it("23 12 ? JAN *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:23 PM, only in January");
});
it("0 7 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 07:00 AM", "trailing space");
});
it("30 14,16 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 02:30 PM and 04:30 PM");
});
it("30 6,14,16 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 06:30 AM, 02:30 PM and 04:30 PM");
});
it("0 * 31 * 1", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every hour, on day 31 of the month, and on Monday");
});
it("0 45 12 22,17,6,30,26 * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:45 PM, on day 6, 17, 22, 26, and 30 of the month");
});
});
describe("weekday", function () {
it("* * LW * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the last weekday of the month");
});
it("* * WL * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the last weekday of the month");
});
it("* * 1W * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the first weekday of the month");
});
it("* * 13W * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the weekday nearest day 13 of the month");
});
it("* * W1 * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the first weekday of the month");
});
it("* * 5W * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the weekday nearest day 5 of the month");
});
it("* * W5 * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the weekday nearest day 5 of the month");
});
});
describe("last", function () {
it("* * * * 4L", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the last Thursday of the month");
});
it("*/5 * L JAN *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"Every 5 minutes, on the last day of the month, only in January"
);
});
it("0 20 15,L * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 08:00 PM, on day 15 and the last day of the month");
});
it("0 20 1-10,20-L * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 08:00 PM, on day 1 through 10 and 20 through the last day of the month"
);
});
it("0 15 10 * * L", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 10:15 AM, only on Saturday");
});
it("0 15 10 L * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 10:15 AM, on the last day of the month");
});
it("0 0 0 L-5 * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:00 AM, 5 days before the last day of the month");
});
});
describe("dayOfWeekStartIndexZero=false", function () {
it("23 12 * * 1#2", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { dayOfWeekStartIndexZero: false }),
"At 12:23 PM, on the second Sunday of the month"
);
});
it("* * * ? * 2-6/2", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { dayOfWeekStartIndexZero: false }),
"Every second, every 2 days of the week, Monday through Friday"
);
});
it("* * * ? * 7", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { dayOfWeekStartIndexZero: false }),
"Every second, only on Saturday"
);
});
it("* * * ? * 1,2,3,4,5", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { dayOfWeekStartIndexZero: false }),
"Every second, only on Sunday, Monday, Tuesday, Wednesday, and Thursday"
);
});
it("0 * ? * 1/1", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { dayOfWeekStartIndexZero: false }),
"Every hour, Sunday through Saturday"
);
});
it("0 * ? * 2/1", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { dayOfWeekStartIndexZero: false }),
"Every hour, Monday through Saturday"
);
});
});
describe("monthStartIndexZero=true", function () {
it("* * * 7 *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { monthStartIndexZero: true }),
"Every minute, only in August"
);
});
it("30 * * 6-8 *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { monthStartIndexZero: true }),
"At 30 minutes past the hour, July through September"
);
});
it("30 * * 1-10/2 *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { monthStartIndexZero: true }),
"At 30 minutes past the hour, every 2 months, February through November"
);
});
it("30 * * 4,5,6 *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { monthStartIndexZero: true }),
"At 30 minutes past the hour, only in May, June, and July"
);
});
it("30 * * JAN *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { monthStartIndexZero: true }),
"At 30 minutes past the hour, only in January"
);
});
});
describe("non-trivial expressions", function () {
it("*/5 15 * * MON-FRI", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"Every 5 minutes, between 03:00 PM and 03:59 PM, Monday through Friday"
);
});
it("* * * * MON#3", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the third Monday of the month");
});
it("* * * * MON#3,THU#1", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every minute, on the third Monday and first Thursday of the month");
});
it("5-10 * * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Seconds 5 through 10 past the minute");
});
it("5-10 30-35 10-12 * * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"Seconds 5 through 10 past the minute, minutes 30 through 35 past the hour, between 10:00 AM and 12:59 PM"
);
});
it("30 */5 * * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 30 seconds past the minute, every 5 minutes");
});
it("10 0/5 * * * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 10 seconds past the minute, every 5 minutes");
});
it("2-59/3 1,9,22 11-26 1-6 ?", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"Every 3 minutes, minutes 2 through 59 past the hour, at 01:00 AM, 09:00 AM, and 10:00 PM, between day 11 and 26 of the month, January through June"
);
});
it("23 12 * JAN-FEB * 2013-2014", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:23 PM, January through February, 2013 through 2014");
});
it("23 12 * JAN-MAR * 2013-2015", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:23 PM, January through March, 2013 through 2015");
});
it("12-50 0-10 6 * * * 2022", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"Seconds 12 through 50 past the minute, minutes 0 through 10 past the hour, at 06:00 AM, only in 2022"
);
});
it("0 0/30 8-9 5,20 * ?", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"Every 30 minutes, between 08:00 AM and 09:59 AM, on day 5 and 20 of the month"
);
});
it("23 12 * * SUN#2", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:23 PM, on the second Sunday of the month");
});
it("0 25 7-19/8 ? * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 25 minutes past the hour, every 8 hours, between 07:00 AM and 07:59 PM"
);
});
it("0 25 7-20/13 ? * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 25 minutes past the hour, every 13 hours, between 07:00 AM and 08:59 PM"
);
});
it("0 0 8 1/3 * ? *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 08:00 AM, every 3 days");
});
it("0 15 10 ? * */3", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 10:15 AM, every 3 days of the week");
});
it("* * * ? * 1-5/2", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every second, every 2 days of the week, Monday through Friday");
});
it("0 5 7 2 1/3 ? *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 07:05 AM, on day 2 of the month, every 3 months");
});
it("0 15 6 1 1 ? 1/2", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 06:15 AM, on day 1 of the month, only in January, every 2 years"
);
});
it("2,4-5 1 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 2 and 4 through 5 minutes past the hour, at 01:00 AM");
});
it("2,26-28 18 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 2 and 26 through 28 minutes past the hour, at 06:00 PM");
});
it("5/30 * * * * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every 30 seconds, starting at 5 seconds past the minute");
});
it("0 5/30 * * * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every 30 minutes, starting at 5 minutes past the hour");
});
it("* * 5/8 * * ?", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every second, every 8 hours, starting at 05:00 AM");
});
it("0 5 7 2/3 * ? *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 07:05 AM, every 3 days, starting on day 2 of the month");
});
it("0 5 7 ? 3/2 ? *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 07:05 AM, every 2 months, March through December");
});
it("0 5 7 ? * 2/3 *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 07:05 AM, every 3 days of the week, Tuesday through Saturday"
);
});
it("0 5 7 ? * ? 2016/4", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 07:05 AM, every 4 years, 2016 through 9999");
});
it("0 30 10-13 ? * wed,FRI", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 30 minutes past the hour, between 10:00 AM and 01:59 PM, only on Wednesday and Friday"
);
});
it("0 00 10 ? * MON-THU,SUN *", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 10:00 AM, only on Monday through Thursday and Sunday");
});
it("0 0 0 1,2,3 * WED,FRI", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 12:00 AM, on day 1, 2, and 3 of the month, and on Wednesday and Friday"
);
});
it("0 2,16 1,8,15,22 * 1,2", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 02:00 AM and 04:00 PM, on day 1, 8, 15, and 22 of the month, and on Monday and Tuesday"
);
});
it("0 */4,6 * * * ", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 0 minutes past the hour, every 4 hours and at 06:00 AM");
});
it("5 30 6,14,16 5 * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"At 5 seconds past the minute, at 30 minutes past the hour, at 06:00 AM, 02:00 PM, and 04:00 PM, on day 5 of the month"
);
});
it("0-20/3 9 * * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"Every 3 minutes, minutes 0 through 20 past the hour, between 09:00 AM and 09:59 AM"
);
});
it("5-45/10,*/5,9 * * * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string),
"Every 10 minutes, minutes 5 through 45 past the hour, every 5 minutes, and at 9 minutes past the hour"
);
});
});
describe("@ expressions", function () {
it("@yearly", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:00 AM, on day 1 of the month, only in January");
});
it("@annually", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:00 AM, on day 1 of the month, only in January");
});
it("@monthly", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:00 AM, on day 1 of the month");
});
it("@weekly", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:00 AM, only on Sunday");
});
it("@daily", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:00 AM");
});
it("@midnight", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 12:00 AM");
});
it("@hourly", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "Every hour");
});
});
describe("verbose", function () {
it("30 4 1 * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, { verbose: true }), "At 04:30 AM, on day 1 of the month");
});
it("0 13 * * 1", function () {
assert.equal(cronstrue.toString(this.test?.title as string, { verbose: true }), "At 01:00 PM, only on Monday");
});
});
describe("errors", function () {
it('second out of range', function () {
assert.throws(function () {
cronstrue.toString("61 * * * * *");
}, "seconds part must be >= 0 and <= 59")
});
it('minute out of range', function () {
assert.throws(function () {
cronstrue.toString("0 -1 * * * *");
}, "minutes part must be >= 0 and <= 59")
});
it('hour out of range', function () {
assert.throws(function () {
cronstrue.toString("0 0 24 * * *");
}, "hours part must be >= 0 and <= 23")
});
it('dayOfMonth out of range', function () {
assert.throws(function () {
cronstrue.toString("0 0 0 32 * *");
}, "DOM part must be >= 1 and <= 31")
});
it('month out of range', function () {
assert.throws(function () {
cronstrue.toString("0 0 0 1 13 *", { monthStartIndexZero: false });
}, "month part must be >= 1 and <= 12")
assert.throws(function () {
cronstrue.toString("0 0 0 1 13 *", { monthStartIndexZero: true });
}, "month part must be >= 0 and <= 11")
});
it('dayOfWeek out of range', function () {
assert.throws(function () {
cronstrue.toString("0 0 0 1 12 8", { dayOfWeekStartIndexZero: true });
}, "DOW part must be >= 0 and <= 6");
assert.throws(function () {
cronstrue.toString("0 0 0 1 12 8", { dayOfWeekStartIndexZero: false });
}, "DOW part must be >= 1 and <= 7");
});
it("garbage expression", function () {
assert.throws(function () {
cronstrue.toString("sdlksldksldksd");
}, "Error: Expression has only 1 part. At least 5 parts are required.");
});
it("too many parts", function () {
assert.throws(function () {
cronstrue.toString("* * * * * * * *");
}, "Expression has 8 parts; too many!");
});
it("empty expression", function () {
assert.throws(function () {
cronstrue.toString("");
}, "Error: cron expression is empty");
});
it("null expression", function () {
assert.throws(function () {
cronstrue.toString(null as any as string);
}, "Error: cron expression is empty");
});
it("undefined expression", function () {
assert.throws(function () {
cronstrue.toString("");
}, "Error: cron expression is empty");
});
it("'W' list is invalid", function () {
assert.throws(function () {
cronstrue.toString("0 30 14 1W,15W * ? *");
}, "Error: The 'W' character can be specified only when the day-of-month is a single day, not a range or list of days.");
});
it("garbage expression with option (throwExceptionOnParseError = false)", function () {
assert.equal(
cronstrue.toString("garbage", { throwExceptionOnParseError: false }),
"An error occured when generating the expression description. Check the cron expression syntax."
);
});
});
});