rrule-temporal 0.3.1 → 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/dist/index.cjs CHANGED
@@ -24,86 +24,79 @@ __export(index_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(index_exports);
26
26
  var import_polyfill = require("@js-temporal/polyfill");
27
- var WEEKDAY_NAMES = [
28
- "Monday",
29
- "Tuesday",
30
- "Wednesday",
31
- "Thursday",
32
- "Friday",
33
- "Saturday",
34
- "Sunday"
35
- ];
36
- var MONTH_NAMES = [
37
- "January",
38
- "February",
39
- "March",
40
- "April",
41
- "May",
42
- "June",
43
- "July",
44
- "August",
45
- "September",
46
- "October",
47
- "November",
48
- "December"
49
- ];
50
- var defaultDateFormatter = (year, month, day) => `${month} ${day}, ${year}`;
51
- function ordinal(n) {
52
- const abs = Math.abs(n);
53
- const suffix = abs % 10 === 1 && abs % 100 !== 11 ? "st" : abs % 10 === 2 && abs % 100 !== 12 ? "nd" : abs % 10 === 3 && abs % 100 !== 13 ? "rd" : "th";
54
- return n < 0 ? `last` : `${abs}${suffix}`;
27
+ function unfoldLine(foldedLine) {
28
+ return foldedLine.replace(/\r?\n[ \t]/g, "");
55
29
  }
56
- function list(arr, mapFn = (x) => `${x}`, final = "and") {
57
- const mapped = arr.map(mapFn);
58
- if (mapped.length === 1) return mapped[0];
59
- return mapped.slice(0, -1).join(", ") + ` ${final} ` + mapped[mapped.length - 1];
60
- }
61
- function formatByDayToken(tok) {
62
- if (typeof tok === "number") return tok.toString();
63
- const m = tok.match(/^([+-]?\d+)?(MO|TU|WE|TH|FR|SA|SU)$/);
64
- if (!m) return tok;
65
- const ord = m[1] ? parseInt(m[1], 10) : 0;
66
- const weekdayMap = {
67
- MO: 0,
68
- TU: 1,
69
- WE: 2,
70
- TH: 3,
71
- FR: 4,
72
- SA: 5,
73
- SU: 6
74
- };
75
- const idx = weekdayMap[m[2]];
76
- const name = WEEKDAY_NAMES[idx];
77
- if (ord === 0) return name;
78
- if (ord === -1) return `last ${name}`;
79
- return `${ordinal(ord)} ${name}`;
30
+ function parseDateValues(dateValues, tzid) {
31
+ const dates = [];
32
+ for (const dateValue of dateValues) {
33
+ if (/Z$/.test(dateValue)) {
34
+ const iso = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9, 15)}Z`;
35
+ dates.push(import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(
36
+ tzid || "UTC"
37
+ ));
38
+ } else {
39
+ const isoDate = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9)}`;
40
+ dates.push(import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid));
41
+ }
42
+ }
43
+ return dates;
80
44
  }
81
45
  function parseRRuleString(input, fallbackDtstart) {
46
+ const unfoldedInput = unfoldLine(input);
82
47
  let dtstart;
83
48
  let tzid = "UTC";
84
49
  let rruleLine;
85
- if (/^DTSTART/m.test(input)) {
86
- const [dtLine, rrLine] = input.split(/\r?\n/);
87
- const m = dtLine?.match(/DTSTART(?:;TZID=([^:]+))?:(\d{8}T\d{6})/);
50
+ let exDate = [];
51
+ let rDate = [];
52
+ if (/^DTSTART/mi.test(unfoldedInput)) {
53
+ const lines = unfoldedInput.split(/\r?\n/);
54
+ const dtLine = lines[0];
55
+ const rrLine = lines.find((line) => line.match(/^RRULE:/i));
56
+ const exLines = lines.filter((line) => line.match(/^EXDATE/i));
57
+ const rLines = lines.filter((line) => line.match(/^RDATE/i));
58
+ const m = dtLine?.match(/DTSTART(?:;TZID=([^:]+))?:(\d{8}T\d{6})/i);
88
59
  if (!m) throw new Error("Invalid DTSTART in ICS snippet");
89
60
  tzid = m[1] || tzid;
90
61
  const isoDate = `${m[2].slice(0, 4)}-${m[2].slice(4, 6)}-${m[2].slice(6, 8)}T${m[2].slice(9)}`;
91
62
  dtstart = import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid);
92
63
  rruleLine = rrLine;
64
+ for (const exLine of exLines) {
65
+ const exMatch = exLine.match(/EXDATE(?:;TZID=([^:]+))?:(.+)/i);
66
+ if (exMatch) {
67
+ const exTzid = exMatch[1] || tzid;
68
+ const dateValues = exMatch[2].split(",");
69
+ exDate.push(...parseDateValues(dateValues, exTzid));
70
+ }
71
+ }
72
+ for (const rLine of rLines) {
73
+ const rMatch = rLine.match(/RDATE(?:;TZID=([^:]+))?:(.+)/i);
74
+ if (rMatch) {
75
+ const rTzid = rMatch[1] || tzid;
76
+ const dateValues = rMatch[2].split(",");
77
+ rDate.push(...parseDateValues(dateValues, rTzid));
78
+ }
79
+ }
93
80
  } else {
94
81
  if (!fallbackDtstart)
95
82
  throw new Error("dtstart required when parsing RRULE alone");
96
83
  dtstart = fallbackDtstart;
97
84
  tzid = fallbackDtstart.timeZoneId;
98
- rruleLine = input.replace(/^RRULE:/, "RRULE:");
85
+ rruleLine = unfoldedInput.replace(/^RRULE:/i, "RRULE:");
99
86
  }
100
- const parts = rruleLine.replace(/^RRULE:/, "").split(";");
101
- const opts = { dtstart, tzid };
87
+ const parts = rruleLine ? rruleLine.replace(/^RRULE:/i, "").split(";") : [];
88
+ const opts = {
89
+ dtstart,
90
+ tzid,
91
+ exDate: exDate.length > 0 ? exDate : void 0,
92
+ rDate: rDate.length > 0 ? rDate : void 0
93
+ };
102
94
  for (const part of parts) {
103
95
  const [key, val] = part.split("=");
104
- switch (key) {
96
+ if (!key) continue;
97
+ switch (key.toUpperCase()) {
105
98
  case "FREQ":
106
- opts.freq = val;
99
+ opts.freq = val.toUpperCase();
107
100
  break;
108
101
  case "INTERVAL":
109
102
  opts.interval = parseInt(val, 10);
@@ -159,31 +152,16 @@ function parseRRuleString(input, fallbackDtstart) {
159
152
  }
160
153
  return opts;
161
154
  }
162
- function formatTime(hour, minute = 0) {
163
- const hr12 = (hour + 11) % 12 + 1;
164
- const ampm = hour < 12 ? "AM" : "PM";
165
- if (minute) {
166
- return `${hr12}:${String(minute).padStart(2, "0")} ${ampm}`;
167
- }
168
- return `${hr12} ${ampm}`;
169
- }
170
- function tzAbbreviation(zdt) {
171
- const parts = new Intl.DateTimeFormat("en-US", {
172
- timeZone: zdt.timeZoneId,
173
- timeZoneName: "short",
174
- hour: "numeric"
175
- }).formatToParts(new Date(zdt.epochMilliseconds));
176
- const tzPart = parts.find((p) => p.type === "timeZoneName");
177
- return tzPart?.value || zdt.timeZoneId;
178
- }
179
155
  var RRuleTemporal = class {
180
156
  tzid;
181
157
  originalDtstart;
182
158
  opts;
159
+ maxIterations;
160
+ includeDtstart;
183
161
  constructor(params) {
184
162
  let manual;
185
163
  if ("rruleString" in params) {
186
- manual = parseRRuleString(params.rruleString);
164
+ manual = { ...params, ...parseRRuleString(params.rruleString) };
187
165
  this.tzid = manual.tzid || "UTC";
188
166
  this.originalDtstart = manual.dtstart;
189
167
  } else {
@@ -204,6 +182,8 @@ var RRuleTemporal = class {
204
182
  throw new Error("Manual until must be a ZonedDateTime");
205
183
  }
206
184
  this.opts = this.sanitizeOpts(manual);
185
+ this.maxIterations = manual.maxIterations ?? 1e4;
186
+ this.includeDtstart = manual.includeDtstart ?? false;
207
187
  }
208
188
  sanitizeOpts(opts) {
209
189
  const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
@@ -492,11 +472,25 @@ var RRuleTemporal = class {
492
472
  throw new Error("all() requires iterator when no COUNT/UNTIL");
493
473
  }
494
474
  const dates = [];
475
+ let iterationCount = 0;
495
476
  if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
496
477
  const start = this.originalDtstart;
497
478
  let monthCursor = start.with({ day: 1 });
498
479
  let matchCount2 = 0;
480
+ if (this.includeDtstart && !this.matchesAll(start)) {
481
+ if (iterator && !iterator(start, matchCount2)) {
482
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
483
+ }
484
+ dates.push(start);
485
+ matchCount2++;
486
+ if (this.shouldBreakForCountLimit(matchCount2)) {
487
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
488
+ }
489
+ }
499
490
  outer_month: while (true) {
491
+ if (++iterationCount > this.maxIterations) {
492
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
493
+ }
500
494
  const occs = this.generateMonthlyOccurrences(monthCursor);
501
495
  if (monthCursor.month === start.month && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) < 0)) {
502
496
  monthCursor = monthCursor.add({ months: this.opts.interval });
@@ -522,6 +516,15 @@ var RRuleTemporal = class {
522
516
  }
523
517
  if (this.opts.freq === "WEEKLY") {
524
518
  const start = this.originalDtstart;
519
+ if (this.includeDtstart && !this.matchesAll(start)) {
520
+ if (iterator && !iterator(start, 0)) {
521
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
522
+ }
523
+ dates.push(start);
524
+ if (this.shouldBreakForCountLimit(1)) {
525
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
526
+ }
527
+ }
525
528
  const dayMap = {
526
529
  MO: 1,
527
530
  TU: 2,
@@ -545,6 +548,9 @@ var RRuleTemporal = class {
545
548
  let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
546
549
  let matchCount2 = 0;
547
550
  outer_week: while (true) {
551
+ if (++iterationCount > this.maxIterations) {
552
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
553
+ }
548
554
  const occs = dows.flatMap((dw) => {
549
555
  const delta = (dw - wkstDay + 7) % 7;
550
556
  const sameDate = weekCursor.add({ days: delta });
@@ -568,12 +574,71 @@ var RRuleTemporal = class {
568
574
  }
569
575
  return this.applyCountLimitAndMergeRDates(dates, iterator);
570
576
  }
577
+ if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
578
+ const start = this.originalDtstart;
579
+ if (this.includeDtstart && !this.matchesAll(start)) {
580
+ if (iterator && !iterator(start, 0)) {
581
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
582
+ }
583
+ dates.push(start);
584
+ if (this.shouldBreakForCountLimit(1)) {
585
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
586
+ }
587
+ }
588
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
589
+ let monthOffset = 0;
590
+ let matchCount2 = dates.length;
591
+ let startMonthIndex = months.findIndex((m) => m >= start.month);
592
+ if (startMonthIndex === -1) {
593
+ startMonthIndex = 0;
594
+ monthOffset = 1;
595
+ }
596
+ while (true) {
597
+ if (++iterationCount > this.maxIterations) {
598
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
599
+ }
600
+ const monthIndex = startMonthIndex + monthOffset;
601
+ const targetMonth = months[monthIndex % months.length];
602
+ const yearsToAdd = Math.floor(monthIndex / months.length);
603
+ const candidate = start.with({
604
+ year: start.year + yearsToAdd,
605
+ month: targetMonth
606
+ });
607
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(candidate, this.opts.until) > 0) {
608
+ break;
609
+ }
610
+ if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
611
+ if (iterator && !iterator(candidate, matchCount2)) {
612
+ break;
613
+ }
614
+ dates.push(candidate);
615
+ matchCount2++;
616
+ if (this.shouldBreakForCountLimit(matchCount2)) {
617
+ break;
618
+ }
619
+ }
620
+ monthOffset++;
621
+ }
622
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
623
+ }
571
624
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
572
625
  const start = this.originalDtstart;
626
+ if (this.includeDtstart && !this.matchesAll(start)) {
627
+ if (iterator && !iterator(start, 0)) {
628
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
629
+ }
630
+ dates.push(start);
631
+ if (this.shouldBreakForCountLimit(1)) {
632
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
633
+ }
634
+ }
573
635
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
574
636
  let yearOffset = 0;
575
637
  let matchCount2 = 0;
576
638
  while (true) {
639
+ if (++iterationCount > this.maxIterations) {
640
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
641
+ }
577
642
  const year = start.year + yearOffset * this.opts.interval;
578
643
  for (const month of months) {
579
644
  let occ = start.with({ year, month });
@@ -599,9 +664,21 @@ var RRuleTemporal = class {
599
664
  }
600
665
  if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
601
666
  const start = this.originalDtstart;
667
+ if (this.includeDtstart && !this.matchesAll(start)) {
668
+ if (iterator && !iterator(start, 0)) {
669
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
670
+ }
671
+ dates.push(start);
672
+ if (this.shouldBreakForCountLimit(1)) {
673
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
674
+ }
675
+ }
602
676
  let yearCursor = start.with({ month: 1, day: 1 });
603
677
  let matchCount2 = 0;
604
678
  outer_year: while (true) {
679
+ if (++iterationCount > this.maxIterations) {
680
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
681
+ }
605
682
  const occs = this.generateYearlyOccurrences(yearCursor);
606
683
  for (const occ of occs) {
607
684
  if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
@@ -623,9 +700,21 @@ var RRuleTemporal = class {
623
700
  }
624
701
  if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
625
702
  const start = this.originalDtstart;
703
+ if (this.includeDtstart && !this.matchesAll(start)) {
704
+ if (iterator && !iterator(start, 0)) {
705
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
706
+ }
707
+ dates.push(start);
708
+ if (this.shouldBreakForCountLimit(1)) {
709
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
710
+ }
711
+ }
626
712
  let yearCursor = start.with({ month: 1, day: 1 });
627
713
  let matchCount2 = 0;
628
714
  outer_year2: while (true) {
715
+ if (++iterationCount > this.maxIterations) {
716
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
717
+ }
629
718
  const occs = this.generateYearlyOccurrences(yearCursor);
630
719
  for (const occ of occs) {
631
720
  if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
@@ -647,7 +736,20 @@ var RRuleTemporal = class {
647
736
  }
648
737
  let current = this.computeFirst();
649
738
  let matchCount = 0;
739
+ if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
740
+ if (iterator && !iterator(this.originalDtstart, matchCount)) {
741
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
742
+ }
743
+ dates.push(this.originalDtstart);
744
+ matchCount++;
745
+ if (this.shouldBreakForCountLimit(matchCount)) {
746
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
747
+ }
748
+ }
650
749
  while (true) {
750
+ if (++iterationCount > this.maxIterations) {
751
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
752
+ }
651
753
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
652
754
  break;
653
755
  }
@@ -672,10 +774,7 @@ var RRuleTemporal = class {
672
774
  */
673
775
  mergeAndDeduplicateRDates(dates) {
674
776
  if (!this.opts.rDate) return dates;
675
- const extras = this.opts.rDate.map(
676
- (d) => d instanceof import_polyfill.Temporal.ZonedDateTime ? d : import_polyfill.Temporal.Instant.from(d.toISOString()).toZonedDateTimeISO(this.tzid)
677
- );
678
- dates.push(...extras);
777
+ dates.push(...this.opts.rDate);
679
778
  dates.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
680
779
  const dedup = [];
681
780
  for (const d of dates) {
@@ -685,6 +784,19 @@ var RRuleTemporal = class {
685
784
  }
686
785
  return dedup;
687
786
  }
787
+ /**
788
+ * Excludes exDate entries from the given array of dates.
789
+ * @param dates - Array of dates to filter
790
+ * @returns Filtered array with exDate entries removed
791
+ */
792
+ excludeExDates(dates) {
793
+ if (!this.opts.exDate || this.opts.exDate.length === 0) return dates;
794
+ return dates.filter((date) => {
795
+ return !this.opts.exDate.some(
796
+ (exDate) => import_polyfill.Temporal.ZonedDateTime.compare(date, exDate) === 0
797
+ );
798
+ });
799
+ }
688
800
  /**
689
801
  * Applies count limit and merges rDates with the rule-generated dates.
690
802
  * @param dates - Array of dates generated by the rule
@@ -693,10 +805,11 @@ var RRuleTemporal = class {
693
805
  */
694
806
  applyCountLimitAndMergeRDates(dates, iterator) {
695
807
  const merged = this.mergeAndDeduplicateRDates(dates);
808
+ const excluded = this.excludeExDates(merged);
696
809
  if (this.opts.count !== void 0) {
697
810
  let finalCount = 0;
698
811
  const finalDates = [];
699
- for (const d of merged) {
812
+ for (const d of excluded) {
700
813
  if (finalCount >= this.opts.count) break;
701
814
  if (iterator && !iterator(d, finalCount)) break;
702
815
  finalDates.push(d);
@@ -704,7 +817,7 @@ var RRuleTemporal = class {
704
817
  }
705
818
  return finalDates;
706
819
  }
707
- return merged;
820
+ return excluded;
708
821
  }
709
822
  /**
710
823
  * Checks if the count limit should break the loop based on rDate presence.
@@ -716,7 +829,10 @@ var RRuleTemporal = class {
716
829
  if (!this.opts.rDate) {
717
830
  return matchCount >= this.opts.count;
718
831
  }
719
- return matchCount >= this.opts.count * 2;
832
+ const rDateCount = this.opts.rDate.length;
833
+ const targetRuleCount = Math.max(this.opts.count - rDateCount, 0);
834
+ const safetyMargin = Math.min(targetRuleCount, 10);
835
+ return matchCount >= targetRuleCount + safetyMargin;
720
836
  }
721
837
  /**
722
838
  * Returns all occurrences of the rule within a specified time window.
@@ -729,9 +845,13 @@ var RRuleTemporal = class {
729
845
  const startInst = after instanceof Date ? import_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
730
846
  const endInst = before instanceof Date ? import_polyfill.Temporal.Instant.from(before.toISOString()) : before.toInstant();
731
847
  const results = [];
848
+ let iterationCount = 0;
732
849
  if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
733
850
  let monthCursor = this.computeFirst().with({ day: 1 });
734
851
  outer: while (true) {
852
+ if (++iterationCount > this.maxIterations) {
853
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
854
+ }
735
855
  const occs = this.generateMonthlyOccurrences(monthCursor);
736
856
  for (const occ of occs) {
737
857
  const inst = occ.toInstant();
@@ -752,6 +872,9 @@ var RRuleTemporal = class {
752
872
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
753
873
  let yearOffset = 0;
754
874
  outer: while (true) {
875
+ if (++iterationCount > this.maxIterations) {
876
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
877
+ }
755
878
  const year = start.year + yearOffset * this.opts.interval;
756
879
  for (const month of months) {
757
880
  let occ = start.with({ year, month });
@@ -773,6 +896,9 @@ var RRuleTemporal = class {
773
896
  const start = this.originalDtstart;
774
897
  let yearCursor = start.with({ month: 1, day: 1 });
775
898
  outer_year: while (true) {
899
+ if (++iterationCount > this.maxIterations) {
900
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
901
+ }
776
902
  const occs = this.generateYearlyOccurrences(yearCursor);
777
903
  for (const occ of occs) {
778
904
  const inst = occ.toInstant();
@@ -792,6 +918,9 @@ var RRuleTemporal = class {
792
918
  const start = this.originalDtstart;
793
919
  let yearCursor = start.with({ month: 1, day: 1 });
794
920
  outer_year2: while (true) {
921
+ if (++iterationCount > this.maxIterations) {
922
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
923
+ }
795
924
  const occs = this.generateYearlyOccurrences(yearCursor);
796
925
  for (const occ of occs) {
797
926
  const inst = occ.toInstant();
@@ -813,6 +942,9 @@ var RRuleTemporal = class {
813
942
  let weekCursor = this.computeFirst();
814
943
  const results2 = [];
815
944
  outer: while (true) {
945
+ if (++iterationCount > this.maxIterations) {
946
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
947
+ }
816
948
  const occs = this.generateWeeklyOccurrences(weekCursor);
817
949
  for (const occ of occs) {
818
950
  const inst = occ.toInstant();
@@ -830,6 +962,9 @@ var RRuleTemporal = class {
830
962
  }
831
963
  let current = this.computeFirst();
832
964
  while (true) {
965
+ if (++iterationCount > this.maxIterations) {
966
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
967
+ }
833
968
  const inst = current.toInstant();
834
969
  if (inc) {
835
970
  if (import_polyfill.Temporal.Instant.compare(inst, endInst) > 0) break;
@@ -911,81 +1046,6 @@ var RRuleTemporal = class {
911
1046
  if (byMonthDay) parts.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
912
1047
  return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
913
1048
  }
914
- /**
915
- * Convert this rule to a human readable English description.
916
- */
917
- toText(formatter = defaultDateFormatter) {
918
- const {
919
- freq,
920
- interval = 1,
921
- count,
922
- until,
923
- byDay,
924
- byHour,
925
- byMinute,
926
- byMonth,
927
- byMonthDay
928
- } = this.opts;
929
- const parts = ["every"];
930
- const base = {
931
- YEARLY: "year",
932
- MONTHLY: "month",
933
- WEEKLY: "week",
934
- DAILY: "day",
935
- HOURLY: "hour",
936
- MINUTELY: "minute",
937
- SECONDLY: "second"
938
- }[freq];
939
- const daysNormalized = byDay?.map((d) => d.toUpperCase());
940
- const isWeekdays = daysNormalized && daysNormalized.length === 5 && ["MO", "TU", "WE", "TH", "FR"].every((d) => daysNormalized.includes(d));
941
- const isEveryday = daysNormalized && daysNormalized.length === 7 && ["MO", "TU", "WE", "TH", "FR", "SA", "SU"].every(
942
- (d) => daysNormalized.includes(d)
943
- );
944
- if (freq === "WEEKLY" && interval === 1 && isWeekdays) {
945
- parts.push("weekday");
946
- } else if (freq === "WEEKLY" && interval === 1 && isEveryday) {
947
- parts.push("day");
948
- } else {
949
- if (interval !== 1) {
950
- parts.push(interval.toString(), base + "s");
951
- } else {
952
- parts.push(base);
953
- }
954
- }
955
- if (freq === "WEEKLY" && byDay && !isWeekdays && !isEveryday) {
956
- parts.push("on", list(byDay, formatByDayToken));
957
- } else if (byDay && freq !== "WEEKLY") {
958
- parts.push("on", list(byDay, formatByDayToken));
959
- }
960
- if (byMonth) {
961
- parts.push(
962
- "in",
963
- list(byMonth, (m) => MONTH_NAMES[m - 1])
964
- );
965
- }
966
- if (byMonthDay) {
967
- parts.push(
968
- "on the",
969
- list(byMonthDay, (d) => ordinal(d)),
970
- "day of the month"
971
- );
972
- }
973
- if (byHour) {
974
- const minutes = byMinute ?? [0];
975
- const times = byHour.flatMap((h) => minutes.map((m) => formatTime(h, m)));
976
- parts.push("at", list(times));
977
- parts.push(tzAbbreviation(this.originalDtstart));
978
- }
979
- if (until) {
980
- parts.push(
981
- "until",
982
- formatter(until.year, MONTH_NAMES[until.month - 1], until.day)
983
- );
984
- } else if (count !== void 0) {
985
- parts.push("for", count.toString(), count === 1 ? "time" : "times");
986
- }
987
- return parts.join(" ");
988
- }
989
1049
  /**
990
1050
  * Given any date in a month, return all the ZonedDateTimes in that month
991
1051
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
@@ -998,7 +1058,10 @@ var RRuleTemporal = class {
998
1058
  if (byMonthDay && byMonthDay.length > 0) {
999
1059
  byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
1000
1060
  }
1001
- if (!byDay && byMonthDayHits.length) {
1061
+ if (!byDay && byMonthDay && byMonthDay.length > 0) {
1062
+ if (byMonthDayHits.length === 0) {
1063
+ return [];
1064
+ }
1002
1065
  const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
1003
1066
  return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1004
1067
  }
@@ -1028,18 +1091,21 @@ var RRuleTemporal = class {
1028
1091
  }
1029
1092
  const byDayHits = [];
1030
1093
  for (const { ord, wd } of tokens) {
1031
- const list2 = buckets[wd] ?? [];
1032
- if (!list2.length) continue;
1094
+ const list = buckets[wd] ?? [];
1095
+ if (!list.length) continue;
1033
1096
  if (ord === 0) {
1034
- for (const d of list2) byDayHits.push(d);
1097
+ for (const d of list) byDayHits.push(d);
1035
1098
  } else {
1036
- const idx = ord > 0 ? ord - 1 : list2.length + ord;
1037
- const dayN = list2[idx];
1099
+ const idx = ord > 0 ? ord - 1 : list.length + ord;
1100
+ const dayN = list[idx];
1038
1101
  if (dayN) byDayHits.push(dayN);
1039
1102
  }
1040
1103
  }
1041
1104
  let finalDays = byDayHits;
1042
- if (byMonthDayHits.length) {
1105
+ if (byMonthDay && byMonthDay.length > 0) {
1106
+ if (byMonthDayHits.length === 0) {
1107
+ return [];
1108
+ }
1043
1109
  finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
1044
1110
  }
1045
1111
  const hits = finalDays.map((d) => sample.with({ day: d }));
@@ -1134,10 +1200,10 @@ var RRuleTemporal = class {
1134
1200
  occs = this.applyBySetPos(occs);
1135
1201
  return occs;
1136
1202
  }
1137
- applyBySetPos(list2) {
1203
+ applyBySetPos(list) {
1138
1204
  const { bySetPos } = this.opts;
1139
- if (!bySetPos || !bySetPos.length) return list2;
1140
- const sorted = [...list2].sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1205
+ if (!bySetPos || !bySetPos.length) return list;
1206
+ const sorted = [...list].sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1141
1207
  const out = [];
1142
1208
  const len = sorted.length;
1143
1209
  for (const pos of bySetPos) {
@@ -1146,25 +1212,22 @@ var RRuleTemporal = class {
1146
1212
  }
1147
1213
  return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1148
1214
  }
1149
- mergeRDates(list2, startInst, endInst, inc) {
1150
- if (!this.opts.rDate) return list2;
1151
- const extras = this.opts.rDate.map(
1152
- (d) => d instanceof import_polyfill.Temporal.ZonedDateTime ? d : import_polyfill.Temporal.Instant.from(d.toISOString()).toZonedDateTimeISO(this.tzid)
1153
- );
1154
- for (const z of extras) {
1215
+ mergeRDates(list, startInst, endInst, inc) {
1216
+ if (!this.opts.rDate) return this.excludeExDates(list);
1217
+ for (const z of this.opts.rDate) {
1155
1218
  const inst = z.toInstant();
1156
1219
  const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
1157
1220
  const endOk = inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) <= 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) < 0;
1158
- if (startOk && endOk) list2.push(z);
1221
+ if (startOk && endOk) list.push(z);
1159
1222
  }
1160
- list2.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1223
+ list.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1161
1224
  const dedup = [];
1162
- for (const d of list2) {
1225
+ for (const d of list) {
1163
1226
  if (dedup.length === 0 || import_polyfill.Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
1164
1227
  dedup.push(d);
1165
1228
  }
1166
1229
  }
1167
- return dedup;
1230
+ return this.excludeExDates(dedup);
1168
1231
  }
1169
1232
  };
1170
1233
  // Annotate the CommonJS export names for ESM import in node: