@@ -23,9 +23,11 @@ export enum CronPeriod {
23
23
Month ,
24
24
}
25
25
26
- export type SecondOrMinuteValue = 0 | 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 ;
27
- export type HourValue = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 ;
26
+ export type SecondOrMinute = 0 | 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 ;
27
+ export type Hour = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 ;
28
28
export type DayOfMonth = 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 ;
29
+ export type DayOfWeekNumber = 0 | 1 | 2 | 3 | 4 | 5 | 6 ;
30
+ export type MonthNumber = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 ;
29
31
30
32
export enum DayOfWeek {
31
33
Sunday = "SUN" ,
@@ -37,6 +39,8 @@ export enum DayOfWeek {
37
39
Saturday = "SAT" ,
38
40
}
39
41
42
+ export type DayOfWeekValue = DayOfWeek | DayOfWeekNumber ;
43
+
40
44
export enum Month {
41
45
January = "JAN" ,
42
46
February = "FEB" ,
@@ -52,35 +56,53 @@ export enum Month {
52
56
December = "DEC" ,
53
57
}
54
58
59
+ export type MonthValue = Month | MonthNumber ;
60
+
55
61
export class CronExpressionBuilder {
56
62
public static on (
57
63
period : CronPeriod . Second ,
58
- ...values : ( SecondOrMinuteValue [ ] | SecondOrMinuteValue ) [ ]
64
+ ...values : ( SecondOrMinute [ ] | SecondOrMinute ) [ ]
59
65
) : CronExpression ;
60
66
public static on (
61
67
period : CronPeriod . Minute ,
62
- ...values : ( SecondOrMinuteValue [ ] | SecondOrMinuteValue ) [ ]
68
+ ...values : ( SecondOrMinute [ ] | SecondOrMinute ) [ ]
63
69
) : CronExpression ;
64
- public static on ( period : CronPeriod . Hour , ...values : ( HourValue [ ] | HourValue ) [ ] ) : CronExpression ;
65
- public static on ( period : CronPeriod . DayOfWeek , ...values : ( DayOfWeek [ ] | DayOfWeek ) [ ] ) : CronExpression ;
70
+ public static on ( period : CronPeriod . Hour , ...values : ( Hour [ ] | Hour ) [ ] ) : CronExpression ;
71
+ public static on ( period : CronPeriod . DayOfWeek , ...values : ( DayOfWeekValue [ ] | DayOfWeekValue ) [ ] ) : CronExpression ;
66
72
public static on ( period : CronPeriod . DayOfMonth , ...values : ( DayOfMonth [ ] | DayOfMonth ) [ ] ) : CronExpression ;
67
- public static on ( period : CronPeriod . Month , ...values : ( Month [ ] | Month ) [ ] ) : CronExpression ;
73
+ public static on ( period : CronPeriod . Month , ...values : ( MonthValue [ ] | MonthValue ) [ ] ) : CronExpression ;
68
74
public static on ( period : any , ...values : ( any [ ] | any ) [ ] ) {
69
75
return new CronExpression ( ) . on ( period , values . flat ( ) ) ;
70
76
}
71
77
72
- public static through ( period : CronPeriod . Second , from : SecondOrMinuteValue , to : SecondOrMinuteValue ) : CronExpression ;
73
- public static through ( period : CronPeriod . Minute , from : SecondOrMinuteValue , to : SecondOrMinuteValue ) : CronExpression ;
74
- public static through ( period : CronPeriod . Hour , from : HourValue , to : HourValue ) : CronExpression ;
75
- public static through ( period : CronPeriod . DayOfWeek , from : DayOfWeek , to : DayOfWeek ) : CronExpression ;
78
+ public static through ( period : CronPeriod . Second , from : SecondOrMinute , to : SecondOrMinute ) : CronExpression ;
79
+ public static through ( period : CronPeriod . Minute , from : SecondOrMinute , to : SecondOrMinute ) : CronExpression ;
80
+ public static through ( period : CronPeriod . Hour , from : Hour , to : Hour ) : CronExpression ;
81
+ public static through ( period : CronPeriod . DayOfWeek , from : DayOfWeekValue , to : DayOfWeekValue ) : CronExpression ;
76
82
public static through ( period : CronPeriod . DayOfMonth , from : DayOfMonth , to : DayOfMonth ) : CronExpression ;
77
- public static through ( period : CronPeriod . Month , from : Month , to : Month ) : CronExpression ;
78
- public static through ( period : any , from : any , to : any ) {
83
+ public static through ( period : CronPeriod . Month , from : MonthValue , to : MonthValue ) : CronExpression ;
84
+ public static through ( period : any , from : any , to : any ) : CronExpression {
79
85
return new CronExpression ( ) . through ( period , from , to ) ;
80
86
}
81
87
82
- public static every ( period : CronPeriod ) {
83
- return new CronExpression ( ) . every ( period ) ;
88
+ public static each ( period : CronPeriod . Second ) : CronExpression ;
89
+ public static each ( period : CronPeriod . Minute ) : CronExpression ;
90
+ public static each ( period : CronPeriod . Hour ) : CronExpression ;
91
+ public static each ( period : CronPeriod . DayOfWeek ) : CronExpression ;
92
+ public static each ( period : CronPeriod . DayOfMonth ) : CronExpression ;
93
+ public static each ( period : CronPeriod . Month ) : CronExpression ;
94
+ public static each ( period : any ) {
95
+ return new CronExpression ( ) . each ( period ) ;
96
+ }
97
+
98
+ public static every ( period : CronPeriod . Second , interval : SecondOrMinute ) : CronExpression ;
99
+ public static every ( period : CronPeriod . Minute , interval : SecondOrMinute ) : CronExpression ;
100
+ public static every ( period : CronPeriod . Hour , interval : Hour ) : CronExpression ;
101
+ public static every ( period : CronPeriod . DayOfWeek , interval : DayOfWeekValue ) : CronExpression ;
102
+ public static every ( period : CronPeriod . DayOfMonth , interval : DayOfMonth ) : CronExpression ;
103
+ public static every ( period : CronPeriod . Month , interval : MonthValue ) : CronExpression ;
104
+ public static every ( period : any , interval : any ) {
105
+ return new CronExpression ( ) . every ( period , interval ) ;
84
106
}
85
107
}
86
108
@@ -106,12 +128,12 @@ export class CronExpression {
106
128
private month = "*" ;
107
129
private dayOfWeek = "*" ;
108
130
109
- public on ( period : CronPeriod . Second , ...values : ( SecondOrMinuteValue [ ] | SecondOrMinuteValue ) [ ] ) : this;
110
- public on ( period : CronPeriod . Minute , ...values : ( SecondOrMinuteValue [ ] | SecondOrMinuteValue ) [ ] ) : this;
111
- public on ( period : CronPeriod . Hour , ...values : ( HourValue [ ] | HourValue ) [ ] ) : this;
112
- public on ( period : CronPeriod . DayOfWeek , ...values : ( DayOfWeek [ ] | DayOfWeek ) [ ] ) : this;
131
+ public on ( period : CronPeriod . Second , ...values : ( SecondOrMinute [ ] | SecondOrMinute ) [ ] ) : this;
132
+ public on ( period : CronPeriod . Minute , ...values : ( SecondOrMinute [ ] | SecondOrMinute ) [ ] ) : this;
133
+ public on ( period : CronPeriod . Hour , ...values : ( Hour [ ] | Hour ) [ ] ) : this;
134
+ public on ( period : CronPeriod . DayOfWeek , ...values : ( DayOfWeekValue [ ] | DayOfWeekValue ) [ ] ) : this;
113
135
public on ( period : CronPeriod . DayOfMonth , ...values : ( DayOfMonth [ ] | DayOfMonth ) [ ] ) : this;
114
- public on ( period : CronPeriod . Month , ...values : ( Month [ ] | Month ) [ ] ) : this;
136
+ public on ( period : CronPeriod . Month , ...values : ( MonthValue [ ] | MonthValue ) [ ] ) : this;
115
137
public on ( period : any , ...values : ( any [ ] | any ) [ ] ) : this {
116
138
const fixedValues = values . flat ( ) ;
117
139
@@ -139,13 +161,13 @@ export class CronExpression {
139
161
return this ;
140
162
}
141
163
142
- public through ( period : CronPeriod . Second , from : SecondOrMinuteValue , to : SecondOrMinuteValue ) : CronExpression ;
143
- public through ( period : CronPeriod . Minute , from : SecondOrMinuteValue , to : SecondOrMinuteValue ) : CronExpression ;
144
- public through ( period : CronPeriod . Hour , from : SecondOrMinuteValue , to : SecondOrMinuteValue ) : CronExpression ;
145
- public through ( period : CronPeriod . DayOfWeek , from : DayOfWeek , to : DayOfWeek ) : CronExpression ;
164
+ public through ( period : CronPeriod . Second , from : SecondOrMinute , to : SecondOrMinute ) : CronExpression ;
165
+ public through ( period : CronPeriod . Minute , from : SecondOrMinute , to : SecondOrMinute ) : CronExpression ;
166
+ public through ( period : CronPeriod . Hour , from : SecondOrMinute , to : SecondOrMinute ) : CronExpression ;
167
+ public through ( period : CronPeriod . DayOfWeek , from : DayOfWeekValue , to : DayOfWeekValue ) : CronExpression ;
146
168
public through ( period : CronPeriod . DayOfMonth , from : DayOfMonth , to : DayOfMonth ) : CronExpression ;
147
- public through ( period : CronPeriod . Month , from : Month , to : Month ) : CronExpression ;
148
- public through ( period : any , from : any , to : any ) {
169
+ public through ( period : CronPeriod . Month , from : MonthValue , to : MonthValue ) : CronExpression ;
170
+ public through ( period : any , from : any , to : any ) : this {
149
171
switch ( period ) {
150
172
case CronPeriod . Second :
151
173
this . seconds = this . prepareSecondOrMinuteValueRange ( from , to ) ;
@@ -170,7 +192,13 @@ export class CronExpression {
170
192
return this ;
171
193
}
172
194
173
- public every ( period : CronPeriod ) : this {
195
+ public each ( period : CronPeriod . Second ) : this;
196
+ public each ( period : CronPeriod . Minute ) : this;
197
+ public each ( period : CronPeriod . Hour ) : this;
198
+ public each ( period : CronPeriod . DayOfWeek ) : this;
199
+ public each ( period : CronPeriod . DayOfMonth ) : this;
200
+ public each ( period : CronPeriod . Month ) : this;
201
+ public each ( period : any ) : this {
174
202
switch ( period ) {
175
203
case CronPeriod . Second :
176
204
this . seconds = "*" ;
@@ -194,11 +222,44 @@ export class CronExpression {
194
222
return this ;
195
223
}
196
224
225
+ public every ( period : CronPeriod . Second , interval : SecondOrMinute ) : this;
226
+ public every ( period : CronPeriod . Minute , interval : SecondOrMinute ) : this;
227
+ public every ( period : CronPeriod . Hour , interval : Hour ) : this;
228
+ public every ( period : CronPeriod . DayOfWeek , interval : DayOfWeekValue ) : this;
229
+ public every ( period : CronPeriod . DayOfMonth , interval : DayOfMonth ) : this;
230
+ public every ( period : CronPeriod . Month , interval : MonthValue ) : this;
231
+ public every ( period : any , interval : any ) : this {
232
+
233
+ const intervalValue = `*/${ interval } ` ;
234
+
235
+ switch ( period ) {
236
+ case CronPeriod . Second :
237
+ this . seconds = intervalValue ;
238
+ break ;
239
+ case CronPeriod . Minute :
240
+ this . minutes = intervalValue ;
241
+ break ;
242
+ case CronPeriod . Hour :
243
+ this . hours = intervalValue ;
244
+ break ;
245
+ case CronPeriod . DayOfWeek :
246
+ this . dayOfWeek = intervalValue ;
247
+ break ;
248
+ case CronPeriod . DayOfMonth :
249
+ this . dayOfMonth = intervalValue ;
250
+ break ;
251
+ case CronPeriod . Month :
252
+ this . month = intervalValue ;
253
+ break ;
254
+ }
255
+ return this ;
256
+ }
257
+
197
258
public toString ( ) : CronExpressionString {
198
259
return `${ this . seconds } ${ this . minutes } ${ this . hours } ${ this . dayOfMonth } ${ this . month } ${ this . dayOfWeek } ` ;
199
260
}
200
261
201
- private prepareSecondOrMinuteValueRange ( from : SecondOrMinuteValue , to : SecondOrMinuteValue ) {
262
+ private prepareSecondOrMinuteValueRange ( from : SecondOrMinute , to : SecondOrMinute ) {
202
263
if ( from >= to ) throw new Error ( "Invalid range: 'from' must be less than 'to'" ) ;
203
264
204
265
if ( [ from , to ] . some ( ( v ) => v < 0 || v > 59 ) )
@@ -207,7 +268,7 @@ export class CronExpression {
207
268
return `${ from } -${ to } ` ;
208
269
}
209
270
210
- private prepareHourValueRange ( from : HourValue , to : HourValue ) {
271
+ private prepareHourValueRange ( from : Hour , to : Hour ) {
211
272
if ( from >= to ) throw new Error ( "Invalid range: 'from' must be less than 'to'" ) ;
212
273
213
274
if ( [ from , to ] . some ( ( v ) => v < 0 || v > 23 ) )
@@ -225,49 +286,69 @@ export class CronExpression {
225
286
return `${ from } -${ to } ` ;
226
287
}
227
288
228
- private prepareMonthValueRange ( from : Month , to : Month ) {
229
- if ( Object . values ( Month ) . indexOf ( from ) >= Object . values ( Month ) . indexOf ( to ) )
289
+ private prepareMonthValueRange ( from : MonthValue , to : MonthValue ) {
290
+ if ( typeof ( from ) !== "number" && typeof ( to ) !== "number" && Object . values ( Month ) . indexOf ( from ) >= Object . values ( Month ) . indexOf ( to ) )
230
291
throw new Error ( "Invalid range: 'from' must be less than 'to'" ) ;
231
292
232
293
return `${ from } -${ to } ` ;
233
294
}
234
295
235
- private prepareDayOfWeekValueRange ( from : DayOfWeek , to : DayOfWeek ) {
236
- if ( Object . values ( DayOfWeek ) . indexOf ( from ) >= Object . values ( DayOfWeek ) . indexOf ( to ) )
296
+ private prepareDayOfWeekValueRange ( from : DayOfWeekValue , to : DayOfWeekValue ) {
297
+ if ( typeof ( from ) !== "number" && typeof ( to ) !== "number" && Object . values ( DayOfWeek ) . indexOf ( from ) >= Object . values ( DayOfWeek ) . indexOf ( to ) )
237
298
throw new Error ( "Invalid range: 'from' must be less than 'to'" ) ;
238
299
239
300
return `${ from } -${ to } ` ;
240
301
}
241
302
242
- private prepareSecondOrMinuteValueList ( timeValues : SecondOrMinuteValue [ ] ) {
303
+ private prepareSecondOrMinuteValueList ( timeValues : SecondOrMinute [ ] ) {
304
+
305
+ if ( ! timeValues . length )
306
+ throw new Error ( "Empty time value provided." ) ;
307
+
243
308
if ( timeValues . some ( ( v ) => v < 0 || v > 59 ) )
244
309
throw new RangeError ( "Time values must be within 0 and 59, inclusive." ) ;
245
310
246
311
return [ ...timeValues ] . sort ( ( a , b ) => a - b ) . join ( "," ) ;
247
312
}
248
313
249
- private prepareHourValueList ( timeValues : HourValue [ ] ) {
314
+ private prepareHourValueList ( timeValues : Hour [ ] ) {
315
+
316
+ if ( ! timeValues . length )
317
+ throw new Error ( "Empty hour value provided." ) ;
318
+
250
319
if ( timeValues . some ( ( v ) => v < 0 || v > 23 ) )
251
320
throw new RangeError ( "Hour values must be within 0 and 23, inclusive." ) ;
252
321
253
322
return [ ...timeValues ] . sort ( ( a , b ) => a - b ) . join ( "," ) ;
254
323
}
255
324
256
325
private prepareDayOfMonthValueList ( dayOfMonthValues : DayOfMonth [ ] ) {
326
+
327
+ if ( ! dayOfMonthValues . length || dayOfMonthValues . some ( ( v ) => ! v ) )
328
+ throw new Error ( "Empty day of month value provided." ) ;
329
+
257
330
if ( dayOfMonthValues . some ( ( v ) => v < 1 || v > 31 ) )
258
331
throw new RangeError ( "Day of month values must be within 1 and 31, inclusive." ) ;
259
332
260
333
return [ ...dayOfMonthValues ] . sort ( ( a , b ) => a - b ) . join ( "," ) ;
261
334
}
262
335
263
336
private prepareMonthValueList ( monthValues : Month [ ] ) {
337
+
338
+ if ( ! monthValues . length || monthValues . some ( ( v ) => ! v ) )
339
+ throw new Error ( "Empty month value provided." ) ;
340
+
264
341
return [ ...monthValues ]
265
342
. sort ( ( left , right ) => Object . values ( Month ) . indexOf ( left ) - Object . values ( Month ) . indexOf ( right ) )
266
343
. join ( "," ) ;
267
344
}
268
345
269
- private prepareDayOfWeekValueList ( dayValues : DayOfWeek [ ] ) {
270
- return [ ...dayValues ]
346
+ private prepareDayOfWeekValueList ( dayOfWeekValues : DayOfWeek [ ] ) {
347
+
348
+ if ( ! dayOfWeekValues . length || dayOfWeekValues . some ( ( v ) => ! v ) )
349
+ throw new Error ( "Empty month value provided." ) ;
350
+
351
+ return [ ...dayOfWeekValues ]
271
352
. sort ( ( left , right ) => Object . values ( DayOfWeek ) . indexOf ( left ) - Object . values ( DayOfWeek ) . indexOf ( right ) )
272
353
. join ( "," ) ;
273
354
}
0 commit comments