cron-toolkit 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +244 -2
- package/dist/constants.d.ts +15 -0
- package/dist/constants.js +28 -0
- package/dist/core/ast.d.ts +10 -0
- package/dist/core/ast.js +27 -0
- package/dist/core/dialects.d.ts +33 -0
- package/dist/core/dialects.js +50 -0
- package/dist/core/errors.d.ts +19 -0
- package/dist/core/errors.js +74 -0
- package/dist/core/fields.d.ts +15 -0
- package/dist/core/fields.js +167 -0
- package/dist/core/format.d.ts +17 -0
- package/dist/core/format.js +110 -0
- package/dist/core/parse.d.ts +10 -0
- package/dist/core/parse.js +102 -0
- package/dist/core/types.d.ts +88 -0
- package/dist/core/types.js +1 -0
- package/dist/describe/describer.d.ts +4 -0
- package/dist/describe/describer.js +224 -0
- package/dist/describe/intl.d.ts +11 -0
- package/dist/describe/intl.js +32 -0
- package/dist/describe/locale.d.ts +64 -0
- package/dist/describe/locale.js +1 -0
- package/dist/describe.d.ts +19 -0
- package/dist/describe.js +14 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/locales/en.d.ts +2 -0
- package/dist/locales/en.js +40 -0
- package/dist/locales/es.d.ts +2 -0
- package/dist/locales/es.js +101 -0
- package/dist/locales/fr.d.ts +2 -0
- package/dist/locales/fr.js +101 -0
- package/dist/recurrence/recurrence.d.ts +15 -0
- package/dist/recurrence/recurrence.js +222 -0
- package/dist/recurrence/types.d.ts +56 -0
- package/dist/recurrence/types.js +5 -0
- package/dist/schedule/civil.d.ts +26 -0
- package/dist/schedule/civil.js +53 -0
- package/dist/schedule/compile.d.ts +18 -0
- package/dist/schedule/compile.js +122 -0
- package/dist/schedule/engine.d.ts +32 -0
- package/dist/schedule/engine.js +97 -0
- package/dist/schedule/search.d.ts +9 -0
- package/dist/schedule/search.js +137 -0
- package/dist/schedule/temporal.d.ts +7 -0
- package/dist/schedule/temporal.js +14 -0
- package/dist/schedule/zone.d.ts +29 -0
- package/dist/schedule/zone.js +66 -0
- package/dist/schedule.d.ts +36 -0
- package/dist/schedule.js +84 -0
- package/dist/utils/operator-grammar.d.ts +49 -0
- package/dist/utils/operator-grammar.js +79 -0
- package/dist/utils/parse-cron-int.d.ts +1 -0
- package/dist/utils/parse-cron-int.js +3 -0
- package/package.json +73 -4
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { fromWallSeconds, toWallSeconds } from "./civil.js";
|
|
2
|
+
/**
|
|
3
|
+
* First matching wall second ≥ `start`, or null if none before the end of
|
|
4
|
+
* `maxYear`. Field-by-field: a mismatch bumps that field to its next allowed
|
|
5
|
+
* value (resetting smaller fields) or carries into the next larger field.
|
|
6
|
+
*/
|
|
7
|
+
export function nextWallMatch(compiled, start, maxYear) {
|
|
8
|
+
let { year, month, day, hour, minute, second } = fromWallSeconds(start);
|
|
9
|
+
const [minYear, boundMax] = compiled.yearBounds;
|
|
10
|
+
const lastYear = Math.min(maxYear, boundMax);
|
|
11
|
+
if (year < minYear)
|
|
12
|
+
[year, month, day, hour, minute, second] = [minYear, 1, 1, 0, 0, 0];
|
|
13
|
+
while (year <= lastYear) {
|
|
14
|
+
if (compiled.years && !compiled.years.includes(year)) {
|
|
15
|
+
const nextYear = firstAtLeast(compiled.years, year);
|
|
16
|
+
if (nextYear === undefined)
|
|
17
|
+
return null;
|
|
18
|
+
[year, month, day, hour, minute, second] = [nextYear, 1, 1, 0, 0, 0];
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const nextMonth = firstAtLeast(compiled.months, month);
|
|
22
|
+
if (nextMonth === undefined) {
|
|
23
|
+
[year, month, day, hour, minute, second] = [year + 1, 1, 1, 0, 0, 0];
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (nextMonth !== month)
|
|
27
|
+
[month, day, hour, minute, second] = [nextMonth, 1, 0, 0, 0];
|
|
28
|
+
const nextDay = firstAtLeast(compiled.daysOf(year, month), day);
|
|
29
|
+
if (nextDay === undefined) {
|
|
30
|
+
[month, day, hour, minute, second] = [month + 1, 1, 0, 0, 0];
|
|
31
|
+
if (month > 12)
|
|
32
|
+
[year, month] = [year + 1, 1];
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (nextDay !== day)
|
|
36
|
+
[day, hour, minute, second] = [nextDay, 0, 0, 0];
|
|
37
|
+
const nextHour = firstAtLeast(compiled.hours, hour);
|
|
38
|
+
if (nextHour === undefined) {
|
|
39
|
+
[day, hour, minute, second] = [day + 1, 0, 0, 0];
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (nextHour !== hour)
|
|
43
|
+
[hour, minute, second] = [nextHour, 0, 0];
|
|
44
|
+
const nextMinute = firstAtLeast(compiled.minutes, minute);
|
|
45
|
+
if (nextMinute === undefined) {
|
|
46
|
+
[hour, minute, second] = [hour + 1, 0, 0];
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (nextMinute !== minute)
|
|
50
|
+
[minute, second] = [nextMinute, 0];
|
|
51
|
+
const nextSecond = firstAtLeast(compiled.seconds, second);
|
|
52
|
+
if (nextSecond === undefined) {
|
|
53
|
+
[minute, second] = [minute + 1, 0];
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
return toWallSeconds({ year, month, day, hour, minute, second: nextSecond });
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
/** Last matching wall second ≤ `start`, or null if none since the start of `minYear`. */
|
|
61
|
+
export function previousWallMatch(compiled, start, minYear) {
|
|
62
|
+
let { year, month, day, hour, minute, second } = fromWallSeconds(start);
|
|
63
|
+
const [boundMin, maxYear] = compiled.yearBounds;
|
|
64
|
+
const firstYear = Math.max(minYear, boundMin);
|
|
65
|
+
if (year > maxYear)
|
|
66
|
+
[year, month, day, hour, minute, second] = [maxYear, 12, 31, 23, 59, 59];
|
|
67
|
+
while (year >= firstYear) {
|
|
68
|
+
if (compiled.years && !compiled.years.includes(year)) {
|
|
69
|
+
const previousYear = lastAtMost(compiled.years, year);
|
|
70
|
+
if (previousYear === undefined)
|
|
71
|
+
return null;
|
|
72
|
+
[year, month, day, hour, minute, second] = [previousYear, 12, 31, 23, 59, 59];
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const previousMonth = lastAtMost(compiled.months, month);
|
|
76
|
+
if (previousMonth === undefined) {
|
|
77
|
+
[year, month, day, hour, minute, second] = [year - 1, 12, 31, 23, 59, 59];
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (previousMonth !== month)
|
|
81
|
+
[month, day, hour, minute, second] = [previousMonth, 31, 23, 59, 59];
|
|
82
|
+
const previousDay = lastAtMost(compiled.daysOf(year, month), day);
|
|
83
|
+
if (previousDay === undefined) {
|
|
84
|
+
[month, day, hour, minute, second] = [month - 1, 31, 23, 59, 59];
|
|
85
|
+
if (month < 1)
|
|
86
|
+
[year, month] = [year - 1, 12];
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (previousDay !== day)
|
|
90
|
+
[day, hour, minute, second] = [previousDay, 23, 59, 59];
|
|
91
|
+
const previousHour = lastAtMost(compiled.hours, hour);
|
|
92
|
+
if (previousHour === undefined) {
|
|
93
|
+
[day, hour, minute, second] = [day - 1, 23, 59, 59];
|
|
94
|
+
if (day < 1)
|
|
95
|
+
[month, day] = [month - 1, 31];
|
|
96
|
+
if (month < 1)
|
|
97
|
+
[year, month] = [year - 1, 12];
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (previousHour !== hour)
|
|
101
|
+
[hour, minute, second] = [previousHour, 59, 59];
|
|
102
|
+
const previousMinute = lastAtMost(compiled.minutes, minute);
|
|
103
|
+
if (previousMinute === undefined) {
|
|
104
|
+
[hour, minute, second] = [hour - 1, 59, 59];
|
|
105
|
+
if (hour < 0)
|
|
106
|
+
[day, hour] = [day - 1, 23];
|
|
107
|
+
if (day < 1)
|
|
108
|
+
[month, day] = [month - 1, 31];
|
|
109
|
+
if (month < 1)
|
|
110
|
+
[year, month] = [year - 1, 12];
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (previousMinute !== minute)
|
|
114
|
+
[minute, second] = [previousMinute, 59];
|
|
115
|
+
const previousSecond = lastAtMost(compiled.seconds, second);
|
|
116
|
+
if (previousSecond === undefined) {
|
|
117
|
+
[minute, second] = [minute - 1, 59];
|
|
118
|
+
if (minute < 0)
|
|
119
|
+
[hour, minute] = [hour - 1, 59];
|
|
120
|
+
if (hour < 0)
|
|
121
|
+
[day, hour] = [day - 1, 23];
|
|
122
|
+
if (day < 1)
|
|
123
|
+
[month, day] = [month - 1, 31];
|
|
124
|
+
if (month < 1)
|
|
125
|
+
[year, month] = [year - 1, 12];
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
return toWallSeconds({ year, month, day, hour, minute, second: previousSecond });
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
function firstAtLeast(sorted, value) {
|
|
133
|
+
return sorted.find((candidate) => candidate >= value);
|
|
134
|
+
}
|
|
135
|
+
function lastAtMost(sorted, value) {
|
|
136
|
+
return sorted.findLast((candidate) => candidate <= value);
|
|
137
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Thrown by `cron-toolkit/schedule` when the runtime has no `Temporal` global. */
|
|
2
|
+
export declare class TemporalUnavailableError extends Error {
|
|
3
|
+
name: string;
|
|
4
|
+
constructor();
|
|
5
|
+
}
|
|
6
|
+
export type TemporalNamespace = typeof Temporal;
|
|
7
|
+
export declare function getTemporal(): TemporalNamespace;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Thrown by `cron-toolkit/schedule` when the runtime has no `Temporal` global. */
|
|
2
|
+
export class TemporalUnavailableError extends Error {
|
|
3
|
+
name = "TemporalUnavailableError";
|
|
4
|
+
constructor() {
|
|
5
|
+
super("Temporal is not available in this runtime (e.g. Safari, or Node < 26). " +
|
|
6
|
+
'Load a polyfill before using cron-toolkit/schedule, e.g. `import "temporal-polyfill/global"`.');
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function getTemporal() {
|
|
10
|
+
const temporal = globalThis.Temporal;
|
|
11
|
+
if (!temporal)
|
|
12
|
+
throw new TemporalUnavailableError();
|
|
13
|
+
return temporal;
|
|
14
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { TemporalNamespace } from "./temporal.js";
|
|
2
|
+
/**
|
|
3
|
+
* A stretch of time with one UTC offset, between two offset transitions.
|
|
4
|
+
* All values are epoch seconds (instants) or offset seconds.
|
|
5
|
+
*/
|
|
6
|
+
export interface Segment {
|
|
7
|
+
/** Transition that starts the segment; null = unbounded past. */
|
|
8
|
+
start: number | null;
|
|
9
|
+
/** Next transition; null = unbounded future. */
|
|
10
|
+
end: number | null;
|
|
11
|
+
offset: number;
|
|
12
|
+
/** Offset just before `start`. */
|
|
13
|
+
previousOffset: number | null;
|
|
14
|
+
}
|
|
15
|
+
/** Thin Temporal wrapper: UTC offsets and transitions of one IANA zone. */
|
|
16
|
+
export declare class Zone {
|
|
17
|
+
#private;
|
|
18
|
+
readonly timeZone: string;
|
|
19
|
+
constructor(temporal: TemporalNamespace, timeZone: string);
|
|
20
|
+
toZonedDateTime(seconds: number): Temporal.ZonedDateTime;
|
|
21
|
+
segmentAt(seconds: number): Segment;
|
|
22
|
+
next(segment: Segment): Segment;
|
|
23
|
+
previous(segment: Segment): Segment;
|
|
24
|
+
}
|
|
25
|
+
export declare function contains(segment: Segment, seconds: number): boolean;
|
|
26
|
+
/** floor(epochNanoseconds / 1e9) as a number. */
|
|
27
|
+
export declare function toSeconds(epochNanoseconds: bigint): number;
|
|
28
|
+
/** ceil(epochNanoseconds / 1e9) as a number. */
|
|
29
|
+
export declare function toSecondsCeil(epochNanoseconds: bigint): number;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const NS_PER_SECOND = 1000000000n;
|
|
2
|
+
/** Thin Temporal wrapper: UTC offsets and transitions of one IANA zone. */
|
|
3
|
+
export class Zone {
|
|
4
|
+
#temporal;
|
|
5
|
+
timeZone;
|
|
6
|
+
constructor(temporal, timeZone) {
|
|
7
|
+
this.#temporal = temporal;
|
|
8
|
+
// Validates the zone up front (throws RangeError for unknown ids).
|
|
9
|
+
this.timeZone = temporal.Now.zonedDateTimeISO(timeZone).timeZoneId;
|
|
10
|
+
}
|
|
11
|
+
toZonedDateTime(seconds) {
|
|
12
|
+
return this.#zoned(seconds);
|
|
13
|
+
}
|
|
14
|
+
segmentAt(seconds) {
|
|
15
|
+
const start = this.#transitionAtOrBefore(seconds);
|
|
16
|
+
return {
|
|
17
|
+
start,
|
|
18
|
+
end: this.#transitionAfter(seconds),
|
|
19
|
+
offset: this.#offsetAt(seconds),
|
|
20
|
+
previousOffset: start === null ? null : this.#offsetAt(start - 1),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
next(segment) {
|
|
24
|
+
const start = segment.end;
|
|
25
|
+
return { start, end: this.#transitionAfter(start), offset: this.#offsetAt(start), previousOffset: segment.offset };
|
|
26
|
+
}
|
|
27
|
+
previous(segment) {
|
|
28
|
+
const end = segment.start;
|
|
29
|
+
const start = this.#transitionAtOrBefore(end - 1);
|
|
30
|
+
return {
|
|
31
|
+
start,
|
|
32
|
+
end,
|
|
33
|
+
offset: segment.previousOffset,
|
|
34
|
+
previousOffset: start === null ? null : this.#offsetAt(start - 1),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
#zoned(seconds, extraNanoseconds = 0n) {
|
|
38
|
+
const epochNanoseconds = BigInt(seconds) * NS_PER_SECOND + extraNanoseconds;
|
|
39
|
+
return this.#temporal.Instant.fromEpochNanoseconds(epochNanoseconds).toZonedDateTimeISO(this.timeZone);
|
|
40
|
+
}
|
|
41
|
+
#offsetAt(seconds) {
|
|
42
|
+
return Math.round(this.#zoned(seconds).offsetNanoseconds / 1e9);
|
|
43
|
+
}
|
|
44
|
+
#transitionAfter(seconds) {
|
|
45
|
+
const transition = this.#zoned(seconds).getTimeZoneTransition("next");
|
|
46
|
+
return transition ? toSeconds(transition.epochNanoseconds) : null;
|
|
47
|
+
}
|
|
48
|
+
#transitionAtOrBefore(seconds) {
|
|
49
|
+
// "previous" is strictly before the instant, so ask from 1ns later.
|
|
50
|
+
const transition = this.#zoned(seconds, 1n).getTimeZoneTransition("previous");
|
|
51
|
+
return transition ? toSeconds(transition.epochNanoseconds) : null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function contains(segment, seconds) {
|
|
55
|
+
return (segment.start === null || segment.start <= seconds) && (segment.end === null || seconds < segment.end);
|
|
56
|
+
}
|
|
57
|
+
/** floor(epochNanoseconds / 1e9) as a number. */
|
|
58
|
+
export function toSeconds(epochNanoseconds) {
|
|
59
|
+
const quotient = epochNanoseconds / NS_PER_SECOND;
|
|
60
|
+
return Number(epochNanoseconds < 0n && quotient * NS_PER_SECOND !== epochNanoseconds ? quotient - 1n : quotient);
|
|
61
|
+
}
|
|
62
|
+
/** ceil(epochNanoseconds / 1e9) as a number. */
|
|
63
|
+
export function toSecondsCeil(epochNanoseconds) {
|
|
64
|
+
const floor = toSeconds(epochNanoseconds);
|
|
65
|
+
return BigInt(floor) * NS_PER_SECOND === epochNanoseconds ? floor : floor + 1;
|
|
66
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/// <reference lib="esnext.temporal" preserve="true" />
|
|
2
|
+
import type { Cron, ParseOptions } from "./core/types.js";
|
|
3
|
+
import { type DstPolicy } from "./schedule/engine.js";
|
|
4
|
+
export { TemporalUnavailableError } from "./schedule/temporal.js";
|
|
5
|
+
export type { DstPolicy } from "./schedule/engine.js";
|
|
6
|
+
export interface ScheduleOptions extends ParseOptions {
|
|
7
|
+
/** Search start (exclusive). Defaults to now. */
|
|
8
|
+
from?: Temporal.Instant | Temporal.ZonedDateTime;
|
|
9
|
+
/** IANA zone. Defaults to `from`'s zone when it is a ZonedDateTime, else the system zone. */
|
|
10
|
+
timeZone?: string;
|
|
11
|
+
/** Overrides the dialect's DST behavior (see `DstPolicy`). */
|
|
12
|
+
dst?: Partial<DstPolicy>;
|
|
13
|
+
}
|
|
14
|
+
export interface RunsOptions extends ScheduleOptions {
|
|
15
|
+
direction?: "forward" | "backward";
|
|
16
|
+
}
|
|
17
|
+
export interface NextRunsOptions extends ScheduleOptions {
|
|
18
|
+
count: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Lazily yields run times, strictly after `from` (or strictly before it when
|
|
22
|
+
* `direction` is `backward`). Ends when no further run exists (e.g. `30 FEB`,
|
|
23
|
+
* or a Quartz year list is exhausted).
|
|
24
|
+
*/
|
|
25
|
+
export declare function runs(cron: string | Cron, options?: RunsOptions): Generator<Temporal.ZonedDateTime, void, undefined>;
|
|
26
|
+
/** Next run strictly after `from`, or null if there is none. */
|
|
27
|
+
export declare function nextRun(cron: string | Cron, options?: ScheduleOptions): Temporal.ZonedDateTime | null;
|
|
28
|
+
/** Up to `count` next runs (fewer if the schedule ends). */
|
|
29
|
+
export declare function nextRuns(cron: string | Cron, options: NextRunsOptions): Temporal.ZonedDateTime[];
|
|
30
|
+
/** Latest run strictly before `from`, or null if there is none. */
|
|
31
|
+
export declare function previousRun(cron: string | Cron, options?: ScheduleOptions): Temporal.ZonedDateTime | null;
|
|
32
|
+
/**
|
|
33
|
+
* Whether a wall-clock date-time matches the expression's fields. Ignores DST
|
|
34
|
+
* policy: `runs` is the source of truth for when a job actually fires.
|
|
35
|
+
*/
|
|
36
|
+
export declare function matches(cron: string | Cron, dateTime: Temporal.ZonedDateTime | Temporal.PlainDateTime): boolean;
|
package/dist/schedule.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/// <reference lib="esnext.temporal" preserve="true" />
|
|
2
|
+
import { resolveCron } from "./core/parse.js";
|
|
3
|
+
import { compile } from "./schedule/compile.js";
|
|
4
|
+
import { defaultDstPolicy, nextInstant, previousInstant } from "./schedule/engine.js";
|
|
5
|
+
import { getTemporal } from "./schedule/temporal.js";
|
|
6
|
+
import { toSeconds, toSecondsCeil, Zone } from "./schedule/zone.js";
|
|
7
|
+
export { TemporalUnavailableError } from "./schedule/temporal.js";
|
|
8
|
+
/**
|
|
9
|
+
* Lazily yields run times, strictly after `from` (or strictly before it when
|
|
10
|
+
* `direction` is `backward`). Ends when no further run exists (e.g. `30 FEB`,
|
|
11
|
+
* or a Quartz year list is exhausted).
|
|
12
|
+
*/
|
|
13
|
+
export function runs(cron, options = {}) {
|
|
14
|
+
const temporal = getTemporal();
|
|
15
|
+
const compiled = compile(resolveCron(cron, options));
|
|
16
|
+
const { from } = options;
|
|
17
|
+
const timeZone = options.timeZone ?? (from && "timeZoneId" in from ? from.timeZoneId : temporal.Now.timeZoneId());
|
|
18
|
+
const context = {
|
|
19
|
+
compiled,
|
|
20
|
+
zone: new Zone(temporal, timeZone),
|
|
21
|
+
policy: { ...defaultDstPolicy(compiled), ...options.dst },
|
|
22
|
+
};
|
|
23
|
+
const epochNanoseconds = (from ?? temporal.Now.instant()).epochNanoseconds;
|
|
24
|
+
return options.direction === "backward"
|
|
25
|
+
? iterateBackward(context, toSecondsCeil(epochNanoseconds) - 1)
|
|
26
|
+
: iterateForward(context, toSeconds(epochNanoseconds));
|
|
27
|
+
}
|
|
28
|
+
function* iterateForward(context, cursor) {
|
|
29
|
+
let hint;
|
|
30
|
+
for (;;) {
|
|
31
|
+
const found = nextInstant(context, cursor, hint);
|
|
32
|
+
if (!found)
|
|
33
|
+
return;
|
|
34
|
+
yield context.zone.toZonedDateTime(found.instant);
|
|
35
|
+
cursor = found.instant;
|
|
36
|
+
hint = found.segment;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function* iterateBackward(context, upper) {
|
|
40
|
+
let hint;
|
|
41
|
+
for (;;) {
|
|
42
|
+
const found = previousInstant(context, upper, hint);
|
|
43
|
+
if (!found)
|
|
44
|
+
return;
|
|
45
|
+
yield context.zone.toZonedDateTime(found.instant);
|
|
46
|
+
upper = found.instant - 1;
|
|
47
|
+
hint = found.segment;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Next run strictly after `from`, or null if there is none. */
|
|
51
|
+
export function nextRun(cron, options = {}) {
|
|
52
|
+
return runs(cron, options).next().value ?? null;
|
|
53
|
+
}
|
|
54
|
+
/** Up to `count` next runs (fewer if the schedule ends). */
|
|
55
|
+
export function nextRuns(cron, options) {
|
|
56
|
+
const iterator = runs(cron, options);
|
|
57
|
+
const result = [];
|
|
58
|
+
while (result.length < options.count) {
|
|
59
|
+
const { value, done } = iterator.next();
|
|
60
|
+
if (done)
|
|
61
|
+
break;
|
|
62
|
+
result.push(value);
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
/** Latest run strictly before `from`, or null if there is none. */
|
|
67
|
+
export function previousRun(cron, options = {}) {
|
|
68
|
+
return runs(cron, { ...options, direction: "backward" }).next().value ?? null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Whether a wall-clock date-time matches the expression's fields. Ignores DST
|
|
72
|
+
* policy: `runs` is the source of truth for when a job actually fires.
|
|
73
|
+
*/
|
|
74
|
+
export function matches(cron, dateTime) {
|
|
75
|
+
const compiled = compile(resolveCron(cron));
|
|
76
|
+
const [minYear, maxYear] = compiled.yearBounds;
|
|
77
|
+
const { year, month, day, hour, minute, second } = dateTime;
|
|
78
|
+
return ((compiled.years ? compiled.years.includes(year) : year >= minYear && year <= maxYear) &&
|
|
79
|
+
compiled.months.includes(month) &&
|
|
80
|
+
compiled.daysOf(year, month).includes(day) &&
|
|
81
|
+
compiled.hours.includes(hour) &&
|
|
82
|
+
compiled.minutes.includes(minute) &&
|
|
83
|
+
compiled.seconds.includes(second));
|
|
84
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export type Boundaries = readonly [min: number, max: number];
|
|
2
|
+
export declare function isWithinBoundaries(value: number, [min, max]: Boundaries): boolean;
|
|
3
|
+
export interface FieldGrammar {
|
|
4
|
+
/** Inclusive bounds, in the field's native numbering (e.g. Quartz day-of-week is 1-7). */
|
|
5
|
+
bounds: Boundaries;
|
|
6
|
+
/** Resolves a single token (number or name) to its native value, or NaN. */
|
|
7
|
+
parseAtom: (token: string) => number;
|
|
8
|
+
/** Largest allowed increment after `/`; unbounded when omitted. */
|
|
9
|
+
maxStep?: number;
|
|
10
|
+
/** Whether a name may start a step (`MON/2`). Defaults to true. */
|
|
11
|
+
allowNamedStepStart?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** Items in the field's native numbering. Field modules map them to canonical values. */
|
|
14
|
+
export type OperatorItem = {
|
|
15
|
+
kind: "value";
|
|
16
|
+
value: number;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "range";
|
|
19
|
+
start: number;
|
|
20
|
+
end: number;
|
|
21
|
+
step?: number;
|
|
22
|
+
} | {
|
|
23
|
+
kind: "step";
|
|
24
|
+
start: number | "any";
|
|
25
|
+
step: number;
|
|
26
|
+
};
|
|
27
|
+
export type OperatorExpr = {
|
|
28
|
+
kind: "any";
|
|
29
|
+
} | {
|
|
30
|
+
kind: "list";
|
|
31
|
+
items: OperatorItem[];
|
|
32
|
+
};
|
|
33
|
+
export interface OperatorIssue {
|
|
34
|
+
code: "INVALID_VALUE" | "INVALID_RANGE" | "INVALID_STEP";
|
|
35
|
+
token: string;
|
|
36
|
+
}
|
|
37
|
+
export type OperatorResult = {
|
|
38
|
+
ok: true;
|
|
39
|
+
expr: OperatorExpr;
|
|
40
|
+
} | {
|
|
41
|
+
ok: false;
|
|
42
|
+
issue: OperatorIssue;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Shared grammar for cron field values: wildcard, comma lists, dashed ranges,
|
|
46
|
+
* and slash steps. Each field supplies its own bounds and atom resolver (plain
|
|
47
|
+
* numbers, or names like JAN/SUN) and gets list/range/step parsing for free.
|
|
48
|
+
*/
|
|
49
|
+
export declare function parseOperatorExpression(expression: string, grammar: FieldGrammar): OperatorResult;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { parseCronInt } from "./parse-cron-int.js";
|
|
2
|
+
export function isWithinBoundaries(value, [min, max]) {
|
|
3
|
+
return value >= min && value <= max;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Shared grammar for cron field values: wildcard, comma lists, dashed ranges,
|
|
7
|
+
* and slash steps. Each field supplies its own bounds and atom resolver (plain
|
|
8
|
+
* numbers, or names like JAN/SUN) and gets list/range/step parsing for free.
|
|
9
|
+
*/
|
|
10
|
+
export function parseOperatorExpression(expression, grammar) {
|
|
11
|
+
if (expression === "*")
|
|
12
|
+
return { ok: true, expr: { kind: "any" } };
|
|
13
|
+
const items = [];
|
|
14
|
+
for (const token of expression.split(",")) {
|
|
15
|
+
const result = parseItem(token, grammar);
|
|
16
|
+
if (!result.ok)
|
|
17
|
+
return result;
|
|
18
|
+
items.push(result.item);
|
|
19
|
+
}
|
|
20
|
+
return { ok: true, expr: { kind: "list", items } };
|
|
21
|
+
}
|
|
22
|
+
function parseItem(token, grammar) {
|
|
23
|
+
if (token.includes("/"))
|
|
24
|
+
return parseStep(token, grammar);
|
|
25
|
+
if (token.includes("-"))
|
|
26
|
+
return parseRange(token, grammar);
|
|
27
|
+
const value = parseAtom(token, grammar);
|
|
28
|
+
if (Number.isNaN(value))
|
|
29
|
+
return invalid("INVALID_VALUE", token);
|
|
30
|
+
return { ok: true, item: { kind: "value", value } };
|
|
31
|
+
}
|
|
32
|
+
function parseStep(token, grammar) {
|
|
33
|
+
const parts = token.split("/");
|
|
34
|
+
if (parts.length !== 2)
|
|
35
|
+
return invalid("INVALID_STEP", token);
|
|
36
|
+
const [base, increment] = parts;
|
|
37
|
+
const step = parseCronInt(increment);
|
|
38
|
+
const maxStep = grammar.maxStep ?? Number.POSITIVE_INFINITY;
|
|
39
|
+
if (!(step >= 1 && step <= maxStep))
|
|
40
|
+
return invalid("INVALID_STEP", token);
|
|
41
|
+
if (grammar.allowNamedStepStart === false && /[a-z]/i.test(base)) {
|
|
42
|
+
return invalid("INVALID_STEP", token);
|
|
43
|
+
}
|
|
44
|
+
if (base === "*")
|
|
45
|
+
return { ok: true, item: { kind: "step", start: "any", step } };
|
|
46
|
+
if (base.includes("-")) {
|
|
47
|
+
const range = parseRange(base, grammar);
|
|
48
|
+
if (!range.ok || range.item.kind !== "range")
|
|
49
|
+
return range;
|
|
50
|
+
return { ok: true, item: { ...range.item, step } };
|
|
51
|
+
}
|
|
52
|
+
const start = parseAtom(base, grammar);
|
|
53
|
+
if (Number.isNaN(start))
|
|
54
|
+
return invalid("INVALID_VALUE", base);
|
|
55
|
+
return { ok: true, item: { kind: "step", start, step } };
|
|
56
|
+
}
|
|
57
|
+
function parseRange(token, grammar) {
|
|
58
|
+
const parts = token.split("-");
|
|
59
|
+
if (parts.length !== 2)
|
|
60
|
+
return invalid("INVALID_RANGE", token);
|
|
61
|
+
const [startToken, endToken] = parts;
|
|
62
|
+
const start = parseAtom(startToken, grammar);
|
|
63
|
+
if (Number.isNaN(start))
|
|
64
|
+
return invalid("INVALID_VALUE", startToken);
|
|
65
|
+
const end = parseAtom(endToken, grammar);
|
|
66
|
+
if (Number.isNaN(end))
|
|
67
|
+
return invalid("INVALID_VALUE", endToken);
|
|
68
|
+
if (start > end)
|
|
69
|
+
return invalid("INVALID_RANGE", token);
|
|
70
|
+
return { ok: true, item: { kind: "range", start, end } };
|
|
71
|
+
}
|
|
72
|
+
/** Native value of a single token, or NaN when it is unknown or out of bounds. */
|
|
73
|
+
function parseAtom(token, grammar) {
|
|
74
|
+
const value = grammar.parseAtom(token);
|
|
75
|
+
return isWithinBoundaries(value, grammar.bounds) ? value : NaN;
|
|
76
|
+
}
|
|
77
|
+
function invalid(code, token) {
|
|
78
|
+
return { ok: false, issue: { code, token } };
|
|
79
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseCronInt(value: string): number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cron-toolkit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Cron toolkit: parse, validate, describe (en/es/fr), and compute next runs in any time zone. Quartz and Unix dialects, zero dependencies, built on Temporal and Intl.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./describe": {
|
|
14
|
+
"types": "./dist/describe.d.ts",
|
|
15
|
+
"import": "./dist/describe.js"
|
|
16
|
+
},
|
|
17
|
+
"./schedule": {
|
|
18
|
+
"types": "./dist/schedule.d.ts",
|
|
19
|
+
"import": "./dist/schedule.js"
|
|
20
|
+
},
|
|
21
|
+
"./locales/*": {
|
|
22
|
+
"types": "./dist/locales/*.d.ts",
|
|
23
|
+
"import": "./dist/locales/*.js"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public",
|
|
33
|
+
"provenance": true
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=22"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/andrewaldasoro/cron-toolkit.git"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/andrewaldasoro/cron-toolkit#readme",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/andrewaldasoro/cron-toolkit/issues"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsc -p tsconfig.build.json",
|
|
48
|
+
"prepack": "pnpm build",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"test:watch": "vitest",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"lint": "oxlint ."
|
|
53
|
+
},
|
|
54
|
+
"keywords": [
|
|
55
|
+
"cron",
|
|
56
|
+
"quartz",
|
|
57
|
+
"crontab",
|
|
58
|
+
"schedule",
|
|
59
|
+
"next-run",
|
|
60
|
+
"timezone",
|
|
61
|
+
"temporal",
|
|
62
|
+
"describe",
|
|
63
|
+
"i18n",
|
|
64
|
+
"validator"
|
|
65
|
+
],
|
|
66
|
+
"author": "Andrew Aldasoro",
|
|
67
|
+
"license": "ISC",
|
|
68
|
+
"packageManager": "pnpm@11.16.0",
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"oxlint": "^1.76.0",
|
|
71
|
+
"temporal-polyfill": "^1.0.8",
|
|
72
|
+
"typescript": "^7.0.2",
|
|
73
|
+
"vitest": "^4.1.10"
|
|
74
|
+
}
|
|
75
|
+
}
|