rrule-temporal 0.1.1 → 0.1.3
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/dist/index.d.ts +35 -0
- package/dist/index.js +455 -50
- package/dist/tests/rrule-temporal.test.js +105 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ interface ManualOpts extends BaseOpts {
|
|
|
10
10
|
until?: Temporal.ZonedDateTime;
|
|
11
11
|
byHour?: number[];
|
|
12
12
|
byMinute?: number[];
|
|
13
|
+
byDay?: string[];
|
|
14
|
+
byMonth?: number[];
|
|
13
15
|
dtstart: string | Temporal.ZonedDateTime;
|
|
14
16
|
}
|
|
15
17
|
interface IcsOpts extends BaseOpts {
|
|
@@ -26,10 +28,43 @@ export declare class RRuleTemporal {
|
|
|
26
28
|
private rawAdvance;
|
|
27
29
|
private applyTimeOverride;
|
|
28
30
|
private computeFirst;
|
|
31
|
+
private generateWeeklyOccurrences;
|
|
32
|
+
private matchesByDay;
|
|
33
|
+
private matchesByMonth;
|
|
34
|
+
private matchesAll;
|
|
35
|
+
/**
|
|
36
|
+
* Returns all occurrences of the rule.
|
|
37
|
+
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
38
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
39
|
+
*/
|
|
29
40
|
all(iterator?: (date: Temporal.ZonedDateTime, i: number) => boolean): Temporal.ZonedDateTime[];
|
|
41
|
+
/**
|
|
42
|
+
* Returns all occurrences of the rule within a specified time window.
|
|
43
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
44
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
45
|
+
* @param inc - Optional boolean flag to include the end date in the results.
|
|
46
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
47
|
+
*/
|
|
30
48
|
between(after: Date | Temporal.ZonedDateTime, before: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime[];
|
|
49
|
+
/**
|
|
50
|
+
* Returns the next occurrence of the rule after a specified date.
|
|
51
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
52
|
+
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
53
|
+
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
54
|
+
*/
|
|
31
55
|
next(after?: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime | null;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the previous occurrence of the rule before a specified date.
|
|
58
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
59
|
+
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
60
|
+
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
61
|
+
*/
|
|
32
62
|
previous(before?: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime | null;
|
|
33
63
|
toString(): string;
|
|
64
|
+
/**
|
|
65
|
+
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
66
|
+
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
67
|
+
*/
|
|
68
|
+
private generateMonthlyOccurrences;
|
|
34
69
|
}
|
|
35
70
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
// rrule-temporal.ts
|
|
2
|
-
// A robust RFC-5545 RRule implementation using the Temporal polyfill
|
|
3
|
-
// Supports FREQ=DAILY|WEEKLY|MONTHLY|YEARLY, BYHOUR, BYMINUTE, DTSTART, UNTIL, COUNT, TZID
|
|
4
2
|
import { Temporal } from "@js-temporal/polyfill";
|
|
5
3
|
/**
|
|
6
4
|
* Parse either a full ICS snippet or an RRULE line into ManualOpts
|
|
7
|
-
* Accepts:
|
|
8
|
-
* - "DTSTART...\nRRULE:..." ICS snippet
|
|
9
|
-
* - "RRULE:..." alone (dtstart must be provided by caller)
|
|
10
5
|
*/
|
|
11
6
|
function parseRRuleString(input, fallbackDtstart) {
|
|
12
7
|
let dtstart;
|
|
13
8
|
let tzid = "";
|
|
14
9
|
let rruleLine;
|
|
15
|
-
console.log("input", input);
|
|
16
10
|
if (/^DTSTART/m.test(input)) {
|
|
17
11
|
// ICS snippet: split DTSTART and RRULE
|
|
18
12
|
const [dtLine, rrLine] = input.split(/\r?\n/);
|
|
@@ -20,7 +14,8 @@ function parseRRuleString(input, fallbackDtstart) {
|
|
|
20
14
|
if (!m)
|
|
21
15
|
throw new Error("Invalid DTSTART in ICS snippet");
|
|
22
16
|
tzid = m[1] || tzid;
|
|
23
|
-
const isoDate = `${m[2].slice(0, 4)}-${m[2].slice(4, 6)}-${m[2].slice(6, 8)}
|
|
17
|
+
const isoDate = `${m[2].slice(0, 4)}-${m[2].slice(4, 6)}-${m[2].slice(6, 8)}` +
|
|
18
|
+
`T${m[2].slice(9)}`;
|
|
24
19
|
dtstart = Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid);
|
|
25
20
|
rruleLine = rrLine;
|
|
26
21
|
}
|
|
@@ -34,7 +29,7 @@ function parseRRuleString(input, fallbackDtstart) {
|
|
|
34
29
|
}
|
|
35
30
|
// Parse RRULE
|
|
36
31
|
const parts = rruleLine.replace(/^RRULE:/, "").split(";");
|
|
37
|
-
const opts = { dtstart, tzid
|
|
32
|
+
const opts = { dtstart, tzid };
|
|
38
33
|
for (const part of parts) {
|
|
39
34
|
const [key, val] = part.split("=");
|
|
40
35
|
switch (key) {
|
|
@@ -50,11 +45,13 @@ function parseRRuleString(input, fallbackDtstart) {
|
|
|
50
45
|
case "UNTIL": {
|
|
51
46
|
// RFC5545 UNTIL is YYYYMMDDTHHMMSSZ or without Z
|
|
52
47
|
if (/Z$/.test(val)) {
|
|
53
|
-
const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}
|
|
48
|
+
const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-` +
|
|
49
|
+
`${val.slice(6, 8)}T${val.slice(9, 15)}Z`;
|
|
54
50
|
opts.until = Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
|
|
55
51
|
}
|
|
56
52
|
else {
|
|
57
|
-
const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}
|
|
53
|
+
const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-` +
|
|
54
|
+
`${val.slice(6, 8)}T${val.slice(9, 15)}`;
|
|
58
55
|
opts.until = Temporal.PlainDateTime.from(iso).toZonedDateTime(tzid || "UTC");
|
|
59
56
|
}
|
|
60
57
|
break;
|
|
@@ -65,6 +62,12 @@ function parseRRuleString(input, fallbackDtstart) {
|
|
|
65
62
|
case "BYMINUTE":
|
|
66
63
|
opts.byMinute = val.split(",").map((n) => parseInt(n, 10));
|
|
67
64
|
break;
|
|
65
|
+
case "BYDAY":
|
|
66
|
+
opts.byDay = val.split(","); // e.g. ["MO","2FR","-1SU"]
|
|
67
|
+
break;
|
|
68
|
+
case "BYMONTH":
|
|
69
|
+
opts.byMonth = val.split(",").map((n) => parseInt(n, 10));
|
|
70
|
+
break;
|
|
68
71
|
}
|
|
69
72
|
}
|
|
70
73
|
return opts;
|
|
@@ -76,13 +79,11 @@ export class RRuleTemporal {
|
|
|
76
79
|
constructor(params) {
|
|
77
80
|
let manual;
|
|
78
81
|
if ("rruleString" in params) {
|
|
79
|
-
// parse full ICS snippet
|
|
80
82
|
manual = parseRRuleString(params.rruleString);
|
|
81
83
|
this.tzid = manual.tzid || "UTC";
|
|
82
84
|
this.originalDtstart = manual.dtstart;
|
|
83
85
|
}
|
|
84
86
|
else {
|
|
85
|
-
// manual options
|
|
86
87
|
manual = { ...params };
|
|
87
88
|
if (typeof manual.dtstart === "string") {
|
|
88
89
|
throw new Error("Manual dtstart must be a ZonedDateTime");
|
|
@@ -93,7 +94,6 @@ export class RRuleTemporal {
|
|
|
93
94
|
}
|
|
94
95
|
if (!manual.freq)
|
|
95
96
|
throw new Error("RRULE must include FREQ");
|
|
96
|
-
// default interval
|
|
97
97
|
manual.interval = manual.interval ?? 1;
|
|
98
98
|
this.opts = manual;
|
|
99
99
|
}
|
|
@@ -128,38 +128,269 @@ export class RRuleTemporal {
|
|
|
128
128
|
return dt;
|
|
129
129
|
}
|
|
130
130
|
computeFirst() {
|
|
131
|
+
let zdt = this.originalDtstart;
|
|
132
|
+
// If it's WEEKLY with BYDAY, advance zdt to the first matching weekday ≥ dtstart
|
|
133
|
+
if (this.opts.freq === "WEEKLY" && this.opts.byDay?.length) {
|
|
134
|
+
const dayMap = {
|
|
135
|
+
MO: 1,
|
|
136
|
+
TU: 2,
|
|
137
|
+
WE: 3,
|
|
138
|
+
TH: 4,
|
|
139
|
+
FR: 5,
|
|
140
|
+
SA: 6,
|
|
141
|
+
SU: 7,
|
|
142
|
+
};
|
|
143
|
+
const targetDow = dayMap[this.opts.byDay[0]];
|
|
144
|
+
const delta = (targetDow - zdt.dayOfWeek + 7) % 7;
|
|
145
|
+
zdt = zdt.add({ days: delta });
|
|
146
|
+
}
|
|
147
|
+
// then your existing BYHOUR/BYMINUTE override logic:
|
|
131
148
|
const { byHour, byMinute } = this.opts;
|
|
132
|
-
let candidate = this.originalDtstart;
|
|
133
149
|
if (byHour || byMinute) {
|
|
134
|
-
|
|
135
|
-
if (Temporal.Instant.compare(
|
|
136
|
-
|
|
150
|
+
zdt = this.applyTimeOverride(zdt);
|
|
151
|
+
if (Temporal.Instant.compare(zdt.toInstant(), this.originalDtstart.toInstant()) < 0) {
|
|
152
|
+
zdt = this.applyTimeOverride(this.rawAdvance(this.originalDtstart));
|
|
137
153
|
}
|
|
138
154
|
}
|
|
139
|
-
return
|
|
155
|
+
return zdt;
|
|
156
|
+
}
|
|
157
|
+
// inside class RRuleTemporal:
|
|
158
|
+
generateWeeklyOccurrences(sample) {
|
|
159
|
+
const { byDay } = this.opts;
|
|
160
|
+
// Map token → ISO dayOfWeek
|
|
161
|
+
const dayMap = {
|
|
162
|
+
MO: 1,
|
|
163
|
+
TU: 2,
|
|
164
|
+
WE: 3,
|
|
165
|
+
TH: 4,
|
|
166
|
+
FR: 5,
|
|
167
|
+
SA: 6,
|
|
168
|
+
SU: 7,
|
|
169
|
+
};
|
|
170
|
+
// If no BYDAY, default to the sample’s own weekday token
|
|
171
|
+
const tokens = byDay && byDay.length
|
|
172
|
+
? byDay
|
|
173
|
+
: [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
|
|
174
|
+
// For each token, find the date in [sample, sample+6d] matching that weekday
|
|
175
|
+
const occs = tokens.map((tok) => {
|
|
176
|
+
const targetDow = dayMap[tok];
|
|
177
|
+
const delta = (targetDow - sample.dayOfWeek + 7) % 7;
|
|
178
|
+
return this.applyTimeOverride(sample.add({ days: delta }));
|
|
179
|
+
});
|
|
180
|
+
// Return in chronological order
|
|
181
|
+
return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
182
|
+
}
|
|
183
|
+
// --- NEW: constraint checks ---
|
|
184
|
+
// 2) Replace your matchesByDay with this:
|
|
185
|
+
matchesByDay(zdt) {
|
|
186
|
+
const { byDay } = this.opts;
|
|
187
|
+
if (!byDay)
|
|
188
|
+
return true;
|
|
189
|
+
// map two‑letter to Temporal dayOfWeek (1=Mon … 7=Sun)
|
|
190
|
+
const dayMap = {
|
|
191
|
+
MO: 1,
|
|
192
|
+
TU: 2,
|
|
193
|
+
WE: 3,
|
|
194
|
+
TH: 4,
|
|
195
|
+
FR: 5,
|
|
196
|
+
SA: 6,
|
|
197
|
+
SU: 7,
|
|
198
|
+
};
|
|
199
|
+
for (const token of byDay) {
|
|
200
|
+
// 1) match and destructure
|
|
201
|
+
const m = token.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
202
|
+
if (!m)
|
|
203
|
+
continue;
|
|
204
|
+
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
205
|
+
// 2) pull weekday into its own variable and guard
|
|
206
|
+
const weekday = m[2];
|
|
207
|
+
if (!weekday)
|
|
208
|
+
continue; // now TS knows `weekday` is string
|
|
209
|
+
const wd = dayMap[weekday]; // no more "undefined index" error
|
|
210
|
+
// no ordinal → simple weekday match
|
|
211
|
+
if (ord === 0) {
|
|
212
|
+
if (zdt.dayOfWeek === wd)
|
|
213
|
+
return true;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
// build all days in month with this weekday
|
|
217
|
+
const year = zdt.year;
|
|
218
|
+
const month = zdt.month;
|
|
219
|
+
let dt = zdt.with({ day: 1 });
|
|
220
|
+
const candidates = [];
|
|
221
|
+
while (dt.month === month) {
|
|
222
|
+
if (dt.dayOfWeek === wd)
|
|
223
|
+
candidates.push(dt.day);
|
|
224
|
+
dt = dt.add({ days: 1 });
|
|
225
|
+
}
|
|
226
|
+
// pick the “ord-th” entry (supports negative ord)
|
|
227
|
+
const idx = ord > 0 ? ord - 1 : candidates.length + ord;
|
|
228
|
+
if (candidates[idx] === zdt.day)
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
matchesByMonth(zdt) {
|
|
234
|
+
const { byMonth } = this.opts;
|
|
235
|
+
if (!byMonth)
|
|
236
|
+
return true;
|
|
237
|
+
return byMonth.includes(zdt.month);
|
|
140
238
|
}
|
|
239
|
+
matchesAll(zdt) {
|
|
240
|
+
return this.matchesByDay(zdt) && this.matchesByMonth(zdt);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Returns all occurrences of the rule.
|
|
244
|
+
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
245
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
246
|
+
*/
|
|
141
247
|
all(iterator) {
|
|
142
248
|
if (!this.opts.count && !this.opts.until && !iterator) {
|
|
143
249
|
throw new Error("all() requires iterator when no COUNT/UNTIL");
|
|
144
250
|
}
|
|
145
251
|
const dates = [];
|
|
252
|
+
// --- 1) MONTHLY + BYDAY (multi-day expansions) ---
|
|
253
|
+
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
254
|
+
const start = this.originalDtstart;
|
|
255
|
+
let monthCursor = start.with({ day: 1 });
|
|
256
|
+
let matchCount = 0;
|
|
257
|
+
outer_month: while (true) {
|
|
258
|
+
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
259
|
+
for (const occ of occs) {
|
|
260
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0)
|
|
261
|
+
continue;
|
|
262
|
+
if (this.opts.until &&
|
|
263
|
+
Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
264
|
+
break outer_month;
|
|
265
|
+
}
|
|
266
|
+
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
267
|
+
break outer_month;
|
|
268
|
+
}
|
|
269
|
+
if (iterator && !iterator(occ, matchCount)) {
|
|
270
|
+
break outer_month;
|
|
271
|
+
}
|
|
272
|
+
dates.push(occ);
|
|
273
|
+
matchCount++;
|
|
274
|
+
}
|
|
275
|
+
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
276
|
+
}
|
|
277
|
+
return dates;
|
|
278
|
+
}
|
|
279
|
+
// --- 2) WEEKLY + BYDAY (or default to DTSTART’s weekday) ---
|
|
280
|
+
if (this.opts.freq === "WEEKLY") {
|
|
281
|
+
const start = this.originalDtstart;
|
|
282
|
+
// Build the list of target weekdays (1=Mon..7=Sun)
|
|
283
|
+
const dayMap = {
|
|
284
|
+
MO: 1,
|
|
285
|
+
TU: 2,
|
|
286
|
+
WE: 3,
|
|
287
|
+
TH: 4,
|
|
288
|
+
FR: 5,
|
|
289
|
+
SA: 6,
|
|
290
|
+
SU: 7,
|
|
291
|
+
};
|
|
292
|
+
// If no BYDAY, default to DTSTART’s weekday token
|
|
293
|
+
const tokens = this.opts.byDay
|
|
294
|
+
? [...this.opts.byDay]
|
|
295
|
+
: [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
|
|
296
|
+
const dows = tokens
|
|
297
|
+
.map((tok) => dayMap[tok])
|
|
298
|
+
.filter((d) => d !== undefined)
|
|
299
|
+
.sort((a, b) => a - b);
|
|
300
|
+
// Find the very first weekCursor: the earliest of this week’s matching days ≥ start
|
|
301
|
+
const firstWeekDates = dows.map((dw) => {
|
|
302
|
+
const delta = (dw - start.dayOfWeek + 7) % 7;
|
|
303
|
+
return start.add({ days: delta });
|
|
304
|
+
});
|
|
305
|
+
let weekCursor = firstWeekDates.reduce((a, b) => Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
|
|
306
|
+
let matchCount = 0;
|
|
307
|
+
outer_week: while (true) {
|
|
308
|
+
// Generate this week’s occurrences
|
|
309
|
+
const baseDow = weekCursor.dayOfWeek;
|
|
310
|
+
const occs = dows
|
|
311
|
+
.map((dw) => {
|
|
312
|
+
const delta = dw - baseDow;
|
|
313
|
+
return this.applyTimeOverride(weekCursor.add({ days: delta }));
|
|
314
|
+
})
|
|
315
|
+
.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
316
|
+
for (const occ of occs) {
|
|
317
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0)
|
|
318
|
+
continue;
|
|
319
|
+
if (this.opts.until &&
|
|
320
|
+
Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
321
|
+
break outer_week;
|
|
322
|
+
}
|
|
323
|
+
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
324
|
+
break outer_week;
|
|
325
|
+
}
|
|
326
|
+
if (iterator && !iterator(occ, matchCount)) {
|
|
327
|
+
break outer_week;
|
|
328
|
+
}
|
|
329
|
+
dates.push(occ);
|
|
330
|
+
matchCount++;
|
|
331
|
+
}
|
|
332
|
+
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
333
|
+
}
|
|
334
|
+
return dates;
|
|
335
|
+
}
|
|
336
|
+
// --- 3) YEARLY + BYMONTH (one per year, rotating) ---
|
|
337
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
338
|
+
const start = this.originalDtstart;
|
|
339
|
+
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
340
|
+
let i = 0;
|
|
341
|
+
while (true) {
|
|
342
|
+
const year = start.year + i * this.opts.interval;
|
|
343
|
+
const month = months[i % months.length];
|
|
344
|
+
let occ = start.with({ year, month });
|
|
345
|
+
occ = this.applyTimeOverride(occ);
|
|
346
|
+
if (i === 0 && Temporal.ZonedDateTime.compare(occ, start) < 0) {
|
|
347
|
+
i++;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (this.opts.until &&
|
|
351
|
+
Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
if (this.opts.count !== undefined && i >= this.opts.count) {
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
if (iterator && !iterator(occ, i)) {
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
dates.push(occ);
|
|
361
|
+
i++;
|
|
362
|
+
}
|
|
363
|
+
return dates;
|
|
364
|
+
}
|
|
365
|
+
// --- 4) fallback: step + filter ---
|
|
146
366
|
let current = this.computeFirst();
|
|
147
|
-
let
|
|
367
|
+
let matchCount = 0;
|
|
148
368
|
while (true) {
|
|
149
369
|
if (this.opts.until &&
|
|
150
|
-
Temporal.ZonedDateTime.compare(current, this.opts.until) > 0)
|
|
370
|
+
Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
|
|
151
371
|
break;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
372
|
+
}
|
|
373
|
+
if (this.matchesAll(current)) {
|
|
374
|
+
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
if (iterator && !iterator(current, matchCount)) {
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
dates.push(current);
|
|
381
|
+
matchCount++;
|
|
382
|
+
}
|
|
383
|
+
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
160
384
|
}
|
|
161
385
|
return dates;
|
|
162
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Returns all occurrences of the rule within a specified time window.
|
|
389
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
390
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
391
|
+
* @param inc - Optional boolean flag to include the end date in the results.
|
|
392
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
393
|
+
*/
|
|
163
394
|
between(after, before, inc = false) {
|
|
164
395
|
const startInst = after instanceof Date
|
|
165
396
|
? Temporal.Instant.from(after.toISOString())
|
|
@@ -168,70 +399,161 @@ export class RRuleTemporal {
|
|
|
168
399
|
? Temporal.Instant.from(before.toISOString())
|
|
169
400
|
: before.toInstant();
|
|
170
401
|
const results = [];
|
|
402
|
+
// 1) MONTHLY + BYDAY
|
|
403
|
+
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
404
|
+
let monthCursor = this.computeFirst().with({ day: 1 });
|
|
405
|
+
outer: while (true) {
|
|
406
|
+
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
407
|
+
for (const occ of occs) {
|
|
408
|
+
const inst = occ.toInstant();
|
|
409
|
+
if (inc
|
|
410
|
+
? Temporal.Instant.compare(inst, endInst) > 0
|
|
411
|
+
: Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
412
|
+
break outer;
|
|
413
|
+
}
|
|
414
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
415
|
+
results.push(occ);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
419
|
+
}
|
|
420
|
+
return results;
|
|
421
|
+
}
|
|
422
|
+
// 2) YEARLY + BYMONTH
|
|
423
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
424
|
+
const start = this.originalDtstart;
|
|
425
|
+
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
426
|
+
let i = 0;
|
|
427
|
+
outer: while (true) {
|
|
428
|
+
const year = start.year + i * this.opts.interval;
|
|
429
|
+
const month = months[i % months.length];
|
|
430
|
+
let occ = start.with({ year, month });
|
|
431
|
+
occ = this.applyTimeOverride(occ);
|
|
432
|
+
const inst = occ.toInstant();
|
|
433
|
+
if (inc
|
|
434
|
+
? Temporal.Instant.compare(inst, endInst) > 0
|
|
435
|
+
: Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
439
|
+
results.push(occ);
|
|
440
|
+
}
|
|
441
|
+
i++;
|
|
442
|
+
}
|
|
443
|
+
return results;
|
|
444
|
+
}
|
|
445
|
+
if (this.opts.freq === "WEEKLY") {
|
|
446
|
+
const startInst = after instanceof Date
|
|
447
|
+
? Temporal.Instant.from(after.toISOString())
|
|
448
|
+
: after.toInstant();
|
|
449
|
+
const endInst = before instanceof Date
|
|
450
|
+
? Temporal.Instant.from(before.toISOString())
|
|
451
|
+
: before.toInstant();
|
|
452
|
+
let weekCursor = this.computeFirst();
|
|
453
|
+
const results = [];
|
|
454
|
+
outer: while (true) {
|
|
455
|
+
const occs = this.generateWeeklyOccurrences(weekCursor);
|
|
456
|
+
for (const occ of occs) {
|
|
457
|
+
const inst = occ.toInstant();
|
|
458
|
+
// break when beyond end
|
|
459
|
+
if (inc
|
|
460
|
+
? Temporal.Instant.compare(inst, endInst) > 0
|
|
461
|
+
: Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
462
|
+
break outer;
|
|
463
|
+
}
|
|
464
|
+
// include if on/after start
|
|
465
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
466
|
+
results.push(occ);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
470
|
+
}
|
|
471
|
+
return results;
|
|
472
|
+
}
|
|
473
|
+
// 3) fallback
|
|
171
474
|
let current = this.computeFirst();
|
|
172
475
|
while (true) {
|
|
173
476
|
const inst = current.toInstant();
|
|
174
|
-
// NEW: we break *earlier* when exclusive
|
|
175
477
|
if (inc) {
|
|
176
|
-
// inclusive: keep inst ≤ endInst, so only break when inst > endInst
|
|
177
478
|
if (Temporal.Instant.compare(inst, endInst) > 0)
|
|
178
479
|
break;
|
|
179
480
|
}
|
|
180
481
|
else {
|
|
181
|
-
// exclusive: drop inst ≥ endInst, so break when inst ≥ endInst
|
|
182
482
|
if (Temporal.Instant.compare(inst, endInst) >= 0)
|
|
183
483
|
break;
|
|
184
484
|
}
|
|
185
|
-
|
|
186
|
-
|
|
485
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0 &&
|
|
486
|
+
this.matchesAll(current)) {
|
|
187
487
|
results.push(current);
|
|
188
488
|
}
|
|
189
489
|
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
190
490
|
}
|
|
191
491
|
return results;
|
|
192
492
|
}
|
|
493
|
+
/**
|
|
494
|
+
* Returns the next occurrence of the rule after a specified date.
|
|
495
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
496
|
+
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
497
|
+
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
498
|
+
*/
|
|
193
499
|
next(after = new Date(), inc = false) {
|
|
194
500
|
const afterInst = after instanceof Date
|
|
195
501
|
? Temporal.Instant.from(after.toISOString())
|
|
196
502
|
: after.toInstant();
|
|
197
503
|
let current = this.computeFirst();
|
|
198
|
-
let
|
|
504
|
+
let matchCount = 0;
|
|
199
505
|
while (true) {
|
|
200
506
|
const inst = current.toInstant();
|
|
201
|
-
if
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
507
|
+
// if this satisfies constraints, check if it's beyond 'after'
|
|
508
|
+
if (this.matchesAll(current)) {
|
|
509
|
+
const ok = inc
|
|
510
|
+
? Temporal.Instant.compare(inst, afterInst) >= 0
|
|
511
|
+
: Temporal.Instant.compare(inst, afterInst) > 0;
|
|
512
|
+
if (ok)
|
|
513
|
+
return current;
|
|
514
|
+
// count it toward COUNT
|
|
515
|
+
matchCount++;
|
|
516
|
+
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
517
|
+
return null;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
// stop on UNTIL
|
|
207
521
|
if (this.opts.until &&
|
|
208
|
-
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0)
|
|
522
|
+
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
|
|
209
523
|
return null;
|
|
524
|
+
}
|
|
210
525
|
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
211
|
-
i++;
|
|
212
526
|
}
|
|
213
527
|
}
|
|
528
|
+
/**
|
|
529
|
+
* Returns the previous occurrence of the rule before a specified date.
|
|
530
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
531
|
+
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
532
|
+
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
533
|
+
*/
|
|
214
534
|
previous(before = new Date(), inc = false) {
|
|
215
535
|
const beforeInst = before instanceof Date
|
|
216
536
|
? Temporal.Instant.from(before.toISOString())
|
|
217
537
|
: before.toInstant();
|
|
218
538
|
let current = this.computeFirst();
|
|
219
539
|
let prev = null;
|
|
220
|
-
let i = 0;
|
|
221
540
|
while (true) {
|
|
222
541
|
const inst = current.toInstant();
|
|
542
|
+
// if beyond window, stop
|
|
223
543
|
if (inc
|
|
224
544
|
? Temporal.Instant.compare(inst, beforeInst) > 0
|
|
225
|
-
: Temporal.Instant.compare(inst, beforeInst) >= 0)
|
|
226
|
-
break;
|
|
227
|
-
prev = current;
|
|
228
|
-
if (this.opts.count !== undefined && i >= this.opts.count - 1)
|
|
545
|
+
: Temporal.Instant.compare(inst, beforeInst) >= 0) {
|
|
229
546
|
break;
|
|
547
|
+
}
|
|
548
|
+
// track it if it matches constraints
|
|
549
|
+
if (this.matchesAll(current)) {
|
|
550
|
+
prev = current;
|
|
551
|
+
}
|
|
230
552
|
if (this.opts.until &&
|
|
231
|
-
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0)
|
|
553
|
+
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
|
|
232
554
|
break;
|
|
555
|
+
}
|
|
233
556
|
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
234
|
-
i++;
|
|
235
557
|
}
|
|
236
558
|
return prev;
|
|
237
559
|
}
|
|
@@ -241,7 +563,7 @@ export class RRuleTemporal {
|
|
|
241
563
|
.replace(/[-:]/g, "");
|
|
242
564
|
const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
|
|
243
565
|
const parts = [];
|
|
244
|
-
const { freq, interval, count, byHour, byMinute } = this.opts;
|
|
566
|
+
const { freq, interval, count, byHour, byMinute, byDay, byMonth } = this.opts;
|
|
245
567
|
parts.push(`FREQ=${freq}`);
|
|
246
568
|
if (interval !== 1)
|
|
247
569
|
parts.push(`INTERVAL=${interval}`);
|
|
@@ -251,6 +573,89 @@ export class RRuleTemporal {
|
|
|
251
573
|
parts.push(`BYHOUR=${byHour.join(",")}`);
|
|
252
574
|
if (byMinute)
|
|
253
575
|
parts.push(`BYMINUTE=${byMinute.join(",")}`);
|
|
576
|
+
if (byDay)
|
|
577
|
+
parts.push(`BYDAY=${byDay.join(",")}`);
|
|
578
|
+
if (byMonth)
|
|
579
|
+
parts.push(`BYMONTH=${byMonth.join(",")}`);
|
|
254
580
|
return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
|
|
255
581
|
}
|
|
582
|
+
/**
|
|
583
|
+
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
584
|
+
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
585
|
+
*/
|
|
586
|
+
generateMonthlyOccurrences(sample) {
|
|
587
|
+
const { byDay, byMonth, tzid } = this.opts;
|
|
588
|
+
// 1) Skip entire month if BYMONTH excludes it
|
|
589
|
+
if (byMonth && !byMonth.includes(sample.month)) {
|
|
590
|
+
return [];
|
|
591
|
+
}
|
|
592
|
+
// 2) If no BYDAY, just return the sample (we'll override time later)
|
|
593
|
+
if (!byDay || byDay.length === 0) {
|
|
594
|
+
return [sample];
|
|
595
|
+
}
|
|
596
|
+
// Map MO,TU…SU → dayOfWeek numbers
|
|
597
|
+
const dayMap = {
|
|
598
|
+
MO: 1,
|
|
599
|
+
TU: 2,
|
|
600
|
+
WE: 3,
|
|
601
|
+
TH: 4,
|
|
602
|
+
FR: 5,
|
|
603
|
+
SA: 6,
|
|
604
|
+
SU: 7,
|
|
605
|
+
};
|
|
606
|
+
const tokens = byDay
|
|
607
|
+
.map((tok) => {
|
|
608
|
+
const m = tok.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
609
|
+
if (!m)
|
|
610
|
+
return null;
|
|
611
|
+
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
612
|
+
const wd = dayMap[m[2]];
|
|
613
|
+
return { ord, wd };
|
|
614
|
+
})
|
|
615
|
+
.filter((x) => x !== null);
|
|
616
|
+
// 3) Build a map from weekday → all day‑of‑month numbers
|
|
617
|
+
const year = sample.year;
|
|
618
|
+
const month = sample.month;
|
|
619
|
+
let cursor = Temporal.ZonedDateTime.from({
|
|
620
|
+
year,
|
|
621
|
+
month,
|
|
622
|
+
day: 1,
|
|
623
|
+
hour: sample.hour,
|
|
624
|
+
minute: sample.minute,
|
|
625
|
+
second: sample.second,
|
|
626
|
+
timeZone: tzid,
|
|
627
|
+
});
|
|
628
|
+
const weekdayBuckets = {};
|
|
629
|
+
while (cursor.month === month) {
|
|
630
|
+
const dow = cursor.dayOfWeek;
|
|
631
|
+
weekdayBuckets[dow] = weekdayBuckets[dow] || [];
|
|
632
|
+
weekdayBuckets[dow].push(cursor.day);
|
|
633
|
+
cursor = cursor.add({ days: 1 });
|
|
634
|
+
}
|
|
635
|
+
// 4) For each token, pick the right day‑of‑month
|
|
636
|
+
const results = [];
|
|
637
|
+
for (const { ord, wd } of tokens) {
|
|
638
|
+
const days = weekdayBuckets[wd] || [];
|
|
639
|
+
if (!days.length)
|
|
640
|
+
continue;
|
|
641
|
+
if (ord === 0) {
|
|
642
|
+
// simple weekday: include *all* Fridays, e.g.
|
|
643
|
+
for (const d of days) {
|
|
644
|
+
results.push(sample.with({ day: d }));
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
else {
|
|
648
|
+
// ordinal: 2FR → days[1], -1FR → days[days.length-1], etc.
|
|
649
|
+
const idx = ord > 0 ? ord - 1 : days.length + ord;
|
|
650
|
+
const dayNum = days[idx];
|
|
651
|
+
if (dayNum) {
|
|
652
|
+
results.push(sample.with({ day: dayNum }));
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
// 5) Apply your BYHOUR/BYMINUTE overrides and sort chronologically
|
|
657
|
+
return results
|
|
658
|
+
.map((zdt) => this.applyTimeOverride(zdt))
|
|
659
|
+
.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
660
|
+
}
|
|
256
661
|
}
|
|
@@ -133,3 +133,108 @@ RRULE:FREQ=DAILY`.trim();
|
|
|
133
133
|
expect(dates).toHaveLength(3);
|
|
134
134
|
});
|
|
135
135
|
});
|
|
136
|
+
describe("RRuleTemporal - BYDAY frequencies", () => {
|
|
137
|
+
const ics = `DTSTART;TZID=America/Chicago:20250325T000000
|
|
138
|
+
RRULE:FREQ=MONTHLY;BYDAY=2FR,4FR;BYHOUR=0;BYMINUTE=0`.trim();
|
|
139
|
+
const rule = new RRuleTemporal({ rruleString: ics });
|
|
140
|
+
// 2025 Apr 22 00:00 UTC
|
|
141
|
+
const start = new Date(Date.UTC(2025, 3, 20, 0, 0));
|
|
142
|
+
// 2026 Apr 22 00:00 UTC
|
|
143
|
+
const end = new Date(Date.UTC(2026, 3, 22, 0, 0, 0));
|
|
144
|
+
test("between returns occurrences in window", () => {
|
|
145
|
+
const arrInc = rule.between(start, end, true);
|
|
146
|
+
console.log("arrInc: ", arrInc.map((d) => d.toString()));
|
|
147
|
+
expect(arrInc.map((d) => d.day)).toEqual([
|
|
148
|
+
25, 9, 23, 13, 27, 11, 25, 8, 22, 12, 26, 10, 24, 14, 28, 12, 26, 9, 23,
|
|
149
|
+
13, 27, 13, 27, 10,
|
|
150
|
+
]);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
describe("RRuleTemporal - BYMONTH with MONTHLY freq (ICS snippet)", () => {
|
|
154
|
+
const ics = `DTSTART;TZID=UTC:20250115T000000
|
|
155
|
+
RRULE:FREQ=MONTHLY;BYMONTH=1,4,7;BYHOUR=0;BYMINUTE=0;COUNT=3`.trim();
|
|
156
|
+
const rule = new RRuleTemporal({ rruleString: ics });
|
|
157
|
+
test("all() returns exactly the 15th of Jan, Apr & Jul", () => {
|
|
158
|
+
const dates = rule.all();
|
|
159
|
+
expect(dates).toHaveLength(3);
|
|
160
|
+
expect(dates.map((d) => d.month)).toEqual([1, 4, 7]);
|
|
161
|
+
expect(dates.map((d) => d.day)).toEqual([15, 15, 15]);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
describe("RRuleTemporal - BYMONTH with YEARLY freq (manual opts)", () => {
|
|
165
|
+
const dtstart = Temporal.ZonedDateTime.from({
|
|
166
|
+
year: 2025,
|
|
167
|
+
month: 1,
|
|
168
|
+
day: 10,
|
|
169
|
+
hour: 9,
|
|
170
|
+
minute: 0,
|
|
171
|
+
timeZone: "UTC",
|
|
172
|
+
});
|
|
173
|
+
const rule = new RRuleTemporal({
|
|
174
|
+
freq: "YEARLY",
|
|
175
|
+
interval: 1,
|
|
176
|
+
count: 4,
|
|
177
|
+
byMonth: [1, 6, 12],
|
|
178
|
+
byHour: [9],
|
|
179
|
+
byMinute: [0],
|
|
180
|
+
dtstart,
|
|
181
|
+
});
|
|
182
|
+
test("all() emits Jan ’25, Jun ’26, Dec ’27, Jan ’28", () => {
|
|
183
|
+
const dates = rule.all();
|
|
184
|
+
expect(dates).toHaveLength(4);
|
|
185
|
+
expect(dates.map((d) => ({ year: d.year, month: d.month }))).toEqual([
|
|
186
|
+
{ year: 2025, month: 1 },
|
|
187
|
+
{ year: 2026, month: 6 },
|
|
188
|
+
{ year: 2027, month: 12 },
|
|
189
|
+
{ year: 2028, month: 1 },
|
|
190
|
+
]);
|
|
191
|
+
for (const d of dates) {
|
|
192
|
+
expect(d.hour).toBe(9);
|
|
193
|
+
expect(d.minute).toBe(0);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
describe("RRuleTemporal - Weekly BYDAY simple all()", () => {
|
|
198
|
+
const ics = `DTSTART;TZID=America/Chicago:20250406T000000
|
|
199
|
+
RRULE:FREQ=WEEKLY;BYDAY=MO;BYHOUR=0;BYMINUTE=0;COUNT=4`.trim();
|
|
200
|
+
const rule = new RRuleTemporal({ rruleString: ics });
|
|
201
|
+
test("all() returns 4 consecutive Mondays", () => {
|
|
202
|
+
const dates = rule.all();
|
|
203
|
+
// dayOfWeek 1 == Monday
|
|
204
|
+
expect(dates).toHaveLength(4);
|
|
205
|
+
expect(dates.map((d) => d.day)).toEqual([7, 14, 21, 28]);
|
|
206
|
+
dates.forEach((d) => expect(d.dayOfWeek).toBe(1));
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
describe("RRuleTemporal - Weekly BYDAY simple between()", () => {
|
|
210
|
+
const ics = `DTSTART;TZID=America/Chicago:20250325T000000
|
|
211
|
+
RRULE:FREQ=WEEKLY;BYDAY=MO;BYHOUR=0;BYMINUTE=0`.trim();
|
|
212
|
+
const rule = new RRuleTemporal({ rruleString: ics });
|
|
213
|
+
// Window: April 22 2025 UTC → May 20 2025 UTC
|
|
214
|
+
const start = new Date(Date.UTC(2025, 3, 22, 0, 0)); // April is month=3
|
|
215
|
+
const end = new Date(Date.UTC(2025, 4, 20, 0, 0)); // May is month=4
|
|
216
|
+
test("between() returns the Mondays falling in that window", () => {
|
|
217
|
+
const arrInc = rule.between(start, end, true);
|
|
218
|
+
// Expect 2025-04-28, 05-05, 05-12, 05-19
|
|
219
|
+
expect(arrInc.map((d) => d.day)).toEqual([28, 5, 12, 19]);
|
|
220
|
+
arrInc.forEach((d) => expect(d.dayOfWeek).toBe(1));
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
describe("RRuleTemporal - Weekly BYDAY frequencies without positional prefix", () => {
|
|
224
|
+
const ics = `DTSTART;TZID=America/Chicago:20250325T000000
|
|
225
|
+
RRULE:FREQ=WEEKLY;BYDAY=MO;BYHOUR=0;BYMINUTE=0`.trim();
|
|
226
|
+
const rule = new RRuleTemporal({ rruleString: ics });
|
|
227
|
+
// 2025 Apr 22 00:00 UTC
|
|
228
|
+
const start = new Date(Date.UTC(2025, 3, 22, 0, 0));
|
|
229
|
+
// 2026 Apr 22 00:00 UTC
|
|
230
|
+
const end = new Date(Date.UTC(2026, 3, 22, 0, 0, 0));
|
|
231
|
+
test("between returns occurrences in window", () => {
|
|
232
|
+
const arrInc = rule.between(start, end, true);
|
|
233
|
+
console.log("arrInc: ", arrInc.map((d) => d.toString()));
|
|
234
|
+
expect(arrInc.map((d) => d.day)).toEqual([
|
|
235
|
+
28, 5, 12, 19, 26, 2, 9, 16, 23, 30, 7, 14, 21, 28, 4, 11, 18, 25, 1, 8,
|
|
236
|
+
15, 22, 29, 6, 13, 20, 27, 3, 10, 17, 24, 1, 8, 15, 22, 29, 5, 12, 19, 26,
|
|
237
|
+
2, 9, 16, 23, 2, 9, 16, 23, 30, 6, 13, 20,
|
|
238
|
+
]);
|
|
239
|
+
});
|
|
240
|
+
});
|