rrule-temporal 0.1.0 → 0.1.2
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/README.md +17 -1
- package/dist/index.d.ts +34 -0
- package/dist/index.js +332 -45
- package/dist/tests/rrule-temporal.test.js +61 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,5 +15,21 @@ npm install rrule-temporal
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
|
-
|
|
18
|
+
const ics = `DTSTART;TZID=America/Chicago:20250401T000000
|
|
19
|
+
RRULE:FREQ=DAILY;BYHOUR=0;BYMINUTE=0;UNTIL=20250405T000000Z`.trim();
|
|
20
|
+
const rule = new RRuleTemporal({ rruleString: ics });
|
|
21
|
+
const start = new Date(Date.UTC(2025, 3, 2, 0, 0)); // Apr 2 00:00 UTC
|
|
22
|
+
// after:
|
|
23
|
+
const end = new Date(
|
|
24
|
+
// 2025-04-04 00:00 America/Chicago → 05:00 UTC
|
|
25
|
+
Date.UTC(2025, 3, 4, 5, 0, 0)
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
test("between returns occurrences in window inclusive/exclusive", () => {
|
|
29
|
+
const arrExc = rule.between(start, end, false);
|
|
30
|
+
expect(arrExc.map((d) => d.day)).toEqual([2, 3]);
|
|
31
|
+
|
|
32
|
+
const arrInc = rule.between(start, end, true);
|
|
33
|
+
expect(arrInc.map((d) => d.day)).toEqual([2, 3, 4]);
|
|
34
|
+
});
|
|
19
35
|
```
|
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,42 @@ export declare class RRuleTemporal {
|
|
|
26
28
|
private rawAdvance;
|
|
27
29
|
private applyTimeOverride;
|
|
28
30
|
private computeFirst;
|
|
31
|
+
private matchesByDay;
|
|
32
|
+
private matchesByMonth;
|
|
33
|
+
private matchesAll;
|
|
34
|
+
/**
|
|
35
|
+
* Returns all occurrences of the rule.
|
|
36
|
+
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
37
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
38
|
+
*/
|
|
29
39
|
all(iterator?: (date: Temporal.ZonedDateTime, i: number) => boolean): Temporal.ZonedDateTime[];
|
|
40
|
+
/**
|
|
41
|
+
* Returns all occurrences of the rule within a specified time window.
|
|
42
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
43
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
44
|
+
* @param inc - Optional boolean flag to include the end date in the results.
|
|
45
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
46
|
+
*/
|
|
30
47
|
between(after: Date | Temporal.ZonedDateTime, before: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime[];
|
|
48
|
+
/**
|
|
49
|
+
* Returns the next occurrence of the rule after a specified date.
|
|
50
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
51
|
+
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
52
|
+
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
53
|
+
*/
|
|
31
54
|
next(after?: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime | null;
|
|
55
|
+
/**
|
|
56
|
+
* Returns the previous occurrence of the rule before a specified date.
|
|
57
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
58
|
+
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
59
|
+
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
60
|
+
*/
|
|
32
61
|
previous(before?: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime | null;
|
|
33
62
|
toString(): string;
|
|
63
|
+
/**
|
|
64
|
+
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
65
|
+
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
66
|
+
*/
|
|
67
|
+
private generateMonthlyOccurrences;
|
|
34
68
|
}
|
|
35
69
|
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
|
}
|
|
@@ -138,28 +138,169 @@ export class RRuleTemporal {
|
|
|
138
138
|
}
|
|
139
139
|
return candidate;
|
|
140
140
|
}
|
|
141
|
+
// --- NEW: constraint checks ---
|
|
142
|
+
// 2) Replace your matchesByDay with this:
|
|
143
|
+
matchesByDay(zdt) {
|
|
144
|
+
const { byDay } = this.opts;
|
|
145
|
+
if (!byDay)
|
|
146
|
+
return true;
|
|
147
|
+
// map two‑letter to Temporal dayOfWeek (1=Mon … 7=Sun)
|
|
148
|
+
const dayMap = {
|
|
149
|
+
MO: 1,
|
|
150
|
+
TU: 2,
|
|
151
|
+
WE: 3,
|
|
152
|
+
TH: 4,
|
|
153
|
+
FR: 5,
|
|
154
|
+
SA: 6,
|
|
155
|
+
SU: 7,
|
|
156
|
+
};
|
|
157
|
+
for (const token of byDay) {
|
|
158
|
+
// 1) match and destructure
|
|
159
|
+
const m = token.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
160
|
+
if (!m)
|
|
161
|
+
continue;
|
|
162
|
+
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
163
|
+
// 2) pull weekday into its own variable and guard
|
|
164
|
+
const weekday = m[2];
|
|
165
|
+
if (!weekday)
|
|
166
|
+
continue; // now TS knows `weekday` is string
|
|
167
|
+
const wd = dayMap[weekday]; // no more "undefined index" error
|
|
168
|
+
// no ordinal → simple weekday match
|
|
169
|
+
if (ord === 0) {
|
|
170
|
+
if (zdt.dayOfWeek === wd)
|
|
171
|
+
return true;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
// build all days in month with this weekday
|
|
175
|
+
const year = zdt.year;
|
|
176
|
+
const month = zdt.month;
|
|
177
|
+
let dt = zdt.with({ day: 1 });
|
|
178
|
+
const candidates = [];
|
|
179
|
+
while (dt.month === month) {
|
|
180
|
+
if (dt.dayOfWeek === wd)
|
|
181
|
+
candidates.push(dt.day);
|
|
182
|
+
dt = dt.add({ days: 1 });
|
|
183
|
+
}
|
|
184
|
+
// pick the “ord-th” entry (supports negative ord)
|
|
185
|
+
const idx = ord > 0 ? ord - 1 : candidates.length + ord;
|
|
186
|
+
if (candidates[idx] === zdt.day)
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
matchesByMonth(zdt) {
|
|
192
|
+
const { byMonth } = this.opts;
|
|
193
|
+
if (!byMonth)
|
|
194
|
+
return true;
|
|
195
|
+
return byMonth.includes(zdt.month);
|
|
196
|
+
}
|
|
197
|
+
matchesAll(zdt) {
|
|
198
|
+
return this.matchesByDay(zdt) && this.matchesByMonth(zdt);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Returns all occurrences of the rule.
|
|
202
|
+
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
203
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
204
|
+
*/
|
|
141
205
|
all(iterator) {
|
|
142
206
|
if (!this.opts.count && !this.opts.until && !iterator) {
|
|
143
207
|
throw new Error("all() requires iterator when no COUNT/UNTIL");
|
|
144
208
|
}
|
|
145
209
|
const dates = [];
|
|
210
|
+
// 1) MONTHLY + BYDAY (multi‐day expansions)
|
|
211
|
+
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
212
|
+
const start = this.originalDtstart;
|
|
213
|
+
let monthCursor = start.with({ day: 1 });
|
|
214
|
+
let matchCount = 0;
|
|
215
|
+
outer: while (true) {
|
|
216
|
+
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
217
|
+
for (const occ of occs) {
|
|
218
|
+
// skip anything before the very first DTSTART
|
|
219
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) {
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
// stop on UNTIL
|
|
223
|
+
if (this.opts.until &&
|
|
224
|
+
Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
225
|
+
break outer;
|
|
226
|
+
}
|
|
227
|
+
// stop on COUNT
|
|
228
|
+
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
229
|
+
break outer;
|
|
230
|
+
}
|
|
231
|
+
// iterator callback
|
|
232
|
+
if (iterator && !iterator(occ, matchCount)) {
|
|
233
|
+
break outer;
|
|
234
|
+
}
|
|
235
|
+
dates.push(occ);
|
|
236
|
+
matchCount++;
|
|
237
|
+
}
|
|
238
|
+
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
239
|
+
}
|
|
240
|
+
return dates;
|
|
241
|
+
}
|
|
242
|
+
// 2) YEARLY + BYMONTH (one per year, rotating through the byMonth array)
|
|
243
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
244
|
+
const start = this.originalDtstart;
|
|
245
|
+
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
246
|
+
let i = 0;
|
|
247
|
+
while (true) {
|
|
248
|
+
const year = start.year + i * this.opts.interval;
|
|
249
|
+
const month = months[i % months.length];
|
|
250
|
+
let occ = start.with({ year, month });
|
|
251
|
+
occ = this.applyTimeOverride(occ);
|
|
252
|
+
// skip the very first if it's before dtstart
|
|
253
|
+
if (i === 0 && Temporal.ZonedDateTime.compare(occ, start) < 0) {
|
|
254
|
+
i++;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
// stop on UNTIL
|
|
258
|
+
if (this.opts.until &&
|
|
259
|
+
Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
// stop on COUNT
|
|
263
|
+
if (this.opts.count !== undefined && i >= this.opts.count) {
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
// iterator callback
|
|
267
|
+
if (iterator && !iterator(occ, i)) {
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
dates.push(occ);
|
|
271
|
+
i++;
|
|
272
|
+
}
|
|
273
|
+
return dates;
|
|
274
|
+
}
|
|
275
|
+
// 3) fallback: step and filter
|
|
146
276
|
let current = this.computeFirst();
|
|
147
|
-
let
|
|
277
|
+
let matchCount = 0;
|
|
148
278
|
while (true) {
|
|
149
279
|
if (this.opts.until &&
|
|
150
|
-
Temporal.ZonedDateTime.compare(current, this.opts.until) > 0)
|
|
151
|
-
break;
|
|
152
|
-
if (this.opts.count !== undefined && i >= this.opts.count)
|
|
153
|
-
break;
|
|
154
|
-
if (iterator && !iterator(current, i))
|
|
280
|
+
Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
|
|
155
281
|
break;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
282
|
+
}
|
|
283
|
+
if (this.matchesAll(current)) {
|
|
284
|
+
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
if (iterator && !iterator(current, matchCount)) {
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
dates.push(current);
|
|
291
|
+
matchCount++;
|
|
292
|
+
}
|
|
293
|
+
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
160
294
|
}
|
|
161
295
|
return dates;
|
|
162
296
|
}
|
|
297
|
+
/**
|
|
298
|
+
* Returns all occurrences of the rule within a specified time window.
|
|
299
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
300
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
301
|
+
* @param inc - Optional boolean flag to include the end date in the results.
|
|
302
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
303
|
+
*/
|
|
163
304
|
between(after, before, inc = false) {
|
|
164
305
|
const startInst = after instanceof Date
|
|
165
306
|
? Temporal.Instant.from(after.toISOString())
|
|
@@ -168,70 +309,133 @@ export class RRuleTemporal {
|
|
|
168
309
|
? Temporal.Instant.from(before.toISOString())
|
|
169
310
|
: before.toInstant();
|
|
170
311
|
const results = [];
|
|
312
|
+
// 1) MONTHLY + BYDAY
|
|
313
|
+
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
314
|
+
let monthCursor = this.computeFirst().with({ day: 1 });
|
|
315
|
+
outer: while (true) {
|
|
316
|
+
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
317
|
+
for (const occ of occs) {
|
|
318
|
+
const inst = occ.toInstant();
|
|
319
|
+
if (inc
|
|
320
|
+
? Temporal.Instant.compare(inst, endInst) > 0
|
|
321
|
+
: Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
322
|
+
break outer;
|
|
323
|
+
}
|
|
324
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
325
|
+
results.push(occ);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
329
|
+
}
|
|
330
|
+
return results;
|
|
331
|
+
}
|
|
332
|
+
// 2) YEARLY + BYMONTH
|
|
333
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
334
|
+
const start = this.originalDtstart;
|
|
335
|
+
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
336
|
+
let i = 0;
|
|
337
|
+
outer: while (true) {
|
|
338
|
+
const year = start.year + i * this.opts.interval;
|
|
339
|
+
const month = months[i % months.length];
|
|
340
|
+
let occ = start.with({ year, month });
|
|
341
|
+
occ = this.applyTimeOverride(occ);
|
|
342
|
+
const inst = occ.toInstant();
|
|
343
|
+
if (inc
|
|
344
|
+
? Temporal.Instant.compare(inst, endInst) > 0
|
|
345
|
+
: Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
349
|
+
results.push(occ);
|
|
350
|
+
}
|
|
351
|
+
i++;
|
|
352
|
+
}
|
|
353
|
+
return results;
|
|
354
|
+
}
|
|
355
|
+
// 3) fallback
|
|
171
356
|
let current = this.computeFirst();
|
|
172
357
|
while (true) {
|
|
173
358
|
const inst = current.toInstant();
|
|
174
|
-
// NEW: we break *earlier* when exclusive
|
|
175
359
|
if (inc) {
|
|
176
|
-
// inclusive: keep inst ≤ endInst, so only break when inst > endInst
|
|
177
360
|
if (Temporal.Instant.compare(inst, endInst) > 0)
|
|
178
361
|
break;
|
|
179
362
|
}
|
|
180
363
|
else {
|
|
181
|
-
// exclusive: drop inst ≥ endInst, so break when inst ≥ endInst
|
|
182
364
|
if (Temporal.Instant.compare(inst, endInst) >= 0)
|
|
183
365
|
break;
|
|
184
366
|
}
|
|
185
|
-
|
|
186
|
-
|
|
367
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0 &&
|
|
368
|
+
this.matchesAll(current)) {
|
|
187
369
|
results.push(current);
|
|
188
370
|
}
|
|
189
371
|
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
190
372
|
}
|
|
191
373
|
return results;
|
|
192
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* Returns the next occurrence of the rule after a specified date.
|
|
377
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
378
|
+
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
379
|
+
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
380
|
+
*/
|
|
193
381
|
next(after = new Date(), inc = false) {
|
|
194
382
|
const afterInst = after instanceof Date
|
|
195
383
|
? Temporal.Instant.from(after.toISOString())
|
|
196
384
|
: after.toInstant();
|
|
197
385
|
let current = this.computeFirst();
|
|
198
|
-
let
|
|
386
|
+
let matchCount = 0;
|
|
199
387
|
while (true) {
|
|
200
388
|
const inst = current.toInstant();
|
|
201
|
-
if
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
389
|
+
// if this satisfies constraints, check if it's beyond 'after'
|
|
390
|
+
if (this.matchesAll(current)) {
|
|
391
|
+
const ok = inc
|
|
392
|
+
? Temporal.Instant.compare(inst, afterInst) >= 0
|
|
393
|
+
: Temporal.Instant.compare(inst, afterInst) > 0;
|
|
394
|
+
if (ok)
|
|
395
|
+
return current;
|
|
396
|
+
// count it toward COUNT
|
|
397
|
+
matchCount++;
|
|
398
|
+
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
399
|
+
return null;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
// stop on UNTIL
|
|
207
403
|
if (this.opts.until &&
|
|
208
|
-
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0)
|
|
404
|
+
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
|
|
209
405
|
return null;
|
|
406
|
+
}
|
|
210
407
|
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
211
|
-
i++;
|
|
212
408
|
}
|
|
213
409
|
}
|
|
410
|
+
/**
|
|
411
|
+
* Returns the previous occurrence of the rule before a specified date.
|
|
412
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
413
|
+
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
414
|
+
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
415
|
+
*/
|
|
214
416
|
previous(before = new Date(), inc = false) {
|
|
215
417
|
const beforeInst = before instanceof Date
|
|
216
418
|
? Temporal.Instant.from(before.toISOString())
|
|
217
419
|
: before.toInstant();
|
|
218
420
|
let current = this.computeFirst();
|
|
219
421
|
let prev = null;
|
|
220
|
-
let i = 0;
|
|
221
422
|
while (true) {
|
|
222
423
|
const inst = current.toInstant();
|
|
424
|
+
// if beyond window, stop
|
|
223
425
|
if (inc
|
|
224
426
|
? 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)
|
|
427
|
+
: Temporal.Instant.compare(inst, beforeInst) >= 0) {
|
|
229
428
|
break;
|
|
429
|
+
}
|
|
430
|
+
// track it if it matches constraints
|
|
431
|
+
if (this.matchesAll(current)) {
|
|
432
|
+
prev = current;
|
|
433
|
+
}
|
|
230
434
|
if (this.opts.until &&
|
|
231
|
-
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0)
|
|
435
|
+
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
|
|
232
436
|
break;
|
|
437
|
+
}
|
|
233
438
|
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
234
|
-
i++;
|
|
235
439
|
}
|
|
236
440
|
return prev;
|
|
237
441
|
}
|
|
@@ -241,7 +445,7 @@ export class RRuleTemporal {
|
|
|
241
445
|
.replace(/[-:]/g, "");
|
|
242
446
|
const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
|
|
243
447
|
const parts = [];
|
|
244
|
-
const { freq, interval, count, byHour, byMinute } = this.opts;
|
|
448
|
+
const { freq, interval, count, byHour, byMinute, byDay, byMonth } = this.opts;
|
|
245
449
|
parts.push(`FREQ=${freq}`);
|
|
246
450
|
if (interval !== 1)
|
|
247
451
|
parts.push(`INTERVAL=${interval}`);
|
|
@@ -251,6 +455,89 @@ export class RRuleTemporal {
|
|
|
251
455
|
parts.push(`BYHOUR=${byHour.join(",")}`);
|
|
252
456
|
if (byMinute)
|
|
253
457
|
parts.push(`BYMINUTE=${byMinute.join(",")}`);
|
|
458
|
+
if (byDay)
|
|
459
|
+
parts.push(`BYDAY=${byDay.join(",")}`);
|
|
460
|
+
if (byMonth)
|
|
461
|
+
parts.push(`BYMONTH=${byMonth.join(",")}`);
|
|
254
462
|
return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
|
|
255
463
|
}
|
|
464
|
+
/**
|
|
465
|
+
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
466
|
+
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
467
|
+
*/
|
|
468
|
+
generateMonthlyOccurrences(sample) {
|
|
469
|
+
const { byDay, byMonth, tzid } = this.opts;
|
|
470
|
+
// 1) Skip entire month if BYMONTH excludes it
|
|
471
|
+
if (byMonth && !byMonth.includes(sample.month)) {
|
|
472
|
+
return [];
|
|
473
|
+
}
|
|
474
|
+
// 2) If no BYDAY, just return the sample (we'll override time later)
|
|
475
|
+
if (!byDay || byDay.length === 0) {
|
|
476
|
+
return [sample];
|
|
477
|
+
}
|
|
478
|
+
// Map MO,TU…SU → dayOfWeek numbers
|
|
479
|
+
const dayMap = {
|
|
480
|
+
MO: 1,
|
|
481
|
+
TU: 2,
|
|
482
|
+
WE: 3,
|
|
483
|
+
TH: 4,
|
|
484
|
+
FR: 5,
|
|
485
|
+
SA: 6,
|
|
486
|
+
SU: 7,
|
|
487
|
+
};
|
|
488
|
+
const tokens = byDay
|
|
489
|
+
.map((tok) => {
|
|
490
|
+
const m = tok.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
491
|
+
if (!m)
|
|
492
|
+
return null;
|
|
493
|
+
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
494
|
+
const wd = dayMap[m[2]];
|
|
495
|
+
return { ord, wd };
|
|
496
|
+
})
|
|
497
|
+
.filter((x) => x !== null);
|
|
498
|
+
// 3) Build a map from weekday → all day‑of‑month numbers
|
|
499
|
+
const year = sample.year;
|
|
500
|
+
const month = sample.month;
|
|
501
|
+
let cursor = Temporal.ZonedDateTime.from({
|
|
502
|
+
year,
|
|
503
|
+
month,
|
|
504
|
+
day: 1,
|
|
505
|
+
hour: sample.hour,
|
|
506
|
+
minute: sample.minute,
|
|
507
|
+
second: sample.second,
|
|
508
|
+
timeZone: tzid,
|
|
509
|
+
});
|
|
510
|
+
const weekdayBuckets = {};
|
|
511
|
+
while (cursor.month === month) {
|
|
512
|
+
const dow = cursor.dayOfWeek;
|
|
513
|
+
weekdayBuckets[dow] = weekdayBuckets[dow] || [];
|
|
514
|
+
weekdayBuckets[dow].push(cursor.day);
|
|
515
|
+
cursor = cursor.add({ days: 1 });
|
|
516
|
+
}
|
|
517
|
+
// 4) For each token, pick the right day‑of‑month
|
|
518
|
+
const results = [];
|
|
519
|
+
for (const { ord, wd } of tokens) {
|
|
520
|
+
const days = weekdayBuckets[wd] || [];
|
|
521
|
+
if (!days.length)
|
|
522
|
+
continue;
|
|
523
|
+
if (ord === 0) {
|
|
524
|
+
// simple weekday: include *all* Fridays, e.g.
|
|
525
|
+
for (const d of days) {
|
|
526
|
+
results.push(sample.with({ day: d }));
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
// ordinal: 2FR → days[1], -1FR → days[days.length-1], etc.
|
|
531
|
+
const idx = ord > 0 ? ord - 1 : days.length + ord;
|
|
532
|
+
const dayNum = days[idx];
|
|
533
|
+
if (dayNum) {
|
|
534
|
+
results.push(sample.with({ day: dayNum }));
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
// 5) Apply your BYHOUR/BYMINUTE overrides and sort chronologically
|
|
539
|
+
return results
|
|
540
|
+
.map((zdt) => this.applyTimeOverride(zdt))
|
|
541
|
+
.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
542
|
+
}
|
|
256
543
|
}
|
|
@@ -133,3 +133,64 @@ 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
|
+
});
|