rrule-temporal 1.1.3 → 1.1.4

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/totext.cjs CHANGED
@@ -45,25 +45,18 @@ function unfoldLine(foldedLine) {
45
45
  }
46
46
  function parseIcsDateTime(dateStr, tzid, valueType) {
47
47
  const isDate = valueType === "DATE" || !dateStr.includes("T");
48
+ const isoDate = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}`;
48
49
  if (isDate) {
49
- const isoDate = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}`;
50
50
  return import_polyfill.Temporal.PlainDate.from(isoDate).toZonedDateTime({ timeZone: tzid });
51
51
  }
52
- if (/Z$/.test(dateStr)) {
53
- const iso = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}T${dateStr.slice(9, 15)}Z`;
52
+ if (dateStr.endsWith("Z")) {
53
+ const iso = `${isoDate}T${dateStr.slice(9, 15)}Z`;
54
54
  return import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
55
55
  } else {
56
- const isoDate = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}T${dateStr.slice(9)}`;
57
- return import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid);
56
+ const iso = `${isoDate}T${dateStr.slice(9)}`;
57
+ return import_polyfill.Temporal.PlainDateTime.from(iso).toZonedDateTime(tzid);
58
58
  }
59
59
  }
60
- function parseDateValues(dateValues, tzid, valueType) {
61
- const dates = [];
62
- for (const dateValue of dateValues) {
63
- dates.push(parseIcsDateTime(dateValue, tzid, valueType));
64
- }
65
- return dates;
66
- }
67
60
  function parseDateLines(lines, linePrefix, defaultTzid) {
68
61
  const dates = [];
69
62
  const regex = new RegExp(`^${linePrefix}(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)`, "i");
@@ -73,11 +66,18 @@ function parseDateLines(lines, linePrefix, defaultTzid) {
73
66
  const [, valueType, tzid, dateValuesStr] = match;
74
67
  const timezone = tzid || defaultTzid;
75
68
  const dateValues = dateValuesStr.split(",");
76
- dates.push(...parseDateValues(dateValues, timezone, valueType));
69
+ dates.push(...dateValues.map((dateValue) => parseIcsDateTime(dateValue, timezone, valueType)));
77
70
  }
78
71
  }
79
72
  return dates;
80
73
  }
74
+ function parseNumberArray(val, sort = false) {
75
+ const arr = val.split(",").map((n) => parseInt(n, 10));
76
+ if (sort) {
77
+ return arr.sort((a, b) => a - b);
78
+ }
79
+ return arr;
80
+ }
81
81
  function parseRRuleString(input, targetTimezone) {
82
82
  var _a;
83
83
  const unfoldedInput = unfoldLine(input).trim();
@@ -125,37 +125,37 @@ function parseRRuleString(input, targetTimezone) {
125
125
  break;
126
126
  case "UNTIL": {
127
127
  opts.until = parseIcsDateTime(val, tzid || "UTC");
128
- if (!/Z$/.test(val) && tzid !== "UTC") {
128
+ if (!val.endsWith("Z") && tzid !== "UTC") {
129
129
  throw new Error("UNTIL rule part MUST always be specified as a date with UTC time");
130
130
  }
131
131
  break;
132
132
  }
133
133
  case "BYHOUR":
134
- opts.byHour = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
134
+ opts.byHour = parseNumberArray(val, true);
135
135
  break;
136
136
  case "BYMINUTE":
137
- opts.byMinute = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
137
+ opts.byMinute = parseNumberArray(val, true);
138
+ break;
139
+ case "BYSECOND":
140
+ opts.bySecond = parseNumberArray(val, true);
138
141
  break;
139
142
  case "BYDAY":
140
143
  opts.byDay = val.split(",");
141
144
  break;
142
145
  case "BYMONTH":
143
- opts.byMonth = val.split(",").map((n) => parseInt(n, 10));
146
+ opts.byMonth = parseNumberArray(val);
144
147
  break;
145
148
  case "BYMONTHDAY":
146
- opts.byMonthDay = val.split(",").map((n) => parseInt(n, 10));
147
- break;
148
- case "BYSECOND":
149
- opts.bySecond = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
149
+ opts.byMonthDay = parseNumberArray(val);
150
150
  break;
151
151
  case "BYYEARDAY":
152
- opts.byYearDay = val.split(",").map((n) => parseInt(n, 10));
152
+ opts.byYearDay = parseNumberArray(val);
153
153
  break;
154
154
  case "BYWEEKNO":
155
- opts.byWeekNo = val.split(",").map((n) => parseInt(n, 10));
155
+ opts.byWeekNo = parseNumberArray(val);
156
156
  break;
157
157
  case "BYSETPOS":
158
- opts.bySetPos = val.split(",").map((n) => parseInt(n, 10));
158
+ opts.bySetPos = parseNumberArray(val);
159
159
  break;
160
160
  case "WKST":
161
161
  opts.wkst = val;
@@ -200,12 +200,26 @@ var RRuleTemporal = class _RRuleTemporal {
200
200
  if (sanitized.length === 0) return void 0;
201
201
  return sort ? sanitized.sort((a, b) => a - b) : sanitized;
202
202
  }
203
- sanitizeOpts(opts) {
203
+ sanitizeByDay(byDay) {
204
204
  const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
205
- if (opts.byDay) {
206
- opts.byDay = opts.byDay.filter((d) => validDay.test(d));
207
- if (opts.byDay.length === 0) delete opts.byDay;
205
+ const days = (byDay != null ? byDay : []).filter((day) => day && typeof day === "string");
206
+ for (const day of days) {
207
+ const match = day.match(validDay);
208
+ if (!match) {
209
+ throw new Error(`Invalid BYDAY value: ${day}`);
210
+ }
211
+ const ord = match[1];
212
+ if (ord) {
213
+ const ordInt = parseInt(ord, 10);
214
+ if (ordInt === 0) {
215
+ throw new Error(`Invalid BYDAY value: ${day}`);
216
+ }
217
+ }
208
218
  }
219
+ return days.length > 0 ? days : void 0;
220
+ }
221
+ sanitizeOpts(opts) {
222
+ opts.byDay = this.sanitizeByDay(opts.byDay);
209
223
  opts.byMonth = this.sanitizeNumericArray(opts.byMonth, 1, 12, false, false);
210
224
  opts.byMonthDay = this.sanitizeNumericArray(opts.byMonthDay, -31, 31, false, false);
211
225
  opts.byYearDay = this.sanitizeNumericArray(opts.byYearDay, -366, 366, false, false);
@@ -293,37 +307,39 @@ var RRuleTemporal = class _RRuleTemporal {
293
307
  return this.applyTimeOverride(zdt.add({ days: 1 }));
294
308
  }
295
309
  if (freq === "SECONDLY") {
296
- let next = zdt.add({ seconds: interval });
310
+ let candidate = zdt;
297
311
  if (bySecond && bySecond.length > 0) {
298
- const nextSecond = bySecond.find((s) => s > zdt.second);
299
- if (nextSecond !== void 0) {
300
- next = zdt.with({ second: nextSecond });
301
- } else {
302
- next = zdt.add({ minutes: 1 }).with({ second: bySecond[0] });
312
+ const nextSecondInList = bySecond.find((s) => s > candidate.second);
313
+ if (nextSecondInList !== void 0) {
314
+ return candidate.with({ second: nextSecondInList });
303
315
  }
316
+ candidate = candidate.with({ second: bySecond[0] }).add({ minutes: 1 });
317
+ } else {
318
+ candidate = candidate.add({ seconds: interval });
304
319
  }
305
- if (byMinute && byMinute.length > 0 && !byMinute.includes(next.minute)) {
306
- const nextMinute = byMinute.find((m) => m > next.minute);
307
- if (nextMinute !== void 0) {
308
- next = next.with({ minute: nextMinute, second: bySecond ? bySecond[0] : 0 });
309
- } else {
310
- const nextHour = byHour ? byHour.find((h) => h > next.hour) : void 0;
311
- if (nextHour !== void 0) {
312
- next = next.with({ hour: nextHour, minute: byMinute[0], second: bySecond ? bySecond[0] : 0 });
313
- } else {
314
- next = this.applyTimeOverride(next.add({ days: 1 }));
320
+ if (byMinute && byMinute.length > 0) {
321
+ if (!byMinute.includes(candidate.minute) || candidate.minute === zdt.minute && candidate.second < zdt.second) {
322
+ const nextMinuteInList = byMinute.find((m) => m > candidate.minute);
323
+ if (nextMinuteInList !== void 0) {
324
+ return candidate.with({ minute: nextMinuteInList, second: bySecond ? bySecond[0] : 0 });
315
325
  }
326
+ candidate = candidate.with({ minute: byMinute[0], second: bySecond ? bySecond[0] : 0 }).add({ hours: 1 });
316
327
  }
317
328
  }
318
- if (byHour && byHour.length > 0 && !byHour.includes(next.hour)) {
319
- const nextHour = byHour.find((h) => h > next.hour);
320
- if (nextHour !== void 0) {
321
- next = next.with({ hour: nextHour, minute: byMinute ? byMinute[0] : 0, second: bySecond ? bySecond[0] : 0 });
322
- } else {
323
- next = this.applyTimeOverride(next.add({ days: 1 }));
329
+ if (byHour && byHour.length > 0) {
330
+ if (!byHour.includes(candidate.hour) || candidate.hour === zdt.hour && candidate.minute < zdt.minute) {
331
+ const nextHourInList = byHour.find((h) => h > candidate.hour);
332
+ if (nextHourInList !== void 0) {
333
+ return candidate.with({
334
+ hour: nextHourInList,
335
+ minute: byMinute ? byMinute[0] : 0,
336
+ second: bySecond ? bySecond[0] : 0
337
+ });
338
+ }
339
+ candidate = candidate.with({ hour: byHour[0], minute: byMinute ? byMinute[0] : 0, second: bySecond ? bySecond[0] : 0 }).add({ days: 1 });
324
340
  }
325
341
  }
326
- return next;
342
+ return candidate;
327
343
  }
328
344
  if (byMinute && byMinute.length > 1) {
329
345
  const idx = byMinute.indexOf(zdt.minute);
@@ -460,7 +476,7 @@ var RRuleTemporal = class _RRuleTemporal {
460
476
  }
461
477
  }
462
478
  } else {
463
- let deltas = [];
479
+ let deltas;
464
480
  if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && this.opts.byDay.every((tok) => /^[A-Z]{2}$/.test(tok))) {
465
481
  deltas = this.opts.byDay.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
466
482
  } else {
@@ -580,7 +596,7 @@ var RRuleTemporal = class _RRuleTemporal {
580
596
  return bySecond.includes(zdt.second);
581
597
  }
582
598
  matchesAll(zdt) {
583
- return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt) && this.matchesByYearDay(zdt) && this.matchesByWeekNo(zdt) && this.matchesByHour(zdt) && this.matchesByMinute(zdt) && this.matchesBySecond(zdt);
599
+ return this.matchesByMonth(zdt) && this.matchesByWeekNo(zdt) && this.matchesByYearDay(zdt) && this.matchesByMonthDay(zdt) && this.matchesByDay(zdt) && this.matchesByHour(zdt) && this.matchesByMinute(zdt) && this.matchesBySecond(zdt);
584
600
  }
585
601
  matchesByYearDay(zdt) {
586
602
  const { byYearDay } = this.opts;
@@ -603,11 +619,8 @@ var RRuleTemporal = class _RRuleTemporal {
603
619
  if (!byWeekNo) return true;
604
620
  const { week, year } = this.getIsoWeekInfo(zdt);
605
621
  const jan1 = zdt.with({ year, month: 1, day: 1 });
606
- const dec31 = zdt.with({ year, month: 12, day: 31 });
607
- let lastWeek = 52;
608
- if (jan1.dayOfWeek === 4 || dec31.dayOfWeek === 4) {
609
- lastWeek = 53;
610
- }
622
+ const isLeapYear = jan1.inLeapYear;
623
+ const lastWeek = jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52;
611
624
  return byWeekNo.some((wn) => {
612
625
  if (wn > 0) {
613
626
  return week === wn;
@@ -659,326 +672,448 @@ var RRuleTemporal = class _RRuleTemporal {
659
672
  * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
660
673
  * @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
661
674
  */
662
- all(iterator) {
675
+ _allMonthlyByDayOrMonthDay(iterator) {
676
+ const dates = [];
677
+ let iterationCount = 0;
678
+ const start = this.originalDtstart;
679
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
680
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
681
+ }
682
+ let monthCursor = start.with({ day: 1 });
683
+ while (true) {
684
+ if (++iterationCount > this.maxIterations) {
685
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
686
+ }
687
+ let occs = this.generateMonthlyOccurrences(monthCursor);
688
+ occs = this.applyBySetPos(occs);
689
+ if (monthCursor.month === start.month && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) < 0) && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) === 0)) {
690
+ monthCursor = monthCursor.add({ months: this.opts.interval });
691
+ continue;
692
+ }
693
+ const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
694
+ if (shouldBreak) {
695
+ break;
696
+ }
697
+ monthCursor = monthCursor.add({ months: this.opts.interval });
698
+ }
699
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
700
+ }
701
+ _allWeekly(iterator) {
663
702
  var _a;
664
- if (!this.opts.count && !this.opts.until && !iterator) {
665
- throw new Error("all() requires iterator when no COUNT/UNTIL");
703
+ const dates = [];
704
+ let iterationCount = 0;
705
+ const start = this.originalDtstart;
706
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
707
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
666
708
  }
709
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
710
+ const tokens = this.opts.byDay ? [...this.opts.byDay] : this.opts.byMonthDay && this.opts.byMonthDay.length > 0 ? Object.keys(dayMap) : [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
711
+ const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
712
+ const firstWeekDates = dows.map((dw) => {
713
+ const delta = (dw - start.dayOfWeek + 7) % 7;
714
+ return start.add({ days: delta });
715
+ });
716
+ const firstOccurrence = firstWeekDates.reduce((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
717
+ const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
718
+ const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
719
+ let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
720
+ while (true) {
721
+ if (++iterationCount > this.maxIterations) {
722
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
723
+ }
724
+ let occs = dows.flatMap((dw) => {
725
+ const delta = (dw - wkstDay + 7) % 7;
726
+ const sameDate = weekCursor.add({ days: delta });
727
+ return this.expandByTime(sameDate);
728
+ }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
729
+ occs = this.applyBySetPos(occs);
730
+ const { shouldBreak } = this.processOccurrences(
731
+ occs,
732
+ dates,
733
+ start,
734
+ iterator,
735
+ (occ) => this.matchesByMonth(occ) && this.matchesByMonthDay(occ)
736
+ );
737
+ if (shouldBreak) {
738
+ break;
739
+ }
740
+ weekCursor = weekCursor.add({ weeks: this.opts.interval });
741
+ }
742
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
743
+ }
744
+ _allMonthlyByMonth(iterator) {
667
745
  const dates = [];
668
746
  let iterationCount = 0;
669
- if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
670
- const start = this.originalDtstart;
671
- if (!this.addDtstartIfNeeded(dates, iterator)) {
672
- return this.applyCountLimitAndMergeRDates(dates, iterator);
747
+ const start = this.originalDtstart;
748
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
749
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
750
+ }
751
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
752
+ let monthOffset = 0;
753
+ let startMonthIndex = months.findIndex((m) => m >= start.month);
754
+ if (startMonthIndex === -1) {
755
+ startMonthIndex = 0;
756
+ monthOffset = 1;
757
+ }
758
+ while (true) {
759
+ if (++iterationCount > this.maxIterations) {
760
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
673
761
  }
674
- let monthCursor = start.with({ day: 1 });
675
- while (true) {
676
- if (++iterationCount > this.maxIterations) {
677
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
678
- }
679
- let occs = this.generateMonthlyOccurrences(monthCursor);
680
- occs = this.applyBySetPos(occs);
681
- if (monthCursor.month === start.month && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) < 0) && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) === 0)) {
682
- monthCursor = monthCursor.add({ months: this.opts.interval });
683
- continue;
762
+ const monthIndex = startMonthIndex + monthOffset;
763
+ const targetMonth = months[monthIndex % months.length];
764
+ const yearsToAdd = Math.floor(monthIndex / months.length);
765
+ const candidate = start.with({
766
+ year: start.year + yearsToAdd,
767
+ month: targetMonth
768
+ });
769
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(candidate, this.opts.until) > 0) {
770
+ break;
771
+ }
772
+ if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
773
+ if (iterator && !iterator(candidate, dates.length)) {
774
+ break;
684
775
  }
685
- const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
686
- if (shouldBreak) {
776
+ dates.push(candidate);
777
+ if (this.shouldBreakForCountLimit(dates.length)) {
687
778
  break;
688
779
  }
689
- monthCursor = monthCursor.add({ months: this.opts.interval });
690
780
  }
781
+ monthOffset++;
782
+ }
783
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
784
+ }
785
+ _allYearlyByMonth(iterator) {
786
+ const dates = [];
787
+ let iterationCount = 0;
788
+ const start = this.originalDtstart;
789
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
691
790
  return this.applyCountLimitAndMergeRDates(dates, iterator);
692
791
  }
693
- if (this.opts.freq === "WEEKLY" && !(this.opts.byYearDay && this.opts.byYearDay.length > 0)) {
694
- const start = this.originalDtstart;
695
- if (!this.addDtstartIfNeeded(dates, iterator)) {
696
- return this.applyCountLimitAndMergeRDates(dates, iterator);
792
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
793
+ let yearOffset = 0;
794
+ while (true) {
795
+ if (++iterationCount > this.maxIterations) {
796
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
697
797
  }
698
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
699
- const tokens = this.opts.byDay ? [...this.opts.byDay] : this.opts.byMonthDay && this.opts.byMonthDay.length > 0 ? Object.keys(dayMap) : [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
700
- const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
701
- const firstWeekDates = dows.map((dw) => {
702
- const delta = (dw - start.dayOfWeek + 7) % 7;
703
- return start.add({ days: delta });
704
- });
705
- let firstOccurrence = firstWeekDates.reduce((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
706
- const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
707
- const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
708
- let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
709
- while (true) {
710
- if (++iterationCount > this.maxIterations) {
711
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
798
+ const year = start.year + yearOffset * this.opts.interval;
799
+ for (const month of months) {
800
+ let occ = start.with({ year, month });
801
+ occ = this.applyTimeOverride(occ);
802
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) {
803
+ continue;
712
804
  }
713
- let occs = dows.flatMap((dw) => {
714
- const delta = (dw - wkstDay + 7) % 7;
715
- const sameDate = weekCursor.add({ days: delta });
716
- return this.expandByTime(sameDate);
717
- }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
718
- occs = this.applyBySetPos(occs);
719
- const { shouldBreak } = this.processOccurrences(
720
- occs,
721
- dates,
722
- start,
723
- iterator,
724
- (occ) => this.matchesByMonth(occ) && this.matchesByMonthDay(occ)
725
- );
726
- if (shouldBreak) {
727
- break;
805
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
806
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
807
+ }
808
+ if (iterator && !iterator(occ, dates.length)) {
809
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
810
+ }
811
+ dates.push(occ);
812
+ if (this.shouldBreakForCountLimit(dates.length)) {
813
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
728
814
  }
729
- weekCursor = weekCursor.add({ weeks: this.opts.interval });
730
815
  }
816
+ yearOffset++;
817
+ }
818
+ }
819
+ _allYearlyComplex(iterator) {
820
+ const dates = [];
821
+ let iterationCount = 0;
822
+ const start = this.originalDtstart;
823
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
731
824
  return this.applyCountLimitAndMergeRDates(dates, iterator);
732
825
  }
733
- if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay) {
734
- const start = this.originalDtstart;
735
- if (!this.addDtstartIfNeeded(dates, iterator)) {
736
- return this.applyCountLimitAndMergeRDates(dates, iterator);
826
+ let yearCursor = start.with({ month: 1, day: 1 });
827
+ while (true) {
828
+ if (++iterationCount > this.maxIterations) {
829
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
737
830
  }
738
- const months = [...this.opts.byMonth].sort((a, b) => a - b);
739
- let monthOffset = 0;
740
- let startMonthIndex = months.findIndex((m) => m >= start.month);
741
- if (startMonthIndex === -1) {
742
- startMonthIndex = 0;
743
- monthOffset = 1;
744
- }
745
- while (true) {
746
- if (++iterationCount > this.maxIterations) {
747
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
831
+ const occs = this.generateYearlyOccurrences(yearCursor);
832
+ const uniqueOccs = [];
833
+ if (occs.length > 0) {
834
+ uniqueOccs.push(occs[0]);
835
+ for (let i = 1; i < occs.length; i++) {
836
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occs[i], occs[i - 1]) !== 0) {
837
+ uniqueOccs.push(occs[i]);
838
+ }
748
839
  }
749
- const monthIndex = startMonthIndex + monthOffset;
750
- const targetMonth = months[monthIndex % months.length];
751
- const yearsToAdd = Math.floor(monthIndex / months.length);
752
- const candidate = start.with({
753
- year: start.year + yearsToAdd,
754
- month: targetMonth
755
- });
756
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(candidate, this.opts.until) > 0) {
840
+ }
841
+ const { shouldBreak } = this.processOccurrences(uniqueOccs, dates, start, iterator);
842
+ if (shouldBreak) {
843
+ break;
844
+ }
845
+ const interval = this.opts.freq === "WEEKLY" ? 1 : this.opts.interval;
846
+ yearCursor = yearCursor.add({ years: interval });
847
+ if (this.opts.freq === "WEEKLY" && this.opts.until && yearCursor.year > this.opts.until.year) {
848
+ break;
849
+ }
850
+ }
851
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
852
+ }
853
+ _allMinutelySecondlyComplex(iterator) {
854
+ const dates = [];
855
+ let iterationCount = 0;
856
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
857
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
858
+ }
859
+ let current = this.computeFirst();
860
+ while (true) {
861
+ if (++iterationCount > this.maxIterations) {
862
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
863
+ }
864
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
865
+ break;
866
+ }
867
+ if (this.matchesAll(current)) {
868
+ if (iterator && !iterator(current, dates.length)) {
757
869
  break;
758
870
  }
759
- if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
760
- if (iterator && !iterator(candidate, dates.length)) {
761
- break;
762
- }
763
- dates.push(candidate);
764
- if (this.shouldBreakForCountLimit(dates.length)) {
765
- break;
766
- }
871
+ dates.push(current);
872
+ if (this.shouldBreakForCountLimit(dates.length)) {
873
+ break;
767
874
  }
768
- monthOffset++;
875
+ current = this.nextCandidateSameDate(current);
876
+ } else {
877
+ current = this.findNextValidDate(current);
769
878
  }
879
+ }
880
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
881
+ }
882
+ _allMonthlyByWeekNo(iterator) {
883
+ const dates = [];
884
+ let iterationCount = 0;
885
+ const start = this.originalDtstart;
886
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
770
887
  return this.applyCountLimitAndMergeRDates(dates, iterator);
771
888
  }
772
- if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo) {
773
- const start = this.originalDtstart;
774
- if (!this.addDtstartIfNeeded(dates, iterator)) {
775
- return this.applyCountLimitAndMergeRDates(dates, iterator);
889
+ let current = start;
890
+ const weekNos = [...this.opts.byWeekNo].sort((a, b) => a - b);
891
+ const interval = this.opts.interval;
892
+ let monthsAdvanced = 0;
893
+ let lastYearProcessed = -1;
894
+ outer_loop: while (true) {
895
+ if (this.shouldBreakForCountLimit(dates.length)) {
896
+ break;
776
897
  }
777
- const months = [...this.opts.byMonth].sort((a, b) => a - b);
778
- let yearOffset = 0;
779
- while (true) {
780
- if (++iterationCount > this.maxIterations) {
781
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
898
+ if (++iterationCount > this.maxIterations) {
899
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
900
+ }
901
+ const year = current.year;
902
+ if (year !== lastYearProcessed && current.month >= start.month) {
903
+ lastYearProcessed = year;
904
+ for (const weekNo of weekNos) {
905
+ const occs = this.generateOccurrencesForWeekInYear(year, weekNo);
906
+ for (const occ of occs) {
907
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) >= 0) {
908
+ if (iterator && !iterator(occ, dates.length)) {
909
+ break outer_loop;
910
+ }
911
+ dates.push(occ);
912
+ if (this.shouldBreakForCountLimit(dates.length)) {
913
+ break outer_loop;
914
+ }
915
+ }
916
+ }
782
917
  }
783
- const year = start.year + yearOffset * this.opts.interval;
784
- for (const month of months) {
785
- let occ = start.with({ year, month });
786
- occ = this.applyTimeOverride(occ);
787
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) {
918
+ }
919
+ monthsAdvanced += interval;
920
+ current = start.add({ months: monthsAdvanced });
921
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
922
+ break;
923
+ }
924
+ }
925
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
926
+ }
927
+ _allMonthlyByYearDay(iterator) {
928
+ const dates = [];
929
+ let iterationCount = 0;
930
+ const start = this.originalDtstart;
931
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
932
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
933
+ }
934
+ let year = start.year;
935
+ const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
936
+ const interval = this.opts.interval;
937
+ const startMonthAbs = start.year * 12 + start.month;
938
+ outer_loop: while (true) {
939
+ if (this.shouldBreakForCountLimit(dates.length)) {
940
+ break;
941
+ }
942
+ if (++iterationCount > this.maxIterations) {
943
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
944
+ }
945
+ const yearStart = start.with({ year, month: 1, day: 1 });
946
+ const lastDayOfYear = yearStart.with({ month: 12, day: 31 }).dayOfYear;
947
+ for (const yd of yearDays) {
948
+ const dayNum = yd > 0 ? yd : lastDayOfYear + yd + 1;
949
+ if (dayNum <= 0 || dayNum > lastDayOfYear) continue;
950
+ const baseOcc = yearStart.add({ days: dayNum - 1 });
951
+ for (const occ of this.expandByTime(baseOcc)) {
952
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
953
+ if (dates.some((d) => import_polyfill.Temporal.ZonedDateTime.compare(d, occ) === 0)) continue;
954
+ const occMonthAbs = occ.year * 12 + occ.month;
955
+ if ((occMonthAbs - startMonthAbs) % interval !== 0) {
956
+ continue;
957
+ }
958
+ if (!this.matchesByMonth(occ)) {
788
959
  continue;
789
960
  }
790
961
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
791
- return dates;
962
+ break outer_loop;
792
963
  }
793
964
  if (iterator && !iterator(occ, dates.length)) {
794
- return dates;
965
+ break outer_loop;
795
966
  }
796
967
  dates.push(occ);
797
968
  if (this.shouldBreakForCountLimit(dates.length)) {
798
- return this.applyCountLimitAndMergeRDates(dates, iterator);
969
+ break outer_loop;
799
970
  }
800
971
  }
801
- yearOffset++;
802
972
  }
803
- return this.applyCountLimitAndMergeRDates(dates, iterator);
804
- }
805
- if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay || this.opts.byYearDay || this.opts.byWeekNo) || this.opts.freq === "WEEKLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0) {
806
- const start = this.originalDtstart;
807
- if (!this.addDtstartIfNeeded(dates, iterator)) {
808
- return this.applyCountLimitAndMergeRDates(dates, iterator);
973
+ year++;
974
+ if (this.opts.until && year > this.opts.until.year + 2) {
975
+ break;
809
976
  }
810
- let yearCursor = start.with({ month: 1, day: 1 });
811
- while (true) {
812
- if (++iterationCount > this.maxIterations) {
813
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
814
- }
815
- const occs = this.generateYearlyOccurrences(yearCursor);
816
- const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
817
- if (shouldBreak) {
818
- break;
819
- }
820
- const interval = this.opts.freq === "WEEKLY" ? 1 : this.opts.interval;
821
- yearCursor = yearCursor.add({ years: interval });
822
- if (this.opts.freq === "WEEKLY" && this.opts.until && yearCursor.year > this.opts.until.year) {
977
+ if (!this.opts.until && this.opts.count) {
978
+ const yearsToScan = Math.ceil(this.opts.count / (this.opts.byYearDay.length || 1)) * interval + 5;
979
+ if (year > start.year + yearsToScan) {
823
980
  break;
824
981
  }
825
982
  }
983
+ }
984
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
985
+ }
986
+ _allDailyMinutelyHourlyWithBySetPos(iterator) {
987
+ const dates = [];
988
+ let iterationCount = 0;
989
+ const start = this.originalDtstart;
990
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
826
991
  return this.applyCountLimitAndMergeRDates(dates, iterator);
827
992
  }
828
- if ((this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") && (this.opts.byMonth || this.opts.byWeekNo || this.opts.byYearDay || this.opts.byMonthDay || this.opts.byDay)) {
829
- if (!this.addDtstartIfNeeded(dates, iterator)) {
993
+ let cursor;
994
+ let duration;
995
+ switch (this.opts.freq) {
996
+ case "MINUTELY":
997
+ cursor = start.with({ second: 0, microsecond: 0, nanosecond: 0 });
998
+ duration = { minutes: this.opts.interval };
999
+ break;
1000
+ case "HOURLY":
1001
+ cursor = start.with({ minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
1002
+ duration = { hours: this.opts.interval };
1003
+ break;
1004
+ case "DAILY":
1005
+ cursor = start.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
1006
+ duration = { days: this.opts.interval };
1007
+ break;
1008
+ default:
830
1009
  return this.applyCountLimitAndMergeRDates(dates, iterator);
1010
+ }
1011
+ while (true) {
1012
+ if (++iterationCount > this.maxIterations) {
1013
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
831
1014
  }
832
- let current = this.computeFirst();
833
- while (true) {
834
- if (++iterationCount > this.maxIterations) {
835
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
836
- }
837
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
838
- break;
839
- }
840
- if (this.matchesAll(current)) {
841
- if (iterator && !iterator(current, dates.length)) {
842
- break;
843
- }
844
- dates.push(current);
845
- if (this.shouldBreakForCountLimit(dates.length)) {
846
- break;
847
- }
848
- current = this.nextCandidateSameDate(current);
849
- } else {
850
- current = this.findNextValidDate(current);
851
- }
1015
+ let periodOccs = this.expandByTime(cursor);
1016
+ periodOccs = periodOccs.filter((occ) => this.matchesAll(occ));
1017
+ periodOccs = this.applyBySetPos(periodOccs);
1018
+ const { shouldBreak } = this.processOccurrences(periodOccs, dates, start, iterator);
1019
+ if (shouldBreak) {
1020
+ break;
1021
+ }
1022
+ cursor = cursor.add(duration);
1023
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(cursor, this.opts.until) > 0) {
1024
+ break;
852
1025
  }
853
- return this.applyCountLimitAndMergeRDates(dates, iterator);
854
1026
  }
855
- if (this.opts.freq === "MONTHLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 && !this.opts.byDay && !this.opts.byMonthDay) {
856
- const start = this.originalDtstart;
857
- if (!this.addDtstartIfNeeded(dates, iterator)) {
1027
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
1028
+ }
1029
+ _allFallback(iterator) {
1030
+ const dates = [];
1031
+ let iterationCount = 0;
1032
+ let current = this.computeFirst();
1033
+ if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
1034
+ if (iterator && !iterator(this.originalDtstart, dates.length)) {
858
1035
  return this.applyCountLimitAndMergeRDates(dates, iterator);
859
1036
  }
860
- let year = start.year;
861
- const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
862
- const interval = this.opts.interval;
863
- const startMonthAbs = start.year * 12 + start.month;
864
- outer_loop: while (true) {
865
- if (this.shouldBreakForCountLimit(dates.length)) {
866
- break;
867
- }
868
- if (++iterationCount > this.maxIterations) {
869
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
870
- }
871
- const yearStart = start.with({ year, month: 1, day: 1 });
872
- const lastDayOfYear = yearStart.with({ month: 12, day: 31 }).dayOfYear;
873
- for (const yd of yearDays) {
874
- const dayNum = yd > 0 ? yd : lastDayOfYear + yd + 1;
875
- if (dayNum <= 0 || dayNum > lastDayOfYear) continue;
876
- const baseOcc = yearStart.add({ days: dayNum - 1 });
877
- for (const occ of this.expandByTime(baseOcc)) {
878
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
879
- if (dates.some((d) => import_polyfill.Temporal.ZonedDateTime.compare(d, occ) === 0)) continue;
880
- const occMonthAbs = occ.year * 12 + occ.month;
881
- if ((occMonthAbs - startMonthAbs) % interval !== 0) {
882
- continue;
883
- }
884
- if (!this.matchesByMonth(occ)) {
885
- continue;
886
- }
887
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
888
- break outer_loop;
889
- }
890
- if (iterator && !iterator(occ, dates.length)) {
891
- break outer_loop;
892
- }
893
- dates.push(occ);
894
- if (this.shouldBreakForCountLimit(dates.length)) {
895
- break outer_loop;
896
- }
897
- }
898
- }
899
- year++;
900
- if (this.opts.until && year > this.opts.until.year + 2) {
901
- break;
902
- }
903
- if (!this.opts.until && this.opts.count) {
904
- const yearsToScan = Math.ceil(this.opts.count / (this.opts.byYearDay.length || 1)) * interval + 5;
905
- if (year > start.year + yearsToScan) {
906
- break;
907
- }
908
- }
1037
+ dates.push(this.originalDtstart);
1038
+ if (this.shouldBreakForCountLimit(dates.length)) {
1039
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
909
1040
  }
910
- return this.applyCountLimitAndMergeRDates(dates, iterator);
911
1041
  }
912
- if ((this.opts.freq === "MINUTELY" || this.opts.freq === "HOURLY" || this.opts.freq === "DAILY") && this.opts.bySetPos) {
913
- const start = this.originalDtstart;
914
- if (!this.addDtstartIfNeeded(dates, iterator)) {
915
- return this.applyCountLimitAndMergeRDates(dates, iterator);
1042
+ while (true) {
1043
+ if (++iterationCount > this.maxIterations) {
1044
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
916
1045
  }
917
- let cursor;
918
- let duration;
919
- switch (this.opts.freq) {
920
- case "MINUTELY":
921
- cursor = start.with({ second: 0, microsecond: 0, nanosecond: 0 });
922
- duration = { minutes: this.opts.interval };
923
- break;
924
- case "HOURLY":
925
- cursor = start.with({ minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
926
- duration = { hours: this.opts.interval };
927
- break;
928
- case "DAILY":
929
- cursor = start.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
930
- duration = { days: this.opts.interval };
931
- break;
932
- default:
933
- return this.applyCountLimitAndMergeRDates(dates, iterator);
1046
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
1047
+ break;
934
1048
  }
935
- while (true) {
936
- if (++iterationCount > this.maxIterations) {
937
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
938
- }
939
- let periodOccs = this.expandByTime(cursor);
940
- periodOccs = periodOccs.filter((occ) => this.matchesAll(occ));
941
- periodOccs = this.applyBySetPos(periodOccs);
942
- const { shouldBreak } = this.processOccurrences(periodOccs, dates, start, iterator);
943
- if (shouldBreak) {
1049
+ if (this.matchesAll(current)) {
1050
+ if (iterator && !iterator(current, dates.length)) {
944
1051
  break;
945
1052
  }
946
- cursor = cursor.add(duration);
947
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(cursor, this.opts.until) > 0) {
948
- break;
949
- }
950
- }
951
- } else {
952
- let current = this.computeFirst();
953
- if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
954
- if (iterator && !iterator(this.originalDtstart, dates.length)) {
955
- return this.applyCountLimitAndMergeRDates(dates, iterator);
956
- }
957
- dates.push(this.originalDtstart);
1053
+ dates.push(current);
958
1054
  if (this.shouldBreakForCountLimit(dates.length)) {
959
- return this.applyCountLimitAndMergeRDates(dates, iterator);
1055
+ break;
960
1056
  }
961
1057
  }
962
- while (true) {
963
- if (++iterationCount > this.maxIterations) {
964
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
965
- }
966
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
1058
+ current = this.nextCandidateSameDate(current);
1059
+ }
1060
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
1061
+ }
1062
+ /**
1063
+ * Returns all occurrences of the rule.
1064
+ * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
1065
+ * @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
1066
+ */
1067
+ all(iterator) {
1068
+ if (this.opts.byWeekNo && this.opts.byYearDay) {
1069
+ const yearStart = this.originalDtstart.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 });
1070
+ const yearDays = this.opts.byYearDay.map((yd) => {
1071
+ const lastDayOfYear = yearStart.with({ month: 12, day: 31 }).dayOfYear;
1072
+ return yd > 0 ? yd : lastDayOfYear + yd + 1;
1073
+ });
1074
+ let possibleDate = false;
1075
+ for (const yd of yearDays) {
1076
+ const date = yearStart.add({ days: yd - 1 });
1077
+ if (this.matchesByWeekNo(date)) {
1078
+ possibleDate = true;
967
1079
  break;
968
1080
  }
969
- if (this.matchesAll(current)) {
970
- if (iterator && !iterator(current, dates.length)) {
971
- break;
972
- }
973
- dates.push(current);
974
- if (this.shouldBreakForCountLimit(dates.length)) {
975
- break;
976
- }
977
- }
978
- current = this.nextCandidateSameDate(current);
1081
+ }
1082
+ if (!possibleDate) {
1083
+ return [];
979
1084
  }
980
1085
  }
981
- return this.applyCountLimitAndMergeRDates(dates, iterator);
1086
+ if (!this.opts.count && !this.opts.until && !iterator) {
1087
+ throw new Error("all() requires iterator when no COUNT/UNTIL");
1088
+ }
1089
+ if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay) && !this.opts.byWeekNo) {
1090
+ return this._allMonthlyByDayOrMonthDay(iterator);
1091
+ }
1092
+ if (this.opts.freq === "WEEKLY" && !(this.opts.byYearDay && this.opts.byYearDay.length > 0) && !(this.opts.byWeekNo && this.opts.byWeekNo.length > 0)) {
1093
+ return this._allWeekly(iterator);
1094
+ }
1095
+ if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay) {
1096
+ return this._allMonthlyByMonth(iterator);
1097
+ }
1098
+ if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo) {
1099
+ return this._allYearlyByMonth(iterator);
1100
+ }
1101
+ if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay || this.opts.byYearDay || this.opts.byWeekNo) || this.opts.freq === "WEEKLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 || this.opts.freq === "WEEKLY" && this.opts.byWeekNo && this.opts.byWeekNo.length > 0) {
1102
+ return this._allYearlyComplex(iterator);
1103
+ }
1104
+ if ((this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") && (this.opts.byMonth || this.opts.byWeekNo || this.opts.byYearDay || this.opts.byMonthDay || this.opts.byDay)) {
1105
+ return this._allMinutelySecondlyComplex(iterator);
1106
+ }
1107
+ if (this.opts.freq === "MONTHLY" && this.opts.byWeekNo && this.opts.byWeekNo.length > 0) {
1108
+ return this._allMonthlyByWeekNo(iterator);
1109
+ }
1110
+ if (this.opts.freq === "MONTHLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 && !this.opts.byDay && !this.opts.byMonthDay) {
1111
+ return this._allMonthlyByYearDay(iterator);
1112
+ }
1113
+ if ((this.opts.freq === "MINUTELY" || this.opts.freq === "HOURLY" || this.opts.freq === "DAILY") && this.opts.bySetPos) {
1114
+ return this._allDailyMinutelyHourlyWithBySetPos(iterator);
1115
+ }
1116
+ return this._allFallback(iterator);
982
1117
  }
983
1118
  /**
984
1119
  * Converts rDate entries to ZonedDateTime and merges with existing dates.
@@ -986,8 +1121,9 @@ var RRuleTemporal = class _RRuleTemporal {
986
1121
  * @returns Merged and deduplicated array of dates
987
1122
  */
988
1123
  mergeAndDeduplicateRDates(dates) {
989
- if (!this.opts.rDate) return dates;
990
- dates.push(...this.opts.rDate);
1124
+ if (this.opts.rDate) {
1125
+ dates.push(...this.opts.rDate);
1126
+ }
991
1127
  dates.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
992
1128
  const dedup = [];
993
1129
  for (const d of dates) {
@@ -1225,8 +1361,7 @@ var RRuleTemporal = class _RRuleTemporal {
1225
1361
  * if BYMONTH is not specified.
1226
1362
  */
1227
1363
  generateYearlyOccurrences(sample) {
1228
- var _a;
1229
- const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : this.opts.byMonthDay ? [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] : [this.originalDtstart.month];
1364
+ const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : this.opts.byMonthDay || this.opts.byDay ? [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] : [this.originalDtstart.month];
1230
1365
  let occs = [];
1231
1366
  const hasOrdinalByDay = this.opts.byDay && this.opts.byDay.some((t) => /^[+-]?\d/.test(t));
1232
1367
  if (hasOrdinalByDay && !this.opts.byMonth) {
@@ -1258,6 +1393,7 @@ var RRuleTemporal = class _RRuleTemporal {
1258
1393
  const last = sample.with({ month: 12, day: 31 }).dayOfYear;
1259
1394
  for (const d of this.opts.byYearDay) {
1260
1395
  const dayNum = d > 0 ? d : last + d + 1;
1396
+ if (dayNum <= 0 || dayNum > last) continue;
1261
1397
  const dt = this.opts.freq === "MINUTELY" ? sample.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }).add({ days: dayNum - 1 }) : sample.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1262
1398
  if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
1263
1399
  occs.push(...this.expandByTime(dt));
@@ -1265,34 +1401,34 @@ var RRuleTemporal = class _RRuleTemporal {
1265
1401
  }
1266
1402
  }
1267
1403
  if (this.opts.byWeekNo) {
1268
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1269
- const wkst = dayMap[this.opts.wkst || "MO"];
1270
- const jan1 = sample.with({ month: 1, day: 1 });
1271
- const delta = (jan1.dayOfWeek - wkst + 7) % 7;
1272
- const firstWeekStart = jan1.subtract({ days: delta });
1273
- const isLeapYear = jan1.inLeapYear;
1274
- const lastWeek = jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52;
1275
- const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
1276
- var _a2;
1277
- return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
1278
- }) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
1404
+ const { lastWeek, firstWeekStart, tokens } = this.isoWeekByDay(sample);
1279
1405
  for (const weekNo of this.opts.byWeekNo) {
1406
+ if (weekNo > 0 && weekNo > lastWeek || weekNo < 0 && -weekNo > lastWeek) {
1407
+ continue;
1408
+ }
1280
1409
  const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1281
1410
  const weekStart = firstWeekStart.add({ weeks: weekIndex });
1282
- for (const tok of tokens) {
1283
- if (!tok) continue;
1284
- const targetDow = dayMap[tok];
1285
- const inst = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
1286
- if (!this.opts.byMonth || this.opts.byMonth.includes(inst.month)) {
1287
- occs.push(...this.expandByTime(inst));
1288
- }
1289
- }
1411
+ occs.push(...this.addByDay(tokens, weekStart));
1290
1412
  }
1291
1413
  }
1292
1414
  occs = occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1293
1415
  occs = this.applyBySetPos(occs);
1294
1416
  return occs;
1295
1417
  }
1418
+ addByDay(tokens, weekStart) {
1419
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1420
+ const wkst = dayMap[this.opts.wkst || "MO"];
1421
+ const entries = [];
1422
+ for (const tok of tokens) {
1423
+ if (!tok) continue;
1424
+ const targetDow = dayMap[tok];
1425
+ const inst = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
1426
+ if (!this.opts.byMonth || this.opts.byMonth.includes(inst.month)) {
1427
+ entries.push(...this.expandByTime(inst));
1428
+ }
1429
+ }
1430
+ return entries;
1431
+ }
1296
1432
  /**
1297
1433
  * Helper to find the next valid value from a sorted array
1298
1434
  */
@@ -1304,6 +1440,19 @@ var RRuleTemporal = class _RRuleTemporal {
1304
1440
  * large gaps when BYXXX constraints don't match.
1305
1441
  */
1306
1442
  findNextValidDate(current) {
1443
+ if (this.opts.byWeekNo && this.opts.byYearDay) {
1444
+ const yearStart = current.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 });
1445
+ const yearDays = this.opts.byYearDay.map((yd) => {
1446
+ const lastDayOfYear = yearStart.with({ month: 12, day: 31 }).dayOfYear;
1447
+ return yd > 0 ? yd : lastDayOfYear + yd + 1;
1448
+ });
1449
+ for (const yd of yearDays) {
1450
+ const date = yearStart.add({ days: yd - 1 });
1451
+ if (this.matchesByWeekNo(date)) {
1452
+ break;
1453
+ }
1454
+ }
1455
+ }
1307
1456
  if (this.opts.byMonth && !this.opts.byMonth.includes(current.month)) {
1308
1457
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
1309
1458
  const nextMonth = this.findNextValidValue(current.month, months, (a, b) => a - b);
@@ -1315,6 +1464,11 @@ var RRuleTemporal = class _RRuleTemporal {
1315
1464
  current = this.applyTimeOverride(current);
1316
1465
  return current;
1317
1466
  }
1467
+ if (this.opts.byWeekNo && !this.matchesByWeekNo(current)) {
1468
+ current = current.add({ weeks: 1 }).with({ hour: 0, minute: 0, second: 0 });
1469
+ current = this.applyTimeOverride(current);
1470
+ return current;
1471
+ }
1318
1472
  if (this.opts.byYearDay && !this.matchesByYearDay(current)) {
1319
1473
  const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
1320
1474
  const currentYearDay = current.dayOfYear;
@@ -1346,11 +1500,6 @@ var RRuleTemporal = class _RRuleTemporal {
1346
1500
  current = this.applyTimeOverride(current);
1347
1501
  return current;
1348
1502
  }
1349
- if (this.opts.byWeekNo && !this.matchesByWeekNo(current)) {
1350
- current = current.add({ weeks: 1 }).with({ hour: 0, minute: 0, second: 0 });
1351
- current = this.applyTimeOverride(current);
1352
- return current;
1353
- }
1354
1503
  if (this.opts.byMonthDay && !this.matchesByMonthDay(current)) {
1355
1504
  const monthDays = [...this.opts.byMonthDay].sort((a, b) => a - b);
1356
1505
  const lastDayOfMonth = current.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
@@ -1371,6 +1520,8 @@ var RRuleTemporal = class _RRuleTemporal {
1371
1520
  minute: 0,
1372
1521
  second: 0
1373
1522
  });
1523
+ } else {
1524
+ current = current.add({ months: 1 }).with({ day: 1, hour: 0, minute: 0, second: 0 });
1374
1525
  }
1375
1526
  }
1376
1527
  current = this.applyTimeOverride(current);
@@ -1378,26 +1529,39 @@ var RRuleTemporal = class _RRuleTemporal {
1378
1529
  }
1379
1530
  if (this.opts.byDay && !this.matchesByDay(current)) {
1380
1531
  const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1381
- const currentDow = current.dayOfWeek;
1382
- let nextDow = 8;
1383
- for (const token of this.opts.byDay) {
1384
- const match = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
1385
- if (match && !match[1]) {
1386
- const dow = dayMap[match[2]];
1387
- if (dow && dow > currentDow) {
1388
- nextDow = Math.min(nextDow, dow);
1389
- }
1390
- }
1391
- }
1392
- if (nextDow <= 7) {
1393
- current = current.add({ days: nextDow - currentDow }).with({ hour: 0, minute: 0, second: 0 });
1532
+ const targetDays = this.opts.byDay.map((tok) => {
1533
+ var _a;
1534
+ return (_a = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a[1];
1535
+ }).filter(Boolean).map((day) => dayMap[day]).filter(Boolean);
1536
+ const nextDayOfWeek = this.findNextValidValue(current.dayOfWeek, targetDays.sort(), (a, b) => a - b);
1537
+ if (nextDayOfWeek) {
1538
+ const delta = (nextDayOfWeek - current.dayOfWeek + 7) % 7;
1539
+ current = current.add({ days: delta }).with({ hour: 0, minute: 0, second: 0 });
1394
1540
  } else {
1395
- current = current.add({ days: 7 - currentDow + 1 }).with({ hour: 0, minute: 0, second: 0 });
1541
+ const delta = (targetDays[0] - current.dayOfWeek + 7) % 7;
1542
+ current = current.add({ days: delta + 7 }).with({ hour: 0, minute: 0, second: 0 });
1396
1543
  }
1397
1544
  current = this.applyTimeOverride(current);
1398
1545
  return current;
1399
1546
  }
1400
- return this.nextCandidateSameDate(current);
1547
+ switch (this.opts.freq) {
1548
+ case "SECONDLY":
1549
+ case "MINUTELY":
1550
+ current = current.add({ days: 1 }).with({ hour: 0, minute: 0, second: 0 });
1551
+ break;
1552
+ case "HOURLY":
1553
+ current = current.add({ days: 1 }).with({ hour: 0, minute: 0, second: 0 });
1554
+ break;
1555
+ case "DAILY":
1556
+ case "WEEKLY":
1557
+ current = current.add({ months: 1 }).with({ day: 1, hour: 0, minute: 0, second: 0 });
1558
+ break;
1559
+ case "MONTHLY":
1560
+ case "YEARLY":
1561
+ current = current.add({ years: 1 }).with({ month: 1, day: 1, hour: 0, minute: 0, second: 0 });
1562
+ break;
1563
+ }
1564
+ return this.applyTimeOverride(current);
1401
1565
  }
1402
1566
  applyBySetPos(list2) {
1403
1567
  const { bySetPos } = this.opts;
@@ -1411,6 +1575,37 @@ var RRuleTemporal = class _RRuleTemporal {
1411
1575
  }
1412
1576
  return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1413
1577
  }
1578
+ isoWeekByDay(sample) {
1579
+ var _a;
1580
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1581
+ const wkst = dayMap[this.opts.wkst || "MO"];
1582
+ const jan1 = sample.with({ month: 1, day: 1 });
1583
+ const jan4 = sample.with({ month: 1, day: 4 });
1584
+ const delta = (jan4.dayOfWeek - wkst + 7) % 7;
1585
+ const firstWeekStart = jan4.subtract({ days: delta });
1586
+ const isLeapYear = jan1.inLeapYear;
1587
+ const lastWeek = jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52;
1588
+ const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
1589
+ var _a2;
1590
+ return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
1591
+ }) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
1592
+ return { lastWeek, firstWeekStart, tokens };
1593
+ }
1594
+ /**
1595
+ * Generate occurrences for a specific week number in a given year
1596
+ */
1597
+ generateOccurrencesForWeekInYear(year, weekNo) {
1598
+ const occs = [];
1599
+ const sample = this.originalDtstart.with({ year, month: 1, day: 1 });
1600
+ const { lastWeek, firstWeekStart, tokens } = this.isoWeekByDay(sample);
1601
+ if (weekNo > 0 && weekNo > lastWeek || weekNo < 0 && -weekNo > lastWeek) {
1602
+ return occs;
1603
+ }
1604
+ const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1605
+ const weekStart = firstWeekStart.add({ weeks: weekIndex });
1606
+ occs.push(...this.addByDay(tokens, weekStart));
1607
+ return occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1608
+ }
1414
1609
  };
1415
1610
 
1416
1611
  // src/totext.ts