| 59 | } |
| 60 | |
| 61 | function parseField(field: string, [min, max]: [number, number], idx: number): Set<number> { |
| 62 | const out = new Set<number>(); |
| 63 | for (const segment of field.split(',')) { |
| 64 | let step = 1; |
| 65 | let body = segment; |
| 66 | if (segment.includes('/')) { |
| 67 | const [b, s] = segment.split('/'); |
| 68 | body = b!; |
| 69 | step = parseInt(s!, 10); |
| 70 | if (!Number.isFinite(step) || step <= 0) throw new Error(`Invalid step in cron field ${idx}: "${segment}"`); |
| 71 | } |
| 72 | let lo: number, hi: number; |
| 73 | if (body === '*') { |
| 74 | lo = min; hi = max; |
| 75 | } else if (body.includes('-')) { |
| 76 | const [a, b] = body.split('-'); |
| 77 | lo = parseInt(a!, 10); hi = parseInt(b!, 10); |
| 78 | } else { |
| 79 | lo = hi = parseInt(body, 10); |
| 80 | } |
| 81 | if (!Number.isFinite(lo) || !Number.isFinite(hi)) { |
| 82 | throw new Error(`Invalid cron field ${idx}: "${segment}"`); |
| 83 | } |
| 84 | if (lo < min || hi > max || lo > hi) { |
| 85 | throw new Error(`Cron field ${idx} out of range [${min},${max}]: "${segment}"`); |
| 86 | } |
| 87 | for (let v = lo; v <= hi; v += step) out.add(v); |
| 88 | } |
| 89 | return out; |
| 90 | } |
| 91 | |
| 92 | /** Does the given Date match the cron expression at minute precision? */ |
| 93 | export function matches(cron: CronExpression, when: Date): boolean { |