-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuartzCroner.js
1624 lines (1051 loc) · 42.2 KB
/
QuartzCroner.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
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function quartzCronerFunctions() {
(function(global, factory) {
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, global.Cron = factory())
})(this, function() {
var maxRecordLevels = PropertiesService.getScriptProperties().getProperty('maxRecordLevels');
var timesThroughRecursion = 0;
/////////////////////////////////////
// Aaron Sheppard's Quartz Croner JS
// Modified version: 07-19-2023
//
// Updated version of the hexagon.github croner.js
// full credit for originals to the authors
// Croner - MIT License - Hexagon <github.com/Hexagon>
//
// Modified to accept Quartz Cron format with text weekdays
// as a stand-alone JS for use in Google Aps Script
// without any import or dependecies
////////////////////////////////////
function minitz(y, m, d, h, i, s, tz, throwOnInvalid) {
return minitz.fromTZ(minitz.tp(y, m, d, h, i, s, tz), throwOnInvalid);
}
minitz.fromTZISO = (localTimeStr, tz, throwOnInvalid) => {
return minitz.fromTZ(parseISOLocal(localTimeStr, tz), throwOnInvalid);
};
minitz.fromTZ = function(tp, throwOnInvalid) {
const
// Construct a fake Date object with UTC date/time set to local date/time in source timezone
inDate = new Date(Date.UTC(
tp.y,
tp.m - 1,
tp.d,
tp.h,
tp.i,
tp.s
)),
// Get offset between UTC and source timezone
offset = getTimezoneOffset(tp.tz, inDate),
// Remove offset from inDate to hopefully get a true date object
dateGuess = new Date(inDate.getTime() - offset),
// Get offset between UTC and guessed time in target timezone
dateOffsGuess = getTimezoneOffset(tp.tz, dateGuess);
// If offset between guessed true date object and UTC matches initial calculation, the guess
// was spot on
if ((dateOffsGuess - offset) === 0) {
return dateGuess;
} else {
// Not quite there yet, make a second try on guessing the local time, adjust by the offset indicated by the previous guess
// Try recreating input time again
// Then calculate and check the offset again
const
dateGuess2 = new Date(inDate.getTime() - dateOffsGuess),
dateOffsGuess2 = getTimezoneOffset(tp.tz, dateGuess2);
if ((dateOffsGuess2 - dateOffsGuess) === 0) {
// All good, return local time
return dateGuess2;
} else if(!throwOnInvalid && (dateOffsGuess2 - dateOffsGuess) > 0) {
// We're most probably dealing with a DST transition where we should use the offset of the second guess
return dateGuess2;
} else if (!throwOnInvalid) {
// We're most probably dealing with a DST transition where we should use the offset of the initial guess
return dateGuess;
} else {
// Input time is invalid, and the library is instructed to throw, so let's do it
throw new Error("Invalid date passed to fromTZ()");
}
}
};
minitz.toTZ = function (d, tzStr) {
// - replace narrow no break space with regular space to compensate for bug in Node.js 19.1
const localDateString = d.toLocaleString("en-US", {timeZone: tzStr}).replace(/[\u202f]/," ");
const td = new Date(localDateString);
return {
y: td.getFullYear(),
m: td.getMonth() + 1,
d: td.getDate(),
h: td.getHours(),
i: td.getMinutes(),
s: td.getSeconds(),
tz: tzStr
};
};
minitz.tp = (y,m,d,h,i,s,tz) => { return { y, m, d, h, i, s, tz: tz }; };
function getTimezoneOffset(timeZone, date = new Date()) {
// Get timezone
const tz = date.toLocaleString("en-US", {timeZone: timeZone, timeZoneName: "short"}).split(" ").slice(-1)[0];
// Extract time in en-US format
// - replace narrow no break space with regular space to compensate for bug in Node.js 19.1
const dateString = date.toLocaleString("en-US").replace(/[\u202f]/," ");
// Check ms offset between GMT and extracted timezone
return Date.parse(`${dateString} GMT`) - Date.parse(`${dateString} ${tz}`);
}
function parseISOLocal(dtStr, tz) {
// Parse date using built in Date.parse
const pd = new Date(Date.parse(dtStr));
// Check for completeness
if (isNaN(pd)) {
throw new Error("minitz: Invalid ISO8601 passed to parser.");
}
// If
// * date/time is specified in UTC (Z-flag included)
// * or UTC offset is specified (+ or - included after character 9 (20200101 or 2020-01-0))
// Return time in utc, else return local time and include timezone identifier
const stringEnd = dtStr.substring(9);
if (dtStr.includes("Z") || stringEnd.includes("-") || stringEnd.includes("+")) {
return minitz.tp(pd.getUTCFullYear(), pd.getUTCMonth()+1, pd.getUTCDate(),pd.getUTCHours(), pd.getUTCMinutes(),pd.getUTCSeconds(), "Etc/UTC");
} else {
return minitz.tp(pd.getFullYear(), pd.getMonth()+1, pd.getDate(),pd.getHours(), pd.getMinutes(),pd.getSeconds(), tz);
}
// Treat date as local time, in target timezone
}
minitz.minitz = minitz;
////////////////////////////////////
// This import is only used by tsc for generating type definitions from js/jsdoc
// deno-lint-ignore no-unused-vars
const DaysOfMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
const RecursionSteps = [
["month", "year", 0],
["day", "month", -1],
["hour", "day", 0],
["minute", "hour", 0],
["second", "minute", 0],
];
function CronDate (d, tz) {
this.tz = tz;
// Populate object using input date, or throw
if (d && d instanceof Date) {
if (!isNaN(d)) {
this.fromDate(d);
} else {
throw new TypeError("CronDate: Invalid date passed to CronDate constructor");
}
} else if (d === void 0) {
this.fromDate(new Date());
} else if (d && typeof d === "string") {
this.fromString(d);
} else if (d instanceof CronDate) {
this.fromCronDate(d);
} else {
throw new TypeError("CronDate: Invalid type (" + typeof d + ") passed to CronDate constructor");
}
}
CronDate.prototype.fromDate = function (inDate) {
if (this.tz !== void 0) {
if (typeof this.tz === "number") {
this.ms = inDate.getUTCMilliseconds();
this.second = inDate.getUTCSeconds();
this.minute = inDate.getUTCMinutes()+this.tz;
this.hour = inDate.getUTCHours();
this.day = inDate.getUTCDate();
this.month = inDate.getUTCMonth();
this.year = inDate.getUTCFullYear();
// Minute could be out of bounds, apply
this.apply();
} else {
const d = minitz.toTZ(inDate, this.tz);
this.ms = inDate.getMilliseconds();
this.second = d.s;
this.minute = d.i;
this.hour = d.h;
this.day = d.d;
this.month = d.m - 1;
this.year = d.y;
}
} else {
this.ms = inDate.getMilliseconds();
this.second = inDate.getSeconds();
this.minute = inDate.getMinutes();
this.hour = inDate.getHours();
this.day = inDate.getDate();
this.month = inDate.getMonth();
this.year = inDate.getFullYear();
}
};
CronDate.prototype.fromCronDate = function (d) {
this.tz = d.tz;
this.year = d.year;
this.month = d.month;
this.day = d.day;
this.hour = d.hour;
this.minute = d.minute;
this.second = d.second;
this.ms = d.ms;
};
CronDate.prototype.apply = function () {
// If any value could be out of bounds, apply
if (this.month>11||this.day>DaysOfMonth[this.month]||this.hour>59||this.minute>59||this.second>59||this.hour<0||this.minute<0||this.second<0) {
const d = new Date(Date.UTC(this.year, this.month, this.day, this.hour, this.minute, this.second, this.ms));
this.ms = d.getUTCMilliseconds();
this.second = d.getUTCSeconds();
this.minute = d.getUTCMinutes();
this.hour = d.getUTCHours();
this.day = d.getUTCDate();
this.month = d.getUTCMonth();
this.year = d.getUTCFullYear();
return true;
} else {
return false;
}
};
CronDate.prototype.fromString = function (str) {
return this.fromDate(minitz.fromTZISO(str, this.tz));
};
CronDate.prototype.findNext = function (options, target, pattern, offset) {
const originalTarget = this[target];
// In the conditions below, local time is not relevant. And as new Date(Date.UTC(y,m,d)) is way faster
// than new Date(y,m,d). We use the UTC functions to set/get date parts.
// Pre-calculate last day of month if needed
let lastDayOfMonth;
if (pattern.lastDayOfMonth || pattern.lastWeekdayOfMonth) {
// This is an optimization for every month except February, which has a different number of days in different years
if (this.month !== 1) {
lastDayOfMonth = DaysOfMonth[this.month];
} else {
lastDayOfMonth = new Date(Date.UTC(this.year, this.month + 1, 0, 0, 0, 0, 0)).getUTCDate();
}
}
// Pre-calculate weekday if needed
// Calculate offset weekday by ((fDomWeekDay + (targetDate - 1)) % 7)
const fDomWeekDay = (!pattern.starDOW && target === "day") ? new Date(Date.UTC(this.year, this.month, 1, 0, 0, 0, 0)).getUTCDay() : undefined;
for (let i = this[target] + offset; i < pattern[target].length; i++) {
// this applies to all "levels"
let match = pattern[target][i];
// Special case for last day of month
if (target === "day" && pattern.lastDayOfMonth && i - offset === lastDayOfMonth - 1) {
match = true;
}
// Special case for day of week
if (target === "day" && !pattern.starDOW) {
let dowMatch = pattern.dayOfWeek[(fDomWeekDay + ((i - offset) - 1)) % 7];
// Extra check for l-flag
if (dowMatch && pattern.lastWeekdayOfMonth) {
dowMatch = dowMatch && (i - offset + 1 > lastDayOfMonth - 7);
}
// If we use legacyMode, and dayOfMonth is specified - use "OR" to combine day of week with day of month
// In all other cases use "AND"
if (options.legacyMode && !pattern.starDOM) {
match = match || dowMatch;
} else {
match = match && dowMatch;
}
//console.log('date target='+target);
//console.log('date dowMatch='+dowMatch);
//console.log('date match='+match);
}
if (match) {
this[target] = i - offset;
//console.log('date if match='+(originalTarget !== this[target]) ? 2 : 1);
// Return 2 if changed, 1 if unchanged
return (originalTarget !== this[target]) ? 2 : 1;
}
}
// Return 3 if part was not matched
return 3;
};
CronDate.prototype.recurse = function (pattern, options, doing) {
// Find next month (or whichever part we're at)
const res = this.findNext(options, RecursionSteps[doing][0], pattern, RecursionSteps[doing][2]);
//console.log('timesThroughRecursion: '+timesThroughRecursion);
//console.log('maxRecordLevels: '+maxRecordLevels);
if (timesThroughRecursion < maxRecordLevels) {
// console.log('timesThroughRecursion: '+timesThroughRecursion);
// console.log('maxRecordLevels: '+maxRecordLevels);
// Month (or whichever part we're at) changed
if (res > 1) {
// Flag following levels for reset
let resetLevel = doing + 1;
while(resetLevel < RecursionSteps.length) {
this[RecursionSteps[resetLevel][0]] = -RecursionSteps[resetLevel][2];
resetLevel++;
}
// Parent changed
if (res=== 3) {
// Do increment parent, and reset current level
this[RecursionSteps[doing][1]]++;
this[RecursionSteps[doing][0]] = -RecursionSteps[doing][2];
this.apply();
// Restart
return this.recurse(pattern, options, 0);
} else if (this.apply()) {
return this.recurse(pattern, options, doing-1);
} else {
//must have this section with 'return this' to avoid infinite loops and memory overruns
return this;
}
//return;
}
//return;
}
// Move to next level
doing += 1;
// console.log('Utilities-Sleep Count of doing var: '+doing);
// Done?
if (doing >= RecursionSteps.length) {
//console.log('done?: '+pattern);
//Utilities.sleep(500);
return this;
// ... or out of bounds ?
} else if (this.year >= 2050) {
//console.log('out of bounds?: '+this);
return null;
// ... oh, go to next part then
} else {
//console.log('oh, go to next part then: '+this);
return this.recurse(pattern, options, doing);
}
//return;
};
CronDate.prototype.increment = function (pattern, options, hasPreviousRun) {
// Move to next second, or increment according to minimum interval indicated by option `interval: x`
// Do not increment a full interval if this is the very first run
this.second += (options.interval > 1 && hasPreviousRun) ? options.interval : 1;
// Always reset milliseconds, so we are at the next second exactly
this.ms = 0;
// Make sure seconds has not gotten out of bounds
this.apply();
// Recursively change each part (y, m, d ...) until next match is found, return null on failure
return this.recurse(pattern, options, 0);
};
function getNextDayOfWeek(field, year, month, day) {
const daysOfWeek = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
const cronDays = field.toLowerCase().split(',');
const currentDay = new Date(year, month - 1, day);
for (let i = 0; i < 7; i++) {
const nextDay = new Date(year, month - 1, day + i);
const nextDayOfWeek = nextDay.getDay();
const nextDayOfWeekStr = daysOfWeek[nextDayOfWeek].substring(0, 3);
console.log('getNextDayOfWeek - nextDay: '+nextDay);
console.log('getNextDayOfWeek - nextDayOfWeek: '+nextDayOfWeek);
console.log('getNextDayOfWeek - nextDayOfWeekStr: '+nextDayOfWeekStr);
if (cronDays.includes(nextDayOfWeekStr) && nextDay > currentDay) {
return nextDay;
}
}
return null;
}
CronDate.prototype.getDate = function (internal) {
// If this is an internal call, return the date as is
// Also use this option when no timezone or utcOffset is set
if (internal || this.tz === void 0) {
return new Date(this.year, this.month, this.day, this.hour, this.minute, this.second, this.ms);
} else {
// If .tz is a number, it indicates offset in minutes. UTC timestamp of the internal date objects will be off by the same number of minutes.
// Restore this, and return a date object with correct time set.
if (typeof this.tz === "number") {
return new Date(Date.UTC(this.year, this.month, this.day, this.hour, this.minute-this.tz, this.second, this.ms));
// If .tz is something else (hopefully a string), it indicates the timezone of the "local time" of the internal date object
// Use minitz to create a normal Date object, and return that.
} else {
return minitz(this.year, this.month+1, this.day, this.hour, this.minute, this.second, this.tz);
}
}
};
CronDate.prototype.getTime = function () {
return this.getDate().getTime();
};
//////////////////////////////////
function CronPattern(pattern) {
if (!(typeof pattern === "string" || pattern.constructor === String)) {
throw new TypeError("CronPattern: Pattern has to be of type string.");
}
this.pattern = pattern.trim();
this.lastDayOfMonth = false;
this.lastWeekdayOfMonth = false;
this.starDOM = false;
this.starDOW = false;
this.second = new Array(60).fill(0);
this.minute = new Array(60).fill(0);
this.hour = new Array(24).fill(0);
this.day = new Array(31).fill(0);
this.month = new Array(12).fill(0);
this.dayOfWeek = new Array(7).fill(0);
this.year = [];
this.parse();
}
CronPattern.prototype.parse = function () {
if (this.pattern.indexOf("@") >= 0) {
this.pattern = this.handleNicknames(this.pattern);
console.log('CronPattern-prototype-parse: found @: '+this.pattern);
}
const parts = this.pattern.replace(/\s+/g, " ").split(" ");
console.log('CronPattern-prototype-parse: parts length : '+parts.length);
console.log('CronPattern-prototype-parse: "second", parts[0]= '+parts[0]);
console.log('CronPattern-prototype-parse: "minute", parts[1]= '+parts[1]);
console.log('CronPattern-prototype-parse: "hour", parts[2]= '+parts[2]);
console.log('CronPattern-prototype-parse: "day", parts[3]= '+parts[3]);
console.log('CronPattern-prototype-parse: "month", parts[4]= '+parts[4]);
console.log('CronPattern-prototype-parse: "dayOfWeek", parts[5]= '+parts[5]);
console.log('CronPattern-prototype-parse: "year", parts[6]= '+parts[6]);
if (parts.length > 7) {
throw new TypeError(
"CronPattern: Invalid configuration format ('" +
this.pattern +
"'), exactly seven space-separated parts required."
);
}
if (parts.length <= 6) {
console.log('CronPattern-prototype-parse: parts-length <=6 : true');
parts[6] = '*';
}
//replacing "#" in DayOfWeek for Quartz syntax to work with the minitz library as a /
if(parts[5].indexOf("#") >= 0 ){
console.log('CronPattern-prototype-parse: parts5 has # : true');
parts[5] = parts[5].replace("#", "/")
};
//replacing "?" in DayOfWeek for Quartz syntax to work with the minitz library
if(parts[5] === "?"){
console.log('CronPattern-prototype-parse: parts5 has ? : true');
parts[5] = parts[5].replace("?", "*")
};
//replacing "-" in DayOfWeek for Quartz syntax to work with minitz comma delimited list
if(parts[5].indexOf("SAT-SUN") >= 0 ){
console.log('CronPattern-prototype-parse: parts5 has SAT-SUN : true');
parts[5] = parts[5].replace("-", ",")
};
if (parts[3] === "*" && parts[5] === "*") {
// Both dayOfMonth and dayOfWeek are set to *
console.log('CronPattern-prototype-parse: parts3 and parts5 has * : true');
this.starDOM = true;
this.starDOW = true;
} else if (parts[3] === "*") {
// Only dayOfMonth is set to *
console.log('CronPattern-prototype-parse: ONLY parts3 has * : true');
this.starDOM = true;
} else if (parts[5] === "*") {
// Only dayOfWeek is set to *
console.log('CronPattern-prototype-parse: ONLY parts5 has * : true');
this.starDOW = true;
}
if (parts[4].length >= 3) {
console.log('CronPattern-prototype-parse: parts4 length >=3 replaceAlphaDays : true');
parts[4] = this.replaceAlphaMonths(parts[4]);
}
if (parts[5].length >= 3) {
console.log('CronPattern-prototype-parse: parts5 length >=3 replaceAlphaDays : true');
parts[5] = this.replaceAlphaDays(parts[5]);
}
if (this.pattern.indexOf("?") >= 0) {
console.log('CronPattern-prototype-parse: index of ? >= : true');
const initDate = new CronDate(new Date(), this.timezone).getDate(true);
parts[0] = parts[0].replace("?", initDate.getSeconds());
parts[1] = parts[1].replace("?", initDate.getMinutes());
parts[2] = parts[2].replace("?", initDate.getHours());
if (!this.starDOM) parts[3] = parts[3].replace("?", initDate.getDate());
parts[4] = parts[4].replace("?", initDate.getMonth() + 1);
if (!this.starDOW) parts[5] = parts[5].replace("?", initDate.getDay());
}
this.throwAtIllegalCharacters(parts);
this.partToArray("second", parts[0], 0);
this.partToArray("minute", parts[1], 0);
this.partToArray("hour", parts[2], 0);
this.partToArray("day", parts[3], -1);
this.partToArray("month", parts[4], -1);
this.partToArray("dayOfWeek", parts[5], 0);
this.partToArray("year", parts[6], 0);
if (this.dayOfWeek[7]) {
console.log('CronPattern-prototype-parse: this-dayOfWeek[7] : true');
this.dayOfWeek[0] = 1;
}
};
CronPattern.prototype.partToArray = function (type, conf, valueIndexOffset) {
const arr = this[type];
if (conf === "*") {
console.log('CronPattern-prototype-partToArray: conf === * : true : fill(1)');
arr.fill(1);
return;
}
if (conf === "?") {
console.log('CronPattern-prototype-partToArray: conf === ? : true : fill(1)');
arr.fill(1);
return;
}
const split = conf.split(",");
if (split.length > 1) {
console.log('CronPattern-prototype-partToArray: conf split > 1 : true');
for (let i = 0; i < split.length; i++) {
this.partToArray(type, split[i], valueIndexOffset);
}
} else if (conf.indexOf("-") !== -1 && conf.indexOf("/") !== -1) {
console.log('CronPattern-prototype-partToArray: conf.indexOf("-") !== -1 && conf.indexOf("/") !== -1 : true');
this.handleRangeWithStepping(conf, type, valueIndexOffset);
} else if (conf.indexOf("-") !== -1) {
console.log('CronPattern-prototype-partToArray: conf.indexOf("-") !== -1 : true');
this.handleRange(conf, type, valueIndexOffset);
} else if (conf.indexOf("/") !== -1) {
console.log('CronPattern-prototype-partToArray: conf.indexOf("/") !== -1 : true');
this.handleStepping(conf, type, valueIndexOffset);
} else if (conf !== "") {
console.log('CronPattern-prototype-partToArray: conf !== "" : true');
this.handleNumber(conf, type, valueIndexOffset);
}
};
CronPattern.prototype.throwAtIllegalCharacters = function (parts) {
const reValidCron = /[^0-9*\/,\-\?^A-Z]+/;
for(let i = 0; i < parts.length; i++) {
if( reValidCron.test(parts[i]) ) {
throw new TypeError("CronPattern: configuration entry " + i + " (" + parts[i] + ") contains illegal characters.");
}
}
};
CronPattern.prototype.handleNumber = function (conf, type, valueIndexOffset) {
const i = parseInt(conf, 10) + valueIndexOffset;
console.log('CronPattern-prototype-handleNumber: type='+type);
console.log('CronPattern-prototype-handleNumber: i constant='+i);
if (isNaN(i)) {
throw new TypeError("CronPattern: " + type + " is not a number: '" + conf + "'");
}
let maxRange;
if (type === "dayOfWeek") {
console.log('CronPattern-prototype-handleNumber: type === "dayOfWeek": true');
maxRange = 7; // Day of week range: 0-6 (Sunday-Saturday)
} else if (type === "year") {
console.log('CronPattern-prototype-handleNumber: type === "year": true');
maxRange = 2050; // year range to 3000
} else {
console.log('CronPattern-prototype-handleNumber: type not dayOfWeek not Year: true : maxRange ='+this[type].length);
maxRange = this[type].length;
}
if (i < 0 || i > maxRange) {
throw new TypeError("CronPattern: " + type + " value out of range: '" + conf + "'");
}
this[type][i] = 1;
};
CronPattern.prototype.handleRangeWithStepping = function (conf, type, valueIndexOffset) {
const matches = conf.match(/^(\d+)-(\d+)\/(\d+)$/);
if (matches === null) {
throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '" + conf + "'");
}
let [, lower, upper, steps] = matches;
lower = parseInt(lower, 10) + valueIndexOffset;
upper = parseInt(upper, 10) + valueIndexOffset;
steps = parseInt(steps, 10);
console.log('CronPattern-prototype-handleRangeWithStepping: lower = '+lower+' upper= '+upper+' steps='+steps);
if (isNaN(lower)) {
throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");
}
if (isNaN(upper)) {
throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");
}
if (isNaN(steps)) {
throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");
}
if (steps === 0) {
throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");
}
if (steps > this[type].length) {
throw new TypeError(
"CronPattern: Syntax error, steps cannot be greater than maximum value of part (" +
this[type].length +
")"
);
}
if ( upper == 6 && lower == -7 ) {
console.log('CronPattern-prototype-handleRangeWithStepping: upper == 6 && lower == -7: true');
upper = 7;
lower = 6;
for (let i = lower; i <= upper; i++) {
this[type][i] = 1;
}
}
if ((lower < 0 & upper != -7) || upper >= this[type].length) {
//throw new TypeError("CronPattern: Value out of range: '" + conf + "'");
console.log('CronPattern-prototype-handleRangeWithStepping: (lower < 0 & upper != -7) || upper >= this[type].length : true');
for (let i = lower; i >= upper; i--) {
this[type][i] = 1;
}
}
if (lower > upper) {
//throw new TypeError("CronPattern: From value is larger than to value: '" + conf + "'");
console.log('CronPattern-prototype-handleRangeWithStepping: lower > upper : true');
for (let i = lower; i >= upper; i--) {
this[type][i] = 1;
}
}
for (let i = lower; i <= upper; i += steps) {
this[type][i] = 1;
}
};
CronPattern.prototype.handleRange = function (conf, type, valueIndexOffset) {
const split = conf.split("-");
console.log('CronPattern-prototype-handleRange: const split of - ='+split);
if (split.length !== 2) {
throw new TypeError("CronPattern: Syntax error, illegal range: '" + conf + "'");
}
const lower = parseInt(split[0], 10) + valueIndexOffset;
const upper = parseInt(split[1], 10) + valueIndexOffset;
if (isNaN(lower)) {
throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");
} else if (isNaN(upper)) {
throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");
}
if (lower == -7 && upper == 6){
console.log('CronPattern-prototype-handleRange: lower == -7 && upper == 6 : true');
lower = 6;
upper = 7;
for (let i = lower; i <= upper; i++) {
this[type][i] = 1;
}
}
if ( (lower < 0 && upper != -7) || upper >= this[type].length) {
//throw new TypeError("CronPattern: Value out of range: '" + conf + "'");
console.log('CronPattern-prototype-handleRange: (lower < 0 && upper != -7) || upper >= this[type].length : true');
for (let i = lower; i >= upper; i--) {
this[type][i] = 1;
}
}
if (lower > upper) {
//throw new TypeError("CronPattern: From value is larger than to value: '" + conf + "'");
console.log('CronPattern-prototype-handleRange: lower > upper : true');
for (let i = lower; i >= upper; i--) {
this[type][i] = 1;
}
}
for (let i = lower; i <= upper; i++) {
this[type][i] = 1;
}
};
CronPattern.prototype.handleStepping = function (conf, type, valueIndexOffset) {
const split = conf.split("/");
console.log('CronPattern-prototype-handleStepping: const split of / ='+split);
if (split.length !== 2) {
throw new TypeError("CronPattern: Syntax error, illegal stepping: '" + conf + "'");
}
let start = 0;
if (split[0] !== "*") {
console.log('CronPattern-prototype-handleStepping: split[0] !== "*" : true ');
start = parseInt(split[0], 10);
}
if (split[0] !== "?") {
console.log('CronPattern-prototype-handleStepping: split[0] !== "?" : true ');
start = parseInt(split[0], 10);
}
const steps = parseInt(split[1], 10);
if (isNaN(steps)) {
throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");
}
if (steps === 0) {
throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");
}
if (steps > this[type].length) {
throw new TypeError(
"CronPattern: Syntax error, max steps for part is (" + this[type].length + ")"
);
}
for (let i = start; i < this[type].length; i += steps) {