(expr: string)
| 45 | * lists (`a,b,c`), and step (e.g. star-slash-n or `a-b/n`). |
| 46 | */ |
| 47 | export function parseCronExpression(expr: string): ParsedCronExpression { |
| 48 | if (typeof expr !== 'string') { |
| 49 | throw new TypeError('cron expression must be a string'); |
| 50 | } |
| 51 | const trimmed = expr.trim(); |
| 52 | if (trimmed === '') { |
| 53 | throw new Error('cron expression is empty'); |
| 54 | } |
| 55 | const fields = trimmed.split(/\s+/); |
| 56 | if (fields.length !== 5) { |
| 57 | throw new Error( |
| 58 | `cron expression must have exactly 5 fields (minute hour day-of-month month day-of-week); got ${fields.length}`, |
| 59 | ); |
| 60 | } |
| 61 | const [minField, hourField, domField, monthField, dowField] = fields as [ |
| 62 | string, |
| 63 | string, |
| 64 | string, |
| 65 | string, |
| 66 | string, |
| 67 | ]; |
| 68 | |
| 69 | const minutes = parseField(minField, MINUTE_RANGE.min, MINUTE_RANGE.max, 'minute'); |
| 70 | const hours = parseField(hourField, HOUR_RANGE.min, HOUR_RANGE.max, 'hour'); |
| 71 | const daysOfMonth = parseField(domField, DOM_RANGE.min, DOM_RANGE.max, 'day-of-month'); |
| 72 | const months = parseField(monthField, MONTH_RANGE.min, MONTH_RANGE.max, 'month'); |
| 73 | const dowRaw = parseField(dowField, DOW_RANGE.min, DOW_RANGE.max, 'day-of-week'); |
| 74 | const daysOfWeek = new Set<number>(); |
| 75 | for (const v of dowRaw) daysOfWeek.add(v === 7 ? 0 : v); |
| 76 | |
| 77 | return { |
| 78 | raw: trimmed, |
| 79 | minutes, |
| 80 | hours, |
| 81 | daysOfMonth, |
| 82 | months, |
| 83 | daysOfWeek, |
| 84 | daysOfMonthWildcard: isWildcard(domField), |
| 85 | daysOfWeekWildcard: isWildcard(dowField), |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | function isWildcard(field: string): boolean { |
| 90 | // `*` and `*/n` both leave the field unconstrained in the |
no test coverage detected