rrule-temporal 0.1.5 → 0.1.6

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 CHANGED
@@ -14,7 +14,40 @@ npm install rrule-temporal
14
14
 
15
15
  ## Usage
16
16
 
17
+ ## 1. Parsing an ICS snippet
18
+
19
+ Parse a full DTSTART/RRULE snippet and reproduce it or enumerate all occurrences:
20
+
21
+ ```typescript
22
+ // Daily at 17:00 America/Chicago, 5 total occurrences
23
+ const ics = `
24
+ DTSTART;TZID=America/Chicago:20250320T170000
25
+ RRULE:FREQ=DAILY;BYHOUR=17;BYMINUTE=0;COUNT=5
26
+ `.trim();
27
+
28
+ const rule = new RRuleTemporal({ rruleString: ics });
29
+
30
+ // toString() outputs the original DTSTART and RRULE line:
31
+ console.log(rule.toString());
32
+ /*
33
+ DTSTART;TZID=America/Chicago:20250320T170000
34
+ RRULE:FREQ=DAILY;BYHOUR=17;BYMINUTE=0;COUNT=5
35
+ */
36
+
37
+ // all() returns an array of Temporal.ZonedDateTime:
38
+ const dates = rule.all();
39
+ dates.forEach(dt => console.log(dt.toString()));
40
+ // [
41
+ // "2025-03-20T17:00:00[America/Chicago]",
42
+ // "2025-03-21T17:00:00[America/Chicago]",
43
+ // // … total of 5 days
44
+ // ]
45
+ ```
46
+
47
+ ## 2. Enumerate occurrences in a time window
48
+
17
49
  ```typescript
50
+
18
51
  const ics = `DTSTART;TZID=America/Chicago:20250401T000000
19
52
  RRULE:FREQ=DAILY;BYHOUR=0;BYMINUTE=0;UNTIL=20250405T000000Z`.trim();
20
53
  const rule = new RRuleTemporal({ rruleString: ics });
@@ -32,4 +65,139 @@ RRULE:FREQ=DAILY;BYHOUR=0;BYMINUTE=0;UNTIL=20250405T000000Z`.trim();
32
65
  const arrInc = rule.between(start, end, true);
33
66
  expect(arrInc.map((d) => d.day)).toEqual([2, 3, 4]);
34
67
  });
35
- ```
68
+ ```
69
+
70
+ ## 3. Manual options
71
+
72
+ Build a rule from explicit options, overriding start time and interval:
73
+
74
+ ```typescript
75
+ // Every 2 days at 09:15 America/Chicago, 3 occurrences, starting 2025-04-20 08:30 CT
76
+ const dtstart = Temporal.ZonedDateTime.from({
77
+ year: 2025, month: 4, day: 20,
78
+ hour: 8, minute: 30,
79
+ timeZone: "America/Chicago"
80
+ });
81
+
82
+ const rule = new RRuleTemporal({
83
+ freq: "DAILY",
84
+ interval: 2,
85
+ count: 3,
86
+ byHour: [9],
87
+ byMinute: [15],
88
+ dtstart,
89
+ tzid: "America/Chicago",
90
+ });
91
+
92
+ rule.all().forEach((dt, i) => {
93
+ console.log(i, dt.toString());
94
+ });
95
+ // 0 "2025-04-20T09:15:00[America/Chicago]"
96
+ // 1 "2025-04-22T09:15:00[America/Chicago]"
97
+ // 2 "2025-04-24T09:15:00[America/Chicago]"
98
+ ```
99
+
100
+ ## 4. Windowed queries with between()
101
+
102
+ Fetch occurrences in a given time window, with inclusive/exclusive end:
103
+ ```typescript
104
+ // Daily at 00:00 CT until 2025-04-05T00:00Z inclusive
105
+ const ics2 = `
106
+ DTSTART;TZID=America/Chicago:20250401T000000
107
+ RRULE:FREQ=DAILY;BYHOUR=0;BYMINUTE=0;UNTIL=20250405T000000Z
108
+ `.trim();
109
+
110
+ const rule2 = new RRuleTemporal({ rruleString: ics2 });
111
+
112
+ // Define UTC window: from 2025-04-02 00:00 UTC through 2025-04-04 05:00 UTC
113
+ const start = new Date(Date.UTC(2025, 3, 2, 0, 0));
114
+ const end = new Date(Date.UTC(2025, 3, 4, 5, 0));
115
+
116
+ console.log(rule2.between(start, end, false).map(d => d.toString()));
117
+ // [
118
+ // "2025-04-02T00:00:00[America/Chicago]",
119
+ // "2025-04-03T00:00:00[America/Chicago]"
120
+ // ]
121
+
122
+ console.log(rule2.between(start, end, true).map(d => d.toString()));
123
+ // adds "2025-04-04T00:00:00[America/Chicago]"
124
+ ```
125
+
126
+ ## 5. Next/previous occurrence
127
+
128
+ Find the next or previous occurrence relative to any date:
129
+
130
+ ```typescript
131
+ // Monthly at 12:00 America/Chicago, 12 total
132
+ const ics3 = `
133
+ DTSTART;TZID=America/Chicago:20250101T120000
134
+ RRULE:FREQ=MONTHLY;BYHOUR=12;BYMINUTE=0;COUNT=12
135
+ `.trim();
136
+
137
+ const rule3 = new RRuleTemporal({ rruleString: ics3 });
138
+
139
+ // Next occurrence after the current time in Central Time
140
+ // Where hour and minute conditions are met
141
+ const firstOccurrence = rule.next(
142
+ Temporal.Now.zonedDateTimeISO("America/Chicago")
143
+ );
144
+
145
+ // Next after 2025-03-15T00:00:00Z
146
+ const nxt = rule3.next(new Date("2025-03-15T00:00:00Z"));
147
+ console.log(nxt?.toString()); // "2025-04-01T12:00:00[UTC]"
148
+
149
+ // Previous on or before 2025-06-05T00:00:00Z
150
+ const prev = rule3.previous(new Date("2025-06-05T00:00:00Z"), true);
151
+ console.log(prev?.toString()); // "2025-06-01T12:00:00[UTC]"
152
+ ```
153
+
154
+ ## 6. toString()
155
+
156
+ Convert the rule back to an ICS string:
157
+
158
+ ```typescript
159
+ console.log(rule.toString());
160
+ // DTSTART;TZID=America/Chicago:20250320T170000
161
+ // RRULE:FREQ=DAILY;BYHOUR=17;BYMINUTE=0;COUNT=5
162
+ ```
163
+
164
+ ## 7. BYDAY/BYMONTH examples:
165
+
166
+ ```typescript
167
+ // 2nd & 4th Fridays each month at 00:00 CT, first 6 occurrences
168
+ const ics4 = `
169
+ DTSTART;TZID=America/Chicago:20250325T000000
170
+ RRULE:FREQ=MONTHLY;BYDAY=2FR,4FR;BYHOUR=0;BYMINUTE=0;COUNT=6
171
+ `.trim();
172
+
173
+ const rule4 = new RRuleTemporal({ rruleString: ics4 });
174
+ rule4.all().forEach(dt => console.log(dt.toString()));
175
+ // [
176
+ // "2025-04-11T00:00:00[America/Chicago]",
177
+ // "2025-04-25T00:00:00[America/Chicago]",
178
+ // "2025-05-09T00:00:00[America/Chicago]",
179
+ // …
180
+ ]
181
+
182
+ // Yearly rotated through Jan, Jun, Dec at 09:00 UTC, 4 occurrences
183
+ const dtstart5 = Temporal.ZonedDateTime.from({
184
+ year: 2025, month: 1, day: 10, hour: 9, minute: 0, timeZone: "UTC"
185
+ });
186
+ const rule5 = new RRuleTemporal({
187
+ freq: "YEARLY",
188
+ interval: 1,
189
+ count: 4,
190
+ byMonth: [1, 6, 12],
191
+ byHour: [9],
192
+ byMinute: [0],
193
+ dtstart: dtstart5
194
+ });
195
+ rule5.all().forEach(dt => console.log(dt.toString()));
196
+ // [
197
+ // "2025-01-10T09:00:00[UTC]",
198
+ // "2026-06-10T09:00:00[UTC]",
199
+ // "2027-12-10T09:00:00[UTC]",
200
+ // "2028-01-10T09:00:00[UTC]"
201
+ // ]
202
+ ```
203
+
package/dist/index.cjs CHANGED
@@ -26,7 +26,7 @@ module.exports = __toCommonJS(index_exports);
26
26
  var import_polyfill = require("@js-temporal/polyfill");
27
27
  function parseRRuleString(input, fallbackDtstart) {
28
28
  let dtstart;
29
- let tzid = "";
29
+ let tzid = "UTC";
30
30
  let rruleLine;
31
31
  if (/^DTSTART/m.test(input)) {
32
32
  const [dtLine, rrLine] = input.split(/\r?\n/);
@@ -72,10 +72,10 @@ function parseRRuleString(input, fallbackDtstart) {
72
72
  break;
73
73
  }
74
74
  case "BYHOUR":
75
- opts.byHour = val.split(",").map((n) => parseInt(n, 10));
75
+ opts.byHour = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
76
76
  break;
77
77
  case "BYMINUTE":
78
- opts.byMinute = val.split(",").map((n) => parseInt(n, 10));
78
+ opts.byMinute = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
79
79
  break;
80
80
  case "BYDAY":
81
81
  opts.byDay = val.split(",");
@@ -131,6 +131,34 @@ var RRuleTemporal = class {
131
131
  throw new Error(`Unsupported FREQ: ${freq}`);
132
132
  }
133
133
  }
134
+ /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE combinations,
135
+ * keeping chronological order. If BYHOUR/BYMINUTE are not present the
136
+ * original date is returned unchanged.
137
+ */
138
+ expandByHourMinute(base) {
139
+ const hours = this.opts.byHour ?? [base.hour];
140
+ const minutes = this.opts.byMinute ?? [base.minute];
141
+ const out = [];
142
+ for (const h of hours) {
143
+ for (const m of minutes) {
144
+ out.push(base.with({ hour: h, minute: m }));
145
+ }
146
+ }
147
+ return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
148
+ }
149
+ nextCandidateSameDate(zdt) {
150
+ const { byHour, byMinute } = this.opts;
151
+ if (byHour && byHour.length > 1) {
152
+ const idx = byHour.indexOf(zdt.hour);
153
+ if (idx !== -1 && idx < byHour.length - 1) {
154
+ return zdt.with({
155
+ hour: byHour[idx + 1],
156
+ minute: byMinute ? byMinute[0] : zdt.minute
157
+ });
158
+ }
159
+ }
160
+ return this.applyTimeOverride(this.rawAdvance(zdt));
161
+ }
134
162
  applyTimeOverride(zdt) {
135
163
  const { byHour, byMinute } = this.opts;
136
164
  let dt = zdt;
@@ -168,7 +196,6 @@ var RRuleTemporal = class {
168
196
  }
169
197
  // inside class RRuleTemporal:
170
198
  generateWeeklyOccurrences(sample) {
171
- const { byDay } = this.opts;
172
199
  const dayMap = {
173
200
  MO: 1,
174
201
  TU: 2,
@@ -178,11 +205,12 @@ var RRuleTemporal = class {
178
205
  SA: 6,
179
206
  SU: 7
180
207
  };
181
- const tokens = byDay && byDay.length ? byDay : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
182
- const occs = tokens.map((tok) => {
208
+ const tokens = this.opts.byDay?.length ? [...this.opts.byDay] : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
209
+ const occs = tokens.flatMap((tok) => {
183
210
  const targetDow = dayMap[tok];
184
211
  const delta = (targetDow - sample.dayOfWeek + 7) % 7;
185
- return this.applyTimeOverride(sample.add({ days: delta }));
212
+ const sameDate = sample.add({ days: delta });
213
+ return this.expandByHourMinute(sameDate);
186
214
  });
187
215
  return occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
188
216
  }
@@ -232,6 +260,9 @@ var RRuleTemporal = class {
232
260
  matchesAll(zdt) {
233
261
  return this.matchesByDay(zdt) && this.matchesByMonth(zdt);
234
262
  }
263
+ options() {
264
+ return this.opts;
265
+ }
235
266
  /**
236
267
  * Returns all occurrences of the rule.
237
268
  * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
@@ -248,6 +279,10 @@ var RRuleTemporal = class {
248
279
  let matchCount2 = 0;
249
280
  outer_month: while (true) {
250
281
  const occs = this.generateMonthlyOccurrences(monthCursor);
282
+ if (monthCursor.month === start.month && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) < 0)) {
283
+ monthCursor = monthCursor.add({ months: this.opts.interval });
284
+ continue outer_month;
285
+ }
251
286
  for (const occ of occs) {
252
287
  if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
253
288
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
@@ -289,9 +324,10 @@ var RRuleTemporal = class {
289
324
  let matchCount2 = 0;
290
325
  outer_week: while (true) {
291
326
  const baseDow = weekCursor.dayOfWeek;
292
- const occs = dows.map((dw) => {
327
+ const occs = dows.flatMap((dw) => {
293
328
  const delta = dw - baseDow;
294
- return this.applyTimeOverride(weekCursor.add({ days: delta }));
329
+ const sameDate = weekCursor.add({ days: delta });
330
+ return this.expandByHourMinute(sameDate);
295
331
  }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
296
332
  for (const occ of occs) {
297
333
  if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
@@ -354,7 +390,7 @@ var RRuleTemporal = class {
354
390
  dates.push(current);
355
391
  matchCount++;
356
392
  }
357
- current = this.applyTimeOverride(this.rawAdvance(current));
393
+ current = this.nextCandidateSameDate(current);
358
394
  }
359
395
  return dates;
360
396
  }
@@ -437,7 +473,7 @@ var RRuleTemporal = class {
437
473
  if (import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 && this.matchesAll(current)) {
438
474
  results.push(current);
439
475
  }
440
- current = this.applyTimeOverride(this.rawAdvance(current));
476
+ current = this.nextCandidateSameDate(current);
441
477
  }
442
478
  return results;
443
479
  }
@@ -464,7 +500,7 @@ var RRuleTemporal = class {
464
500
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
465
501
  return null;
466
502
  }
467
- current = this.applyTimeOverride(this.rawAdvance(current));
503
+ current = this.nextCandidateSameDate(current);
468
504
  }
469
505
  }
470
506
  /**
@@ -488,7 +524,7 @@ var RRuleTemporal = class {
488
524
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
489
525
  break;
490
526
  }
491
- current = this.applyTimeOverride(this.rawAdvance(current));
527
+ current = this.nextCandidateSameDate(current);
492
528
  }
493
529
  return prev;
494
530
  }
@@ -512,11 +548,9 @@ var RRuleTemporal = class {
512
548
  */
513
549
  generateMonthlyOccurrences(sample) {
514
550
  const { byDay, byMonth, tzid } = this.opts;
515
- if (byMonth && !byMonth.includes(sample.month)) {
516
- return [];
517
- }
551
+ if (byMonth && !byMonth.includes(sample.month)) return [];
518
552
  if (!byDay || byDay.length === 0) {
519
- return [sample];
553
+ return this.expandByHourMinute(sample);
520
554
  }
521
555
  const dayMap = {
522
556
  MO: 1,
@@ -530,45 +564,28 @@ var RRuleTemporal = class {
530
564
  const tokens = byDay.map((tok) => {
531
565
  const m = tok.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
532
566
  if (!m) return null;
533
- const ord = m[1] ? parseInt(m[1], 10) : 0;
534
- const wd = dayMap[m[2]];
535
- return { ord, wd };
567
+ return { ord: m[1] ? parseInt(m[1], 10) : 0, wd: dayMap[m[2]] };
536
568
  }).filter((x) => x !== null);
537
- const year = sample.year;
538
- const month = sample.month;
539
- let cursor = import_polyfill.Temporal.ZonedDateTime.from({
540
- year,
541
- month,
542
- day: 1,
543
- hour: sample.hour,
544
- minute: sample.minute,
545
- second: sample.second,
546
- timeZone: tzid
547
- });
548
- const weekdayBuckets = {};
549
- while (cursor.month === month) {
569
+ const buckets = {};
570
+ let cursor = sample.with({ day: 1 });
571
+ while (cursor.month === sample.month) {
550
572
  const dow = cursor.dayOfWeek;
551
- weekdayBuckets[dow] = weekdayBuckets[dow] || [];
552
- weekdayBuckets[dow].push(cursor.day);
573
+ (buckets[dow] ||= []).push(cursor.day);
553
574
  cursor = cursor.add({ days: 1 });
554
575
  }
555
- const results = [];
576
+ const hits = [];
556
577
  for (const { ord, wd } of tokens) {
557
- const days = weekdayBuckets[wd] || [];
558
- if (!days.length) continue;
578
+ const list = buckets[wd] ?? [];
579
+ if (!list.length) continue;
559
580
  if (ord === 0) {
560
- for (const d of days) {
561
- results.push(sample.with({ day: d }));
562
- }
581
+ for (const d of list) hits.push(sample.with({ day: d }));
563
582
  } else {
564
- const idx = ord > 0 ? ord - 1 : days.length + ord;
565
- const dayNum = days[idx];
566
- if (dayNum) {
567
- results.push(sample.with({ day: dayNum }));
568
- }
583
+ const idx = ord > 0 ? ord - 1 : list.length + ord;
584
+ const dayN = list[idx];
585
+ if (dayN) hits.push(sample.with({ day: dayN }));
569
586
  }
570
587
  }
571
- return results.map((zdt) => this.applyTimeOverride(zdt)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
588
+ return hits.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
572
589
  }
573
590
  };
574
591
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.d.cts CHANGED
@@ -13,7 +13,7 @@ interface ManualOpts extends BaseOpts {
13
13
  byMinute?: number[];
14
14
  byDay?: string[];
15
15
  byMonth?: number[];
16
- dtstart: string | Temporal.ZonedDateTime;
16
+ dtstart: Temporal.ZonedDateTime;
17
17
  }
18
18
  interface IcsOpts extends BaseOpts {
19
19
  rruleString: string;
@@ -27,12 +27,19 @@ declare class RRuleTemporal {
27
27
  rruleString: string;
28
28
  } | ManualOpts);
29
29
  private rawAdvance;
30
+ /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE combinations,
31
+ * keeping chronological order. If BYHOUR/BYMINUTE are not present the
32
+ * original date is returned unchanged.
33
+ */
34
+ private expandByHourMinute;
35
+ private nextCandidateSameDate;
30
36
  private applyTimeOverride;
31
37
  private computeFirst;
32
38
  private generateWeeklyOccurrences;
33
39
  private matchesByDay;
34
40
  private matchesByMonth;
35
41
  private matchesAll;
42
+ options(): ManualOpts;
36
43
  /**
37
44
  * Returns all occurrences of the rule.
38
45
  * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
package/dist/index.d.ts CHANGED
@@ -13,7 +13,7 @@ interface ManualOpts extends BaseOpts {
13
13
  byMinute?: number[];
14
14
  byDay?: string[];
15
15
  byMonth?: number[];
16
- dtstart: string | Temporal.ZonedDateTime;
16
+ dtstart: Temporal.ZonedDateTime;
17
17
  }
18
18
  interface IcsOpts extends BaseOpts {
19
19
  rruleString: string;
@@ -27,12 +27,19 @@ declare class RRuleTemporal {
27
27
  rruleString: string;
28
28
  } | ManualOpts);
29
29
  private rawAdvance;
30
+ /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE combinations,
31
+ * keeping chronological order. If BYHOUR/BYMINUTE are not present the
32
+ * original date is returned unchanged.
33
+ */
34
+ private expandByHourMinute;
35
+ private nextCandidateSameDate;
30
36
  private applyTimeOverride;
31
37
  private computeFirst;
32
38
  private generateWeeklyOccurrences;
33
39
  private matchesByDay;
34
40
  private matchesByMonth;
35
41
  private matchesAll;
42
+ options(): ManualOpts;
36
43
  /**
37
44
  * Returns all occurrences of the rule.
38
45
  * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { Temporal } from "@js-temporal/polyfill";
3
3
  function parseRRuleString(input, fallbackDtstart) {
4
4
  let dtstart;
5
- let tzid = "";
5
+ let tzid = "UTC";
6
6
  let rruleLine;
7
7
  if (/^DTSTART/m.test(input)) {
8
8
  const [dtLine, rrLine] = input.split(/\r?\n/);
@@ -48,10 +48,10 @@ function parseRRuleString(input, fallbackDtstart) {
48
48
  break;
49
49
  }
50
50
  case "BYHOUR":
51
- opts.byHour = val.split(",").map((n) => parseInt(n, 10));
51
+ opts.byHour = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
52
52
  break;
53
53
  case "BYMINUTE":
54
- opts.byMinute = val.split(",").map((n) => parseInt(n, 10));
54
+ opts.byMinute = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
55
55
  break;
56
56
  case "BYDAY":
57
57
  opts.byDay = val.split(",");
@@ -107,6 +107,34 @@ var RRuleTemporal = class {
107
107
  throw new Error(`Unsupported FREQ: ${freq}`);
108
108
  }
109
109
  }
110
+ /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE combinations,
111
+ * keeping chronological order. If BYHOUR/BYMINUTE are not present the
112
+ * original date is returned unchanged.
113
+ */
114
+ expandByHourMinute(base) {
115
+ const hours = this.opts.byHour ?? [base.hour];
116
+ const minutes = this.opts.byMinute ?? [base.minute];
117
+ const out = [];
118
+ for (const h of hours) {
119
+ for (const m of minutes) {
120
+ out.push(base.with({ hour: h, minute: m }));
121
+ }
122
+ }
123
+ return out.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
124
+ }
125
+ nextCandidateSameDate(zdt) {
126
+ const { byHour, byMinute } = this.opts;
127
+ if (byHour && byHour.length > 1) {
128
+ const idx = byHour.indexOf(zdt.hour);
129
+ if (idx !== -1 && idx < byHour.length - 1) {
130
+ return zdt.with({
131
+ hour: byHour[idx + 1],
132
+ minute: byMinute ? byMinute[0] : zdt.minute
133
+ });
134
+ }
135
+ }
136
+ return this.applyTimeOverride(this.rawAdvance(zdt));
137
+ }
110
138
  applyTimeOverride(zdt) {
111
139
  const { byHour, byMinute } = this.opts;
112
140
  let dt = zdt;
@@ -144,7 +172,6 @@ var RRuleTemporal = class {
144
172
  }
145
173
  // inside class RRuleTemporal:
146
174
  generateWeeklyOccurrences(sample) {
147
- const { byDay } = this.opts;
148
175
  const dayMap = {
149
176
  MO: 1,
150
177
  TU: 2,
@@ -154,11 +181,12 @@ var RRuleTemporal = class {
154
181
  SA: 6,
155
182
  SU: 7
156
183
  };
157
- const tokens = byDay && byDay.length ? byDay : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
158
- const occs = tokens.map((tok) => {
184
+ const tokens = this.opts.byDay?.length ? [...this.opts.byDay] : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
185
+ const occs = tokens.flatMap((tok) => {
159
186
  const targetDow = dayMap[tok];
160
187
  const delta = (targetDow - sample.dayOfWeek + 7) % 7;
161
- return this.applyTimeOverride(sample.add({ days: delta }));
188
+ const sameDate = sample.add({ days: delta });
189
+ return this.expandByHourMinute(sameDate);
162
190
  });
163
191
  return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
164
192
  }
@@ -208,6 +236,9 @@ var RRuleTemporal = class {
208
236
  matchesAll(zdt) {
209
237
  return this.matchesByDay(zdt) && this.matchesByMonth(zdt);
210
238
  }
239
+ options() {
240
+ return this.opts;
241
+ }
211
242
  /**
212
243
  * Returns all occurrences of the rule.
213
244
  * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
@@ -224,6 +255,10 @@ var RRuleTemporal = class {
224
255
  let matchCount2 = 0;
225
256
  outer_month: while (true) {
226
257
  const occs = this.generateMonthlyOccurrences(monthCursor);
258
+ if (monthCursor.month === start.month && occs.some((o) => Temporal.ZonedDateTime.compare(o, start) < 0)) {
259
+ monthCursor = monthCursor.add({ months: this.opts.interval });
260
+ continue outer_month;
261
+ }
227
262
  for (const occ of occs) {
228
263
  if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
229
264
  if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
@@ -265,9 +300,10 @@ var RRuleTemporal = class {
265
300
  let matchCount2 = 0;
266
301
  outer_week: while (true) {
267
302
  const baseDow = weekCursor.dayOfWeek;
268
- const occs = dows.map((dw) => {
303
+ const occs = dows.flatMap((dw) => {
269
304
  const delta = dw - baseDow;
270
- return this.applyTimeOverride(weekCursor.add({ days: delta }));
305
+ const sameDate = weekCursor.add({ days: delta });
306
+ return this.expandByHourMinute(sameDate);
271
307
  }).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
272
308
  for (const occ of occs) {
273
309
  if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
@@ -330,7 +366,7 @@ var RRuleTemporal = class {
330
366
  dates.push(current);
331
367
  matchCount++;
332
368
  }
333
- current = this.applyTimeOverride(this.rawAdvance(current));
369
+ current = this.nextCandidateSameDate(current);
334
370
  }
335
371
  return dates;
336
372
  }
@@ -413,7 +449,7 @@ var RRuleTemporal = class {
413
449
  if (Temporal.Instant.compare(inst, startInst) >= 0 && this.matchesAll(current)) {
414
450
  results.push(current);
415
451
  }
416
- current = this.applyTimeOverride(this.rawAdvance(current));
452
+ current = this.nextCandidateSameDate(current);
417
453
  }
418
454
  return results;
419
455
  }
@@ -440,7 +476,7 @@ var RRuleTemporal = class {
440
476
  if (this.opts.until && Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
441
477
  return null;
442
478
  }
443
- current = this.applyTimeOverride(this.rawAdvance(current));
479
+ current = this.nextCandidateSameDate(current);
444
480
  }
445
481
  }
446
482
  /**
@@ -464,7 +500,7 @@ var RRuleTemporal = class {
464
500
  if (this.opts.until && Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
465
501
  break;
466
502
  }
467
- current = this.applyTimeOverride(this.rawAdvance(current));
503
+ current = this.nextCandidateSameDate(current);
468
504
  }
469
505
  return prev;
470
506
  }
@@ -488,11 +524,9 @@ var RRuleTemporal = class {
488
524
  */
489
525
  generateMonthlyOccurrences(sample) {
490
526
  const { byDay, byMonth, tzid } = this.opts;
491
- if (byMonth && !byMonth.includes(sample.month)) {
492
- return [];
493
- }
527
+ if (byMonth && !byMonth.includes(sample.month)) return [];
494
528
  if (!byDay || byDay.length === 0) {
495
- return [sample];
529
+ return this.expandByHourMinute(sample);
496
530
  }
497
531
  const dayMap = {
498
532
  MO: 1,
@@ -506,45 +540,28 @@ var RRuleTemporal = class {
506
540
  const tokens = byDay.map((tok) => {
507
541
  const m = tok.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
508
542
  if (!m) return null;
509
- const ord = m[1] ? parseInt(m[1], 10) : 0;
510
- const wd = dayMap[m[2]];
511
- return { ord, wd };
543
+ return { ord: m[1] ? parseInt(m[1], 10) : 0, wd: dayMap[m[2]] };
512
544
  }).filter((x) => x !== null);
513
- const year = sample.year;
514
- const month = sample.month;
515
- let cursor = Temporal.ZonedDateTime.from({
516
- year,
517
- month,
518
- day: 1,
519
- hour: sample.hour,
520
- minute: sample.minute,
521
- second: sample.second,
522
- timeZone: tzid
523
- });
524
- const weekdayBuckets = {};
525
- while (cursor.month === month) {
545
+ const buckets = {};
546
+ let cursor = sample.with({ day: 1 });
547
+ while (cursor.month === sample.month) {
526
548
  const dow = cursor.dayOfWeek;
527
- weekdayBuckets[dow] = weekdayBuckets[dow] || [];
528
- weekdayBuckets[dow].push(cursor.day);
549
+ (buckets[dow] ||= []).push(cursor.day);
529
550
  cursor = cursor.add({ days: 1 });
530
551
  }
531
- const results = [];
552
+ const hits = [];
532
553
  for (const { ord, wd } of tokens) {
533
- const days = weekdayBuckets[wd] || [];
534
- if (!days.length) continue;
554
+ const list = buckets[wd] ?? [];
555
+ if (!list.length) continue;
535
556
  if (ord === 0) {
536
- for (const d of days) {
537
- results.push(sample.with({ day: d }));
538
- }
557
+ for (const d of list) hits.push(sample.with({ day: d }));
539
558
  } else {
540
- const idx = ord > 0 ? ord - 1 : days.length + ord;
541
- const dayNum = days[idx];
542
- if (dayNum) {
543
- results.push(sample.with({ day: dayNum }));
544
- }
559
+ const idx = ord > 0 ? ord - 1 : list.length + ord;
560
+ const dayN = list[idx];
561
+ if (dayN) hits.push(sample.with({ day: dayN }));
545
562
  }
546
563
  }
547
- return results.map((zdt) => this.applyTimeOverride(zdt)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
564
+ return hits.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
548
565
  }
549
566
  };
550
567
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rrule-temporal",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Recurrence rule processing using Temporal PlainDate/PlainDateTime",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",