1
- import assert from 'assert' ;
2
-
3
1
import { CronFieldCollection } from './CronFieldCollection' ;
4
2
import { CronDate } from './CronDate' ;
5
3
import { CronExpression , CronExpressionOptions } from './CronExpression' ;
@@ -15,7 +13,7 @@ import {
15
13
DayOfWeekRange ,
16
14
HourRange ,
17
15
MonthRange ,
18
- ParseRageResponse ,
16
+ ParseRangeResponse ,
19
17
SixtyRange ,
20
18
} from './fields' ;
21
19
@@ -99,10 +97,9 @@ export class CronExpressionParser {
99
97
100
98
expression = PredefinedExpressions [ expression as keyof typeof PredefinedExpressions ] || expression ;
101
99
const rawFields = CronExpressionParser . #getRawFields( expression , strict ) ;
102
- assert (
103
- rawFields . dayOfMonth === '*' || rawFields . dayOfWeek === '*' || ! strict ,
104
- 'Cannot use both dayOfMonth and dayOfWeek together in strict mode!' ,
105
- ) ;
100
+ if ( ! ( rawFields . dayOfMonth === '*' || rawFields . dayOfWeek === '*' || ! strict ) ) {
101
+ throw new Error ( 'Cannot use both dayOfMonth and dayOfWeek together in strict mode!' ) ;
102
+ }
106
103
107
104
const second = CronExpressionParser . #parseField(
108
105
CronUnit . Second ,
@@ -151,11 +148,17 @@ export class CronExpressionParser {
151
148
* @returns {RawCronFields } The raw fields.
152
149
*/
153
150
static #getRawFields( expression : string , strict : boolean ) : RawCronFields {
154
- assert ( ! strict || expression . length , 'Invalid cron expression' ) ;
151
+ if ( ! ( ! strict || expression . length ) ) {
152
+ throw new Error ( 'Invalid cron expression' ) ;
153
+ }
155
154
expression = expression || '0 * * * * *' ;
156
155
const atoms = expression . trim ( ) . split ( / \s + / ) ;
157
- assert ( ! strict || atoms . length === 6 , 'Invalid cron expression, expected 6 fields' ) ;
158
- assert ( atoms . length <= 6 , 'Invalid cron expression, too many fields' ) ;
156
+ if ( ! ( ! strict || atoms . length === 6 ) ) {
157
+ throw new Error ( 'Invalid cron expression, expected 6 fields' ) ;
158
+ }
159
+ if ( atoms . length > 6 ) {
160
+ throw new Error ( 'Invalid cron expression, too many fields' ) ;
161
+ }
159
162
const defaults = [ '*' , '*' , '*' , '*' , '*' , '0' ] ;
160
163
if ( atoms . length < defaults . length ) {
161
164
atoms . unshift ( ...defaults . slice ( atoms . length ) ) ;
@@ -178,13 +181,17 @@ export class CronExpressionParser {
178
181
value = value . replace ( / [ a - z ] { 3 } / gi, ( match ) => {
179
182
match = match . toLowerCase ( ) ;
180
183
const replacer = Months [ match as keyof typeof Months ] || DayOfWeek [ match as keyof typeof DayOfWeek ] ;
181
- assert ( replacer != null , `Validation error, cannot resolve alias "${ match } "` ) ;
184
+ if ( ! replacer ) {
185
+ throw new Error ( `Validation error, cannot resolve alias "${ match } "` ) ;
186
+ }
182
187
return replacer . toString ( ) ;
183
188
} ) ;
184
189
}
185
190
186
191
// Check for valid characters
187
- assert ( constraints . validChars . test ( value ) , `Invalid characters, got value: ${ value } ` ) ;
192
+ if ( ! constraints . validChars . test ( value ) ) {
193
+ throw new Error ( `Invalid characters, got value: ${ value } ` ) ;
194
+ }
188
195
189
196
// Replace '*' and '?'
190
197
value = value . replace ( / [ * ? ] / g, constraints . min + '-' + constraints . max ) ;
@@ -200,35 +207,30 @@ export class CronExpressionParser {
200
207
*/
201
208
static #parseSequence( field : CronUnit , val : string , constraints : CronConstraints ) : ( number | string ) [ ] {
202
209
const stack : ( number | string ) [ ] = [ ] ;
203
-
204
210
function handleResult ( result : number | string | ( number | string ) [ ] , constraints : CronConstraints ) {
205
211
if ( Array . isArray ( result ) ) {
206
- result . forEach ( ( value ) => {
207
- if ( ! CronExpressionParser . #isValidConstraintChar( constraints , value ) ) {
208
- const v = parseInt ( value . toString ( ) , 10 ) ;
209
- const isValid = v >= constraints . min && v <= constraints . max ;
210
- assert (
211
- isValid ,
212
- `Constraint error, got value ${ value } expected range ${ constraints . min } -${ constraints . max } ` ,
213
- ) ;
214
- stack . push ( value ) ;
215
- }
216
- } ) ;
212
+ stack . push ( ...result ) ;
217
213
} else {
218
214
if ( CronExpressionParser . #isValidConstraintChar( constraints , result ) ) {
219
215
stack . push ( result ) ;
220
216
} else {
221
217
const v = parseInt ( result . toString ( ) , 10 ) ;
222
218
const isValid = v >= constraints . min && v <= constraints . max ;
223
- assert ( isValid , `Constraint error, got value ${ result } expected range ${ constraints . min } -${ constraints . max } ` ) ;
219
+ if ( ! isValid ) {
220
+ throw new Error (
221
+ `Constraint error, got value ${ result } expected range ${ constraints . min } -${ constraints . max } ` ,
222
+ ) ;
223
+ }
224
224
stack . push ( field === CronUnit . DayOfWeek ? v % 7 : result ) ;
225
225
}
226
226
}
227
227
}
228
228
229
229
const atoms = val . split ( ',' ) ;
230
230
atoms . forEach ( ( atom ) => {
231
- assert ( atom . length > 0 , 'Invalid list value format' ) ;
231
+ if ( ! ( atom . length > 0 ) ) {
232
+ throw new Error ( 'Invalid list value format' ) ;
233
+ }
232
234
handleResult ( CronExpressionParser . #parseRepeat( field , atom , constraints ) , constraints ) ;
233
235
} ) ;
234
236
return stack ;
@@ -242,9 +244,11 @@ export class CronExpressionParser {
242
244
* @private
243
245
* @returns {(number | string)[] } The parsed repeat.
244
246
*/
245
- static #parseRepeat( field : CronUnit , val : string , constraints : CronConstraints ) : ParseRageResponse {
247
+ static #parseRepeat( field : CronUnit , val : string , constraints : CronConstraints ) : ParseRangeResponse {
246
248
const atoms = val . split ( '/' ) ;
247
- assert ( atoms . length <= 2 , `Invalid repeat: ${ val } ` ) ;
249
+ if ( atoms . length > 2 ) {
250
+ throw new Error ( `Invalid repeat: ${ val } ` ) ;
251
+ }
248
252
if ( atoms . length === 2 ) {
249
253
if ( ! isNaN ( parseInt ( atoms [ 0 ] , 10 ) ) ) {
250
254
atoms [ 0 ] = `${ atoms [ 0 ] } -${ constraints . max } ` ;
@@ -266,8 +270,12 @@ export class CronExpressionParser {
266
270
*/
267
271
static #validateRange( min : number , max : number , constraints : CronConstraints ) : void {
268
272
const isValid = ! isNaN ( min ) && ! isNaN ( max ) && min >= constraints . min && max <= constraints . max ;
269
- assert ( isValid , `Constraint error, got range ${ min } -${ max } expected range ${ constraints . min } -${ constraints . max } ` ) ;
270
- assert ( min <= max , `Invalid range: ${ min } -${ max } , min(${ min } ) > max(${ max } )` ) ;
273
+ if ( ! isValid ) {
274
+ throw new Error ( `Constraint error, got range ${ min } -${ max } expected range ${ constraints . min } -${ constraints . max } ` ) ;
275
+ }
276
+ if ( min > max ) {
277
+ throw new Error ( `Invalid range: ${ min } -${ max } , min(${ min } ) > max(${ max } )` ) ;
278
+ }
271
279
}
272
280
273
281
/**
@@ -278,10 +286,9 @@ export class CronExpressionParser {
278
286
* @throws {Error } Throws an error if the repeat interval is invalid.
279
287
*/
280
288
static #validateRepeatInterval( repeatInterval : number ) : void {
281
- assert (
282
- ! isNaN ( repeatInterval ) && repeatInterval > 0 ,
283
- `Constraint error, cannot repeat at every ${ repeatInterval } time.` ,
284
- ) ;
289
+ if ( ! ( ! isNaN ( repeatInterval ) && repeatInterval > 0 ) ) {
290
+ throw new Error ( `Constraint error, cannot repeat at every ${ repeatInterval } time.` ) ;
291
+ }
285
292
}
286
293
287
294
/**
@@ -320,7 +327,7 @@ export class CronExpressionParser {
320
327
val : string ,
321
328
repeatInterval : number ,
322
329
constraints : CronConstraints ,
323
- ) : ParseRageResponse {
330
+ ) : ParseRangeResponse {
324
331
const atoms : string [ ] = val . split ( '-' ) ;
325
332
if ( atoms . length <= 1 ) {
326
333
return isNaN ( + val ) ? val : + val ;
@@ -346,14 +353,14 @@ export class CronExpressionParser {
346
353
}
347
354
const nthValue = + atoms [ atoms . length - 1 ] ;
348
355
const matches = val . match ( / ( [ , - / ] ) / ) ;
349
- assert (
350
- matches === null ,
351
- `Constraint error, invalid dayOfWeek \`#\` and \`${ matches ?. [ 0 ] } \` special characters are incompatible` ,
352
- ) ;
353
- assert (
354
- atoms . length <= 2 && ! isNaN ( nthValue ) && nthValue >= 1 && nthValue <= 5 ,
355
- 'Constraint error, invalid dayOfWeek occurrence number (#)' ,
356
- ) ;
356
+ if ( matches !== null ) {
357
+ throw new Error (
358
+ `Constraint error, invalid dayOfWeek \`#\` and \`${ matches ?. [ 0 ] } \` special characters are incompatible` ,
359
+ ) ;
360
+ }
361
+ if ( ! ( atoms . length <= 2 && ! isNaN ( nthValue ) && nthValue >= 1 && nthValue <= 5 ) ) {
362
+ throw new Error ( 'Constraint error, invalid dayOfWeek occurrence number (#)' ) ;
363
+ }
357
364
return { dayOfWeek : atoms [ 0 ] , nthDayOfWeek : nthValue } ;
358
365
}
359
366
0 commit comments