rrule-temporal 1.1.2 → 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/index.cjs CHANGED
@@ -41,25 +41,41 @@ var import_polyfill = require("@js-temporal/polyfill");
41
41
  function unfoldLine(foldedLine) {
42
42
  return foldedLine.replace(/\r?\n[ \t]/g, "");
43
43
  }
44
- function parseDateValues(dateValues, tzid, valueType) {
44
+ function parseIcsDateTime(dateStr, tzid, valueType) {
45
+ const isDate = valueType === "DATE" || !dateStr.includes("T");
46
+ const isoDate = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}`;
47
+ if (isDate) {
48
+ return import_polyfill.Temporal.PlainDate.from(isoDate).toZonedDateTime({ timeZone: tzid });
49
+ }
50
+ if (dateStr.endsWith("Z")) {
51
+ const iso = `${isoDate}T${dateStr.slice(9, 15)}Z`;
52
+ return import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
53
+ } else {
54
+ const iso = `${isoDate}T${dateStr.slice(9)}`;
55
+ return import_polyfill.Temporal.PlainDateTime.from(iso).toZonedDateTime(tzid);
56
+ }
57
+ }
58
+ function parseDateLines(lines, linePrefix, defaultTzid) {
45
59
  const dates = [];
46
- for (const dateValue of dateValues) {
47
- const isDate = valueType === "DATE" || !dateValue.includes("T");
48
- if (isDate) {
49
- const isoDate = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}`;
50
- dates.push(import_polyfill.Temporal.PlainDate.from(isoDate).toZonedDateTime({ timeZone: tzid }));
51
- } else {
52
- if (/Z$/.test(dateValue)) {
53
- const iso = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9, 15)}Z`;
54
- dates.push(import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC"));
55
- } else {
56
- const isoDate = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9)}`;
57
- dates.push(import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid));
58
- }
60
+ const regex = new RegExp(`^${linePrefix}(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)`, "i");
61
+ for (const line of lines) {
62
+ const match = line.match(regex);
63
+ if (match) {
64
+ const [, valueType, tzid, dateValuesStr] = match;
65
+ const timezone = tzid || defaultTzid;
66
+ const dateValues = dateValuesStr.split(",");
67
+ dates.push(...dateValues.map((dateValue) => parseIcsDateTime(dateValue, timezone, valueType)));
59
68
  }
60
69
  }
61
70
  return dates;
62
71
  }
72
+ function parseNumberArray(val, sort = false) {
73
+ const arr = val.split(",").map((n) => parseInt(n, 10));
74
+ if (sort) {
75
+ return arr.sort((a, b) => a - b);
76
+ }
77
+ return arr;
78
+ }
63
79
  function parseRRuleString(input, targetTimezone) {
64
80
  var _a;
65
81
  const unfoldedInput = unfoldLine(input).trim();
@@ -78,26 +94,10 @@ function parseRRuleString(input, targetTimezone) {
78
94
  if (!dtMatch) throw new Error("Invalid DTSTART in ICS snippet");
79
95
  const [, valueType, dtTzid, dtValue] = dtMatch;
80
96
  tzid = (_a = dtTzid != null ? dtTzid : targetTimezone) != null ? _a : tzid;
81
- dtstart = parseDateValues([dtValue], tzid, valueType)[0];
97
+ dtstart = parseIcsDateTime(dtValue, tzid, valueType);
82
98
  rruleLine = rrLine;
83
- for (const exLine of exLines) {
84
- const exMatch = exLine.match(/EXDATE(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)/i);
85
- if (exMatch) {
86
- const [, valueType2, exTzid, dateValuesStr] = exMatch;
87
- const exTimezone = exTzid || tzid;
88
- const dateValues = dateValuesStr.split(",");
89
- exDate.push(...parseDateValues(dateValues, exTimezone, valueType2));
90
- }
91
- }
92
- for (const rLine of rLines) {
93
- const rMatch = rLine.match(/RDATE(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)/i);
94
- if (rMatch) {
95
- const [, valueType2, rTzid, dateValuesStr] = rMatch;
96
- const rTimezone = rTzid || tzid;
97
- const dateValues = dateValuesStr.split(",");
98
- rDate.push(...parseDateValues(dateValues, rTimezone, valueType2));
99
- }
100
- }
99
+ exDate = parseDateLines(exLines, "EXDATE", tzid);
100
+ rDate = parseDateLines(rLines, "RDATE", tzid);
101
101
  } else {
102
102
  throw new Error("dtstart required when parsing RRULE alone");
103
103
  }
@@ -122,44 +122,38 @@ function parseRRuleString(input, targetTimezone) {
122
122
  opts.count = parseInt(val, 10);
123
123
  break;
124
124
  case "UNTIL": {
125
- if (/Z$/.test(val)) {
126
- const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}Z`;
127
- opts.until = import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
128
- } else {
129
- const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}`;
130
- opts.until = import_polyfill.Temporal.PlainDateTime.from(iso).toZonedDateTime(tzid || "UTC");
131
- if (tzid !== "UTC") {
132
- throw new Error("UNTIL rule part MUST always be specified as a date with UTC time");
133
- }
125
+ opts.until = parseIcsDateTime(val, tzid || "UTC");
126
+ if (!val.endsWith("Z") && tzid !== "UTC") {
127
+ throw new Error("UNTIL rule part MUST always be specified as a date with UTC time");
134
128
  }
135
129
  break;
136
130
  }
137
131
  case "BYHOUR":
138
- opts.byHour = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
132
+ opts.byHour = parseNumberArray(val, true);
139
133
  break;
140
134
  case "BYMINUTE":
141
- opts.byMinute = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
135
+ opts.byMinute = parseNumberArray(val, true);
136
+ break;
137
+ case "BYSECOND":
138
+ opts.bySecond = parseNumberArray(val, true);
142
139
  break;
143
140
  case "BYDAY":
144
141
  opts.byDay = val.split(",");
145
142
  break;
146
143
  case "BYMONTH":
147
- opts.byMonth = val.split(",").map((n) => parseInt(n, 10));
144
+ opts.byMonth = parseNumberArray(val);
148
145
  break;
149
146
  case "BYMONTHDAY":
150
- opts.byMonthDay = val.split(",").map((n) => parseInt(n, 10));
151
- break;
152
- case "BYSECOND":
153
- opts.bySecond = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
147
+ opts.byMonthDay = parseNumberArray(val);
154
148
  break;
155
149
  case "BYYEARDAY":
156
- opts.byYearDay = val.split(",").map((n) => parseInt(n, 10));
150
+ opts.byYearDay = parseNumberArray(val);
157
151
  break;
158
152
  case "BYWEEKNO":
159
- opts.byWeekNo = val.split(",").map((n) => parseInt(n, 10));
153
+ opts.byWeekNo = parseNumberArray(val);
160
154
  break;
161
155
  case "BYSETPOS":
162
- opts.bySetPos = val.split(",").map((n) => parseInt(n, 10));
156
+ opts.bySetPos = parseNumberArray(val);
163
157
  break;
164
158
  case "WKST":
165
159
  opts.wkst = val;
@@ -198,46 +192,44 @@ var RRuleTemporal = class _RRuleTemporal {
198
192
  this.maxIterations = (_d = manual.maxIterations) != null ? _d : 1e4;
199
193
  this.includeDtstart = (_e = manual.includeDtstart) != null ? _e : false;
200
194
  }
201
- sanitizeOpts(opts) {
195
+ sanitizeNumericArray(arr, min, max, allowZero = false, sort = false) {
196
+ if (!arr) return void 0;
197
+ const sanitized = arr.filter((n) => Number.isInteger(n) && n >= min && n <= max && (allowZero || n !== 0));
198
+ if (sanitized.length === 0) return void 0;
199
+ return sort ? sanitized.sort((a, b) => a - b) : sanitized;
200
+ }
201
+ sanitizeByDay(byDay) {
202
202
  const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
203
- if (opts.byDay) {
204
- opts.byDay = opts.byDay.filter((d) => validDay.test(d));
205
- if (opts.byDay.length === 0) delete opts.byDay;
206
- }
207
- if (opts.byMonth) {
208
- opts.byMonth = opts.byMonth.filter((n) => Number.isInteger(n) && n >= 1 && n <= 12);
209
- if (opts.byMonth.length === 0) delete opts.byMonth;
210
- }
211
- if (opts.byMonthDay) {
212
- opts.byMonthDay = opts.byMonthDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -31 && n <= 31);
213
- if (opts.byMonthDay.length === 0) delete opts.byMonthDay;
214
- }
215
- if (opts.byYearDay) {
216
- opts.byYearDay = opts.byYearDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -366 && n <= 366);
217
- if (opts.byYearDay.length === 0) delete opts.byYearDay;
218
- }
219
- if (opts.byWeekNo) {
220
- opts.byWeekNo = opts.byWeekNo.filter((n) => Number.isInteger(n) && n !== 0 && n >= -53 && n <= 53);
221
- if (opts.byWeekNo.length === 0) delete opts.byWeekNo;
222
- }
223
- if (opts.byHour) {
224
- opts.byHour = opts.byHour.filter((n) => Number.isInteger(n) && n >= 0 && n <= 23).sort((a, b) => a - b);
225
- if (opts.byHour.length === 0) delete opts.byHour;
226
- }
227
- if (opts.byMinute) {
228
- opts.byMinute = opts.byMinute.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
229
- if (opts.byMinute.length === 0) delete opts.byMinute;
230
- }
231
- if (opts.bySecond) {
232
- opts.bySecond = opts.bySecond.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
233
- if (opts.bySecond.length === 0) delete opts.bySecond;
203
+ const days = (byDay != null ? byDay : []).filter((day) => day && typeof day === "string");
204
+ for (const day of days) {
205
+ const match = day.match(validDay);
206
+ if (!match) {
207
+ throw new Error(`Invalid BYDAY value: ${day}`);
208
+ }
209
+ const ord = match[1];
210
+ if (ord) {
211
+ const ordInt = parseInt(ord, 10);
212
+ if (ordInt === 0) {
213
+ throw new Error(`Invalid BYDAY value: ${day}`);
214
+ }
215
+ }
234
216
  }
217
+ return days.length > 0 ? days : void 0;
218
+ }
219
+ sanitizeOpts(opts) {
220
+ opts.byDay = this.sanitizeByDay(opts.byDay);
221
+ opts.byMonth = this.sanitizeNumericArray(opts.byMonth, 1, 12, false, false);
222
+ opts.byMonthDay = this.sanitizeNumericArray(opts.byMonthDay, -31, 31, false, false);
223
+ opts.byYearDay = this.sanitizeNumericArray(opts.byYearDay, -366, 366, false, false);
224
+ opts.byWeekNo = this.sanitizeNumericArray(opts.byWeekNo, -53, 53, false, false);
225
+ opts.byHour = this.sanitizeNumericArray(opts.byHour, 0, 23, true, true);
226
+ opts.byMinute = this.sanitizeNumericArray(opts.byMinute, 0, 59, true, true);
227
+ opts.bySecond = this.sanitizeNumericArray(opts.bySecond, 0, 59, true, true);
235
228
  if (opts.bySetPos) {
236
229
  if (opts.bySetPos.some((p) => p === 0)) {
237
230
  throw new Error("bySetPos may not contain 0");
238
231
  }
239
- opts.bySetPos = opts.bySetPos.filter((n) => Number.isInteger(n) && n !== 0);
240
- if (opts.bySetPos.length === 0) delete opts.bySetPos;
232
+ opts.bySetPos = this.sanitizeNumericArray(opts.bySetPos, -Infinity, Infinity, false, false);
241
233
  }
242
234
  return opts;
243
235
  }
@@ -252,8 +244,14 @@ var RRuleTemporal = class _RRuleTemporal {
252
244
  return zdt.add({ months: interval });
253
245
  case "YEARLY":
254
246
  return zdt.add({ years: interval });
255
- case "HOURLY":
256
- return zdt.add({ hours: interval });
247
+ case "HOURLY": {
248
+ const originalHour = zdt.hour;
249
+ let next = zdt.add({ hours: interval });
250
+ if (next.hour === originalHour && interval === 1) {
251
+ next = next.add({ hours: interval });
252
+ }
253
+ return next;
254
+ }
257
255
  case "MINUTELY":
258
256
  return zdt.add({ minutes: interval });
259
257
  case "SECONDLY":
@@ -289,30 +287,57 @@ var RRuleTemporal = class _RRuleTemporal {
289
287
  if (freq === "MINUTELY" && byMinute && byMinute.length === 1) {
290
288
  return this.applyTimeOverride(zdt.add({ hours: interval }));
291
289
  }
292
- if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute && interval > 1) {
290
+ if (bySecond && bySecond.length > 1) {
291
+ const idx = bySecond.indexOf(zdt.second);
292
+ if (idx !== -1 && idx < bySecond.length - 1) {
293
+ return zdt.with({ second: bySecond[idx + 1] });
294
+ }
295
+ }
296
+ if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute) {
293
297
  const next = zdt.add({ minutes: interval });
294
298
  if (byHour.includes(next.hour)) {
295
- return next;
299
+ return next.with({ second: bySecond ? bySecond[0] : zdt.second });
296
300
  }
297
301
  const nextHour = byHour.find((h) => h > zdt.hour) || byHour[0];
298
302
  if (nextHour && nextHour > zdt.hour) {
299
- return zdt.with({ hour: nextHour, minute: 0 });
303
+ return zdt.with({ hour: nextHour, minute: 0, second: bySecond ? bySecond[0] : zdt.second });
300
304
  }
301
305
  return this.applyTimeOverride(zdt.add({ days: 1 }));
302
306
  }
303
- if (freq === "SECONDLY" && bySecond && bySecond.length === 1) {
304
- return this.applyTimeOverride(zdt.add({ minutes: interval })).with({ second: bySecond[0] });
305
- }
306
- if (freq === "SECONDLY" && byMinute && byMinute.length === 1) {
307
- const next = zdt.add({ seconds: interval });
308
- if (next.minute === byMinute[0]) return next;
309
- return this.applyTimeOverride(zdt.add({ hours: interval })).with({ second: 0 });
310
- }
311
- if (bySecond && bySecond.length > 1) {
312
- const idx = bySecond.indexOf(zdt.second);
313
- if (idx !== -1 && idx < bySecond.length - 1) {
314
- return zdt.with({ second: bySecond[idx + 1] });
307
+ if (freq === "SECONDLY") {
308
+ let candidate = zdt;
309
+ if (bySecond && bySecond.length > 0) {
310
+ const nextSecondInList = bySecond.find((s) => s > candidate.second);
311
+ if (nextSecondInList !== void 0) {
312
+ return candidate.with({ second: nextSecondInList });
313
+ }
314
+ candidate = candidate.with({ second: bySecond[0] }).add({ minutes: 1 });
315
+ } else {
316
+ candidate = candidate.add({ seconds: interval });
317
+ }
318
+ if (byMinute && byMinute.length > 0) {
319
+ if (!byMinute.includes(candidate.minute) || candidate.minute === zdt.minute && candidate.second < zdt.second) {
320
+ const nextMinuteInList = byMinute.find((m) => m > candidate.minute);
321
+ if (nextMinuteInList !== void 0) {
322
+ return candidate.with({ minute: nextMinuteInList, second: bySecond ? bySecond[0] : 0 });
323
+ }
324
+ candidate = candidate.with({ minute: byMinute[0], second: bySecond ? bySecond[0] : 0 }).add({ hours: 1 });
325
+ }
315
326
  }
327
+ if (byHour && byHour.length > 0) {
328
+ if (!byHour.includes(candidate.hour) || candidate.hour === zdt.hour && candidate.minute < zdt.minute) {
329
+ const nextHourInList = byHour.find((h) => h > candidate.hour);
330
+ if (nextHourInList !== void 0) {
331
+ return candidate.with({
332
+ hour: nextHourInList,
333
+ minute: byMinute ? byMinute[0] : 0,
334
+ second: bySecond ? bySecond[0] : 0
335
+ });
336
+ }
337
+ candidate = candidate.with({ hour: byHour[0], minute: byMinute ? byMinute[0] : 0, second: bySecond ? bySecond[0] : 0 }).add({ days: 1 });
338
+ }
339
+ }
340
+ return candidate;
316
341
  }
317
342
  if (byMinute && byMinute.length > 1) {
318
343
  const idx = byMinute.indexOf(zdt.minute);
@@ -322,6 +347,24 @@ var RRuleTemporal = class _RRuleTemporal {
322
347
  second: bySecond ? bySecond[0] : zdt.second
323
348
  });
324
349
  }
350
+ if (freq === "MINUTELY" && idx === byMinute.length - 1) {
351
+ if (byHour && byHour.length > 0) {
352
+ const currentHourIdx = byHour.indexOf(zdt.hour);
353
+ if (currentHourIdx !== -1 && currentHourIdx < byHour.length - 1) {
354
+ return zdt.with({
355
+ hour: byHour[currentHourIdx + 1],
356
+ minute: byMinute[0],
357
+ second: bySecond ? bySecond[0] : zdt.second
358
+ });
359
+ } else {
360
+ return this.applyTimeOverride(zdt.add({ days: 1 }));
361
+ }
362
+ }
363
+ return zdt.add({ hours: interval }).with({
364
+ minute: byMinute[0],
365
+ second: bySecond ? bySecond[0] : zdt.second
366
+ });
367
+ }
325
368
  }
326
369
  if (byHour && byHour.length > 1) {
327
370
  const idx = byHour.indexOf(zdt.hour);
@@ -333,6 +376,9 @@ var RRuleTemporal = class _RRuleTemporal {
333
376
  });
334
377
  }
335
378
  }
379
+ if (freq === "HOURLY" && byHour && byHour.length > 1) {
380
+ return this.applyTimeOverride(zdt.add({ days: 1 }));
381
+ }
336
382
  return this.applyTimeOverride(this.rawAdvance(zdt));
337
383
  }
338
384
  applyTimeOverride(zdt) {
@@ -344,7 +390,7 @@ var RRuleTemporal = class _RRuleTemporal {
344
390
  return dt;
345
391
  }
346
392
  computeFirst() {
347
- var _a, _b, _c;
393
+ var _a, _b, _c, _d;
348
394
  let zdt = this.originalDtstart;
349
395
  if (((_a = this.opts.byWeekNo) == null ? void 0 : _a.length) && ["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq)) {
350
396
  let targetWeek = this.opts.byWeekNo[0];
@@ -376,15 +422,7 @@ var RRuleTemporal = class _RRuleTemporal {
376
422
  weekStart = firstThursday.subtract({ days: 3 }).add({ weeks: lastWeek + targetWeek });
377
423
  }
378
424
  if ((_b = this.opts.byDay) == null ? void 0 : _b.length) {
379
- const dayMap = {
380
- MO: 1,
381
- TU: 2,
382
- WE: 3,
383
- TH: 4,
384
- FR: 5,
385
- SA: 6,
386
- SU: 7
387
- };
425
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
388
426
  const targetDays = this.opts.byDay.map((tok) => {
389
427
  var _a2;
390
428
  return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
@@ -411,27 +449,44 @@ var RRuleTemporal = class _RRuleTemporal {
411
449
  }
412
450
  }
413
451
  if (((_c = this.opts.byDay) == null ? void 0 : _c.length) && !this.opts.byWeekNo) {
414
- const dayMap = {
415
- MO: 1,
416
- TU: 2,
417
- WE: 3,
418
- TH: 4,
419
- FR: 5,
420
- SA: 6,
421
- SU: 7
422
- };
423
- let deltas = [];
424
- if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && this.opts.byDay.every((tok) => /^[A-Z]{2}$/.test(tok))) {
425
- deltas = this.opts.byDay.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
452
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
453
+ const hasOrdinalTokens = this.opts.byDay.some((tok) => /^[+-]?\d/.test(tok));
454
+ if (hasOrdinalTokens && this.opts.byMonth && (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY")) {
455
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
456
+ let foundFirst = false;
457
+ for (let year = zdt.year; year <= zdt.year + 10 && !foundFirst; year++) {
458
+ for (const month of months) {
459
+ if (year === zdt.year && month < zdt.month) continue;
460
+ const monthSample = zdt.with({ year, month, day: 1 });
461
+ const monthlyOccs = this.generateMonthlyOccurrences(monthSample);
462
+ for (const occ of monthlyOccs) {
463
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, zdt) >= 0) {
464
+ if (!occ.toPlainDate().equals(zdt.toPlainDate())) {
465
+ zdt = this.applyTimeOverride(occ.with({ hour: 0, minute: 0, second: 0 }));
466
+ } else {
467
+ zdt = occ;
468
+ }
469
+ foundFirst = true;
470
+ break;
471
+ }
472
+ }
473
+ if (foundFirst) break;
474
+ }
475
+ }
426
476
  } else {
427
- deltas = this.opts.byDay.map((tok) => {
428
- var _a2;
429
- const wdTok = (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
430
- return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
431
- }).filter((d) => d !== null);
432
- }
433
- if (deltas.length) {
434
- zdt = zdt.add({ days: Math.min(...deltas) });
477
+ let deltas;
478
+ if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && this.opts.byDay.every((tok) => /^[A-Z]{2}$/.test(tok))) {
479
+ deltas = this.opts.byDay.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
480
+ } else {
481
+ deltas = this.opts.byDay.map((tok) => {
482
+ var _a2;
483
+ const wdTok = (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
484
+ return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
485
+ }).filter((d) => d !== null);
486
+ }
487
+ if (deltas.length) {
488
+ zdt = zdt.add({ days: Math.min(...deltas) });
489
+ }
435
490
  }
436
491
  }
437
492
  const { byHour, byMinute, bySecond } = this.opts;
@@ -441,6 +496,18 @@ var RRuleTemporal = class _RRuleTemporal {
441
496
  ) > 0) {
442
497
  zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
443
498
  }
499
+ if (this.opts.freq === "MINUTELY" && !byMinute && import_polyfill.Temporal.ZonedDateTime.compare(
500
+ zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 }),
501
+ this.originalDtstart
502
+ ) > 0) {
503
+ zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
504
+ }
505
+ if (this.opts.freq === "SECONDLY" && ((_d = this.opts.byWeekNo) == null ? void 0 : _d.length) && !bySecond && import_polyfill.Temporal.ZonedDateTime.compare(
506
+ zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 }),
507
+ this.originalDtstart
508
+ ) > 0) {
509
+ zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
510
+ }
444
511
  if (byHour || byMinute || bySecond) {
445
512
  const candidates = this.expandByTime(zdt);
446
513
  for (const candidate of candidates) {
@@ -457,15 +524,7 @@ var RRuleTemporal = class _RRuleTemporal {
457
524
  matchesByDay(zdt) {
458
525
  const { byDay, freq } = this.opts;
459
526
  if (!byDay) return true;
460
- const dayMap = {
461
- MO: 1,
462
- TU: 2,
463
- WE: 3,
464
- TH: 4,
465
- FR: 5,
466
- SA: 6,
467
- SU: 7
468
- };
527
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
469
528
  for (const token of byDay) {
470
529
  const m = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
471
530
  if (!m) continue;
@@ -498,26 +557,57 @@ var RRuleTemporal = class _RRuleTemporal {
498
557
  if (!byMonth) return true;
499
558
  return byMonth.includes(zdt.month);
500
559
  }
560
+ matchesNumericConstraint(value, constraints, maxPositiveValue) {
561
+ return constraints.some((c) => {
562
+ const target = c > 0 ? c : maxPositiveValue + c + 1;
563
+ return value === target;
564
+ });
565
+ }
501
566
  matchesByMonthDay(zdt) {
502
567
  const { byMonthDay } = this.opts;
503
568
  if (!byMonthDay) return true;
504
569
  const lastDay = zdt.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
505
- return byMonthDay.some((d) => d > 0 ? zdt.day === d : zdt.day === lastDay + d + 1);
570
+ return this.matchesNumericConstraint(zdt.day, byMonthDay, lastDay);
571
+ }
572
+ matchesByHour(zdt) {
573
+ const { byHour } = this.opts;
574
+ if (!byHour) return true;
575
+ if (byHour.includes(zdt.hour)) {
576
+ return true;
577
+ }
578
+ for (const h of byHour) {
579
+ const intendedTime = zdt.with({ hour: h });
580
+ if (intendedTime.hour === zdt.hour) {
581
+ return true;
582
+ }
583
+ }
584
+ return false;
585
+ }
586
+ matchesByMinute(zdt) {
587
+ const { byMinute } = this.opts;
588
+ if (!byMinute) return true;
589
+ return byMinute.includes(zdt.minute);
590
+ }
591
+ matchesBySecond(zdt) {
592
+ const { bySecond } = this.opts;
593
+ if (!bySecond) return true;
594
+ return bySecond.includes(zdt.second);
506
595
  }
507
596
  matchesAll(zdt) {
508
- return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt) && this.matchesByYearDay(zdt) && this.matchesByWeekNo(zdt);
597
+ 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);
509
598
  }
510
599
  matchesByYearDay(zdt) {
511
600
  const { byYearDay } = this.opts;
512
601
  if (!byYearDay) return true;
513
602
  const dayOfYear = zdt.dayOfYear;
514
603
  const last = zdt.with({ month: 12, day: 31 }).dayOfYear;
515
- return byYearDay.some((d) => d > 0 ? dayOfYear === d : dayOfYear === last + d + 1);
604
+ return this.matchesNumericConstraint(dayOfYear, byYearDay, last);
516
605
  }
517
606
  getIsoWeekInfo(zdt) {
518
607
  const thursday = zdt.add({ days: 4 - zdt.dayOfWeek });
519
608
  const year = thursday.year;
520
- const firstThursday = thursday.with({ month: 1, day: 1 }).add({ days: (4 - thursday.with({ month: 1, day: 1 }).dayOfWeek + 7) % 7 });
609
+ const jan1 = zdt.with({ year, month: 1, day: 1 });
610
+ const firstThursday = jan1.add({ days: (4 - jan1.dayOfWeek + 7) % 7 });
521
611
  const diffDays = thursday.toPlainDate().since(firstThursday.toPlainDate()).days;
522
612
  const week = Math.floor(diffDays / 7) + 1;
523
613
  return { week, year };
@@ -527,11 +617,8 @@ var RRuleTemporal = class _RRuleTemporal {
527
617
  if (!byWeekNo) return true;
528
618
  const { week, year } = this.getIsoWeekInfo(zdt);
529
619
  const jan1 = zdt.with({ year, month: 1, day: 1 });
530
- const dec31 = zdt.with({ year, month: 12, day: 31 });
531
- let lastWeek = 52;
532
- if (jan1.dayOfWeek === 4 || dec31.dayOfWeek === 4) {
533
- lastWeek = 53;
534
- }
620
+ const isLeapYear = jan1.inLeapYear;
621
+ const lastWeek = jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52;
535
622
  return byWeekNo.some((wn) => {
536
623
  if (wn > 0) {
537
624
  return week === wn;
@@ -543,470 +630,498 @@ var RRuleTemporal = class _RRuleTemporal {
543
630
  options() {
544
631
  return this.opts;
545
632
  }
633
+ addDtstartIfNeeded(dates, iterator) {
634
+ if (this.includeDtstart && !this.matchesAll(this.originalDtstart)) {
635
+ if (iterator && !iterator(this.originalDtstart, dates.length)) {
636
+ return false;
637
+ }
638
+ dates.push(this.originalDtstart);
639
+ if (this.shouldBreakForCountLimit(dates.length)) {
640
+ return false;
641
+ }
642
+ }
643
+ return true;
644
+ }
645
+ processOccurrences(occs, dates, start, iterator, extraFilters) {
646
+ let shouldBreak = false;
647
+ for (const occ of occs) {
648
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
649
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
650
+ shouldBreak = true;
651
+ break;
652
+ }
653
+ if (extraFilters && !extraFilters(occ)) {
654
+ continue;
655
+ }
656
+ if (iterator && !iterator(occ, dates.length)) {
657
+ shouldBreak = true;
658
+ break;
659
+ }
660
+ dates.push(occ);
661
+ if (this.shouldBreakForCountLimit(dates.length)) {
662
+ shouldBreak = true;
663
+ break;
664
+ }
665
+ }
666
+ return { shouldBreak };
667
+ }
546
668
  /**
547
669
  * Returns all occurrences of the rule.
548
670
  * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
549
671
  * @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
550
672
  */
551
- all(iterator) {
552
- var _a;
553
- if (!this.opts.count && !this.opts.until && !iterator) {
554
- throw new Error("all() requires iterator when no COUNT/UNTIL");
555
- }
673
+ _allMonthlyByDayOrMonthDay(iterator) {
556
674
  const dates = [];
557
675
  let iterationCount = 0;
558
- if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
559
- const start = this.originalDtstart;
560
- let monthCursor = start.with({ day: 1 });
561
- let matchCount = 0;
562
- if (this.includeDtstart && !this.matchesAll(start)) {
563
- if (iterator && !iterator(start, matchCount)) {
564
- return this.applyCountLimitAndMergeRDates(dates, iterator);
565
- }
566
- dates.push(start);
567
- matchCount++;
568
- if (this.shouldBreakForCountLimit(matchCount)) {
569
- return this.applyCountLimitAndMergeRDates(dates, iterator);
570
- }
676
+ const start = this.originalDtstart;
677
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
678
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
679
+ }
680
+ let monthCursor = start.with({ day: 1 });
681
+ while (true) {
682
+ if (++iterationCount > this.maxIterations) {
683
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
571
684
  }
572
- outer_month: while (true) {
573
- if (++iterationCount > this.maxIterations) {
574
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
575
- }
576
- let occs = this.generateMonthlyOccurrences(monthCursor);
577
- occs = this.applyBySetPos(occs);
578
- 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)) {
579
- monthCursor = monthCursor.add({ months: this.opts.interval });
580
- continue outer_month;
581
- }
582
- for (const occ of occs) {
583
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
584
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
585
- break outer_month;
586
- }
587
- if (iterator && !iterator(occ, matchCount)) {
588
- break outer_month;
589
- }
590
- dates.push(occ);
591
- matchCount++;
592
- if (this.shouldBreakForCountLimit(matchCount)) {
593
- break outer_month;
594
- }
595
- }
685
+ let occs = this.generateMonthlyOccurrences(monthCursor);
686
+ occs = this.applyBySetPos(occs);
687
+ 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)) {
596
688
  monthCursor = monthCursor.add({ months: this.opts.interval });
689
+ continue;
597
690
  }
691
+ const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
692
+ if (shouldBreak) {
693
+ break;
694
+ }
695
+ monthCursor = monthCursor.add({ months: this.opts.interval });
696
+ }
697
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
698
+ }
699
+ _allWeekly(iterator) {
700
+ var _a;
701
+ const dates = [];
702
+ let iterationCount = 0;
703
+ const start = this.originalDtstart;
704
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
598
705
  return this.applyCountLimitAndMergeRDates(dates, iterator);
599
706
  }
600
- if (this.opts.freq === "WEEKLY") {
601
- if (this.opts.byYearDay && this.opts.byYearDay.length > 0) {
602
- const start2 = this.originalDtstart;
603
- if (this.includeDtstart && !this.matchesAll(start2)) {
604
- if (iterator && !iterator(start2, 0)) {
605
- return this.applyCountLimitAndMergeRDates(dates, iterator);
606
- }
607
- dates.push(start2);
608
- if (this.shouldBreakForCountLimit(1)) {
609
- return this.applyCountLimitAndMergeRDates(dates, iterator);
610
- }
611
- }
612
- let yearCursor = start2.with({ month: 1, day: 1 });
613
- let matchCount2 = dates.length;
614
- outer_year_for_weekly: while (true) {
615
- if (++iterationCount > this.maxIterations) {
616
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
617
- }
618
- const occs = this.generateYearlyOccurrences(yearCursor);
619
- for (const occ of occs) {
620
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start2) < 0) continue;
621
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
622
- break outer_year_for_weekly;
623
- }
624
- if (iterator && !iterator(occ, matchCount2)) {
625
- break outer_year_for_weekly;
626
- }
627
- dates.push(occ);
628
- matchCount2++;
629
- if (this.shouldBreakForCountLimit(matchCount2)) {
630
- break outer_year_for_weekly;
631
- }
632
- }
633
- if (this.opts.until && yearCursor.year > this.opts.until.year) {
634
- break;
635
- }
636
- yearCursor = yearCursor.add({ years: 1 });
637
- }
638
- return this.applyCountLimitAndMergeRDates(dates, iterator);
639
- }
640
- const start = this.originalDtstart;
641
- if (this.includeDtstart && !this.matchesAll(start)) {
642
- if (iterator && !iterator(start, 0)) {
643
- return this.applyCountLimitAndMergeRDates(dates, iterator);
644
- }
645
- dates.push(start);
646
- if (this.shouldBreakForCountLimit(1)) {
647
- return this.applyCountLimitAndMergeRDates(dates, iterator);
648
- }
707
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
708
+ 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]];
709
+ const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
710
+ const firstWeekDates = dows.map((dw) => {
711
+ const delta = (dw - start.dayOfWeek + 7) % 7;
712
+ return start.add({ days: delta });
713
+ });
714
+ const firstOccurrence = firstWeekDates.reduce((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
715
+ const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
716
+ const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
717
+ let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
718
+ while (true) {
719
+ if (++iterationCount > this.maxIterations) {
720
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
649
721
  }
650
- const dayMap = {
651
- MO: 1,
652
- TU: 2,
653
- WE: 3,
654
- TH: 4,
655
- FR: 5,
656
- SA: 6,
657
- SU: 7
658
- };
659
- 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]];
660
- const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
661
- const firstWeekDates = dows.map((dw) => {
662
- const delta = (dw - start.dayOfWeek + 7) % 7;
663
- return start.add({ days: delta });
664
- });
665
- let firstOccurrence = firstWeekDates.reduce((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
666
- const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
667
- const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
668
- let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
669
- let matchCount = 0;
670
- outer_week: while (true) {
671
- if (++iterationCount > this.maxIterations) {
672
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
673
- }
674
- let occs = dows.flatMap((dw) => {
675
- const delta = (dw - wkstDay + 7) % 7;
676
- const sameDate = weekCursor.add({ days: delta });
677
- return this.expandByTime(sameDate);
678
- }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
679
- occs = this.applyBySetPos(occs);
680
- for (const occ of occs) {
681
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
682
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
683
- break outer_week;
684
- }
685
- if (!this.matchesByMonth(occ)) {
686
- continue;
687
- }
688
- if (!this.matchesByMonthDay(occ)) {
689
- continue;
690
- }
691
- if (iterator && !iterator(occ, matchCount)) {
692
- break outer_week;
693
- }
694
- dates.push(occ);
695
- matchCount++;
696
- if (this.shouldBreakForCountLimit(matchCount)) {
697
- break outer_week;
698
- }
699
- }
700
- weekCursor = weekCursor.add({ weeks: this.opts.interval });
722
+ let occs = dows.flatMap((dw) => {
723
+ const delta = (dw - wkstDay + 7) % 7;
724
+ const sameDate = weekCursor.add({ days: delta });
725
+ return this.expandByTime(sameDate);
726
+ }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
727
+ occs = this.applyBySetPos(occs);
728
+ const { shouldBreak } = this.processOccurrences(
729
+ occs,
730
+ dates,
731
+ start,
732
+ iterator,
733
+ (occ) => this.matchesByMonth(occ) && this.matchesByMonthDay(occ)
734
+ );
735
+ if (shouldBreak) {
736
+ break;
701
737
  }
738
+ weekCursor = weekCursor.add({ weeks: this.opts.interval });
739
+ }
740
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
741
+ }
742
+ _allMonthlyByMonth(iterator) {
743
+ const dates = [];
744
+ let iterationCount = 0;
745
+ const start = this.originalDtstart;
746
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
702
747
  return this.applyCountLimitAndMergeRDates(dates, iterator);
703
748
  }
704
- if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay) {
705
- const start = this.originalDtstart;
706
- if (this.includeDtstart && !this.matchesAll(start)) {
707
- if (iterator && !iterator(start, 0)) {
708
- return this.applyCountLimitAndMergeRDates(dates, iterator);
709
- }
710
- dates.push(start);
711
- if (this.shouldBreakForCountLimit(1)) {
712
- return this.applyCountLimitAndMergeRDates(dates, iterator);
713
- }
749
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
750
+ let monthOffset = 0;
751
+ let startMonthIndex = months.findIndex((m) => m >= start.month);
752
+ if (startMonthIndex === -1) {
753
+ startMonthIndex = 0;
754
+ monthOffset = 1;
755
+ }
756
+ while (true) {
757
+ if (++iterationCount > this.maxIterations) {
758
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
714
759
  }
715
- const months = [...this.opts.byMonth].sort((a, b) => a - b);
716
- let monthOffset = 0;
717
- let matchCount = dates.length;
718
- let startMonthIndex = months.findIndex((m) => m >= start.month);
719
- if (startMonthIndex === -1) {
720
- startMonthIndex = 0;
721
- monthOffset = 1;
722
- }
723
- while (true) {
724
- if (++iterationCount > this.maxIterations) {
725
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
726
- }
727
- const monthIndex = startMonthIndex + monthOffset;
728
- const targetMonth = months[monthIndex % months.length];
729
- const yearsToAdd = Math.floor(monthIndex / months.length);
730
- const candidate = start.with({
731
- year: start.year + yearsToAdd,
732
- month: targetMonth
733
- });
734
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(candidate, this.opts.until) > 0) {
760
+ const monthIndex = startMonthIndex + monthOffset;
761
+ const targetMonth = months[monthIndex % months.length];
762
+ const yearsToAdd = Math.floor(monthIndex / months.length);
763
+ const candidate = start.with({
764
+ year: start.year + yearsToAdd,
765
+ month: targetMonth
766
+ });
767
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(candidate, this.opts.until) > 0) {
768
+ break;
769
+ }
770
+ if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
771
+ if (iterator && !iterator(candidate, dates.length)) {
735
772
  break;
736
773
  }
737
- if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
738
- if (iterator && !iterator(candidate, matchCount)) {
739
- break;
740
- }
741
- dates.push(candidate);
742
- matchCount++;
743
- if (this.shouldBreakForCountLimit(matchCount)) {
744
- break;
745
- }
774
+ dates.push(candidate);
775
+ if (this.shouldBreakForCountLimit(dates.length)) {
776
+ break;
746
777
  }
747
- monthOffset++;
748
778
  }
779
+ monthOffset++;
780
+ }
781
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
782
+ }
783
+ _allYearlyByMonth(iterator) {
784
+ const dates = [];
785
+ let iterationCount = 0;
786
+ const start = this.originalDtstart;
787
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
749
788
  return this.applyCountLimitAndMergeRDates(dates, iterator);
750
789
  }
751
- if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo) {
752
- const start = this.originalDtstart;
753
- if (this.includeDtstart && !this.matchesAll(start)) {
754
- if (iterator && !iterator(start, 0)) {
755
- return this.applyCountLimitAndMergeRDates(dates, iterator);
790
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
791
+ let yearOffset = 0;
792
+ while (true) {
793
+ if (++iterationCount > this.maxIterations) {
794
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
795
+ }
796
+ const year = start.year + yearOffset * this.opts.interval;
797
+ for (const month of months) {
798
+ let occ = start.with({ year, month });
799
+ occ = this.applyTimeOverride(occ);
800
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) {
801
+ continue;
756
802
  }
757
- dates.push(start);
758
- if (this.shouldBreakForCountLimit(1)) {
803
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
759
804
  return this.applyCountLimitAndMergeRDates(dates, iterator);
760
805
  }
761
- }
762
- const months = [...this.opts.byMonth].sort((a, b) => a - b);
763
- let yearOffset = 0;
764
- let matchCount = 0;
765
- while (true) {
766
- if (++iterationCount > this.maxIterations) {
767
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
806
+ if (iterator && !iterator(occ, dates.length)) {
807
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
768
808
  }
769
- const year = start.year + yearOffset * this.opts.interval;
770
- for (const month of months) {
771
- let occ = start.with({ year, month });
772
- occ = this.applyTimeOverride(occ);
773
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) {
774
- continue;
775
- }
776
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
777
- return dates;
778
- }
779
- if (iterator && !iterator(occ, matchCount)) {
780
- return dates;
781
- }
782
- dates.push(occ);
783
- matchCount++;
784
- if (this.shouldBreakForCountLimit(matchCount)) {
785
- return this.applyCountLimitAndMergeRDates(dates, iterator);
786
- }
809
+ dates.push(occ);
810
+ if (this.shouldBreakForCountLimit(dates.length)) {
811
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
787
812
  }
788
- yearOffset++;
789
813
  }
814
+ yearOffset++;
815
+ }
816
+ }
817
+ _allYearlyComplex(iterator) {
818
+ const dates = [];
819
+ let iterationCount = 0;
820
+ const start = this.originalDtstart;
821
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
790
822
  return this.applyCountLimitAndMergeRDates(dates, iterator);
791
823
  }
792
- if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
793
- const start = this.originalDtstart;
794
- if (this.includeDtstart && !this.matchesAll(start)) {
795
- if (iterator && !iterator(start, 0)) {
796
- return this.applyCountLimitAndMergeRDates(dates, iterator);
797
- }
798
- dates.push(start);
799
- if (this.shouldBreakForCountLimit(1)) {
800
- return this.applyCountLimitAndMergeRDates(dates, iterator);
801
- }
824
+ let yearCursor = start.with({ month: 1, day: 1 });
825
+ while (true) {
826
+ if (++iterationCount > this.maxIterations) {
827
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
802
828
  }
803
- let yearCursor = start.with({ month: 1, day: 1 });
804
- let matchCount = 0;
805
- outer_year: while (true) {
806
- if (++iterationCount > this.maxIterations) {
807
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
808
- }
809
- const occs = this.generateYearlyOccurrences(yearCursor);
810
- for (const occ of occs) {
811
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
812
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
813
- break outer_year;
814
- }
815
- if (iterator && !iterator(occ, matchCount)) {
816
- break outer_year;
817
- }
818
- dates.push(occ);
819
- matchCount++;
820
- if (this.shouldBreakForCountLimit(matchCount)) {
821
- break outer_year;
829
+ const occs = this.generateYearlyOccurrences(yearCursor);
830
+ const uniqueOccs = [];
831
+ if (occs.length > 0) {
832
+ uniqueOccs.push(occs[0]);
833
+ for (let i = 1; i < occs.length; i++) {
834
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occs[i], occs[i - 1]) !== 0) {
835
+ uniqueOccs.push(occs[i]);
822
836
  }
823
837
  }
824
- yearCursor = yearCursor.add({ years: this.opts.interval });
825
838
  }
839
+ const { shouldBreak } = this.processOccurrences(uniqueOccs, dates, start, iterator);
840
+ if (shouldBreak) {
841
+ break;
842
+ }
843
+ const interval = this.opts.freq === "WEEKLY" ? 1 : this.opts.interval;
844
+ yearCursor = yearCursor.add({ years: interval });
845
+ if (this.opts.freq === "WEEKLY" && this.opts.until && yearCursor.year > this.opts.until.year) {
846
+ break;
847
+ }
848
+ }
849
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
850
+ }
851
+ _allMinutelySecondlyComplex(iterator) {
852
+ const dates = [];
853
+ let iterationCount = 0;
854
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
826
855
  return this.applyCountLimitAndMergeRDates(dates, iterator);
827
856
  }
828
- if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
829
- const start = this.originalDtstart;
830
- if (this.includeDtstart && !this.matchesAll(start)) {
831
- if (iterator && !iterator(start, 0)) {
832
- return this.applyCountLimitAndMergeRDates(dates, iterator);
833
- }
834
- dates.push(start);
835
- if (this.shouldBreakForCountLimit(1)) {
836
- return this.applyCountLimitAndMergeRDates(dates, iterator);
837
- }
857
+ let current = this.computeFirst();
858
+ while (true) {
859
+ if (++iterationCount > this.maxIterations) {
860
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
861
+ }
862
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
863
+ break;
838
864
  }
839
- let yearCursor = start.with({ month: 1, day: 1 });
840
- let matchCount = 0;
841
- outer_year2: while (true) {
842
- if (++iterationCount > this.maxIterations) {
843
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
865
+ if (this.matchesAll(current)) {
866
+ if (iterator && !iterator(current, dates.length)) {
867
+ break;
844
868
  }
845
- const occs = this.generateYearlyOccurrences(yearCursor);
846
- for (const occ of occs) {
847
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
848
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
849
- break outer_year2;
850
- }
851
- if (iterator && !iterator(occ, matchCount)) {
852
- break outer_year2;
853
- }
854
- dates.push(occ);
855
- matchCount++;
856
- if (this.shouldBreakForCountLimit(matchCount)) {
857
- break outer_year2;
858
- }
869
+ dates.push(current);
870
+ if (this.shouldBreakForCountLimit(dates.length)) {
871
+ break;
859
872
  }
860
- yearCursor = yearCursor.add({ years: this.opts.interval });
873
+ current = this.nextCandidateSameDate(current);
874
+ } else {
875
+ current = this.findNextValidDate(current);
861
876
  }
877
+ }
878
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
879
+ }
880
+ _allMonthlyByWeekNo(iterator) {
881
+ const dates = [];
882
+ let iterationCount = 0;
883
+ const start = this.originalDtstart;
884
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
862
885
  return this.applyCountLimitAndMergeRDates(dates, iterator);
863
886
  }
864
- if (this.opts.freq === "MONTHLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 && !this.opts.byDay && !this.opts.byMonthDay) {
865
- const start = this.originalDtstart;
866
- let matchCount = 0;
867
- if (this.includeDtstart && !this.matchesAll(start)) {
868
- if (iterator && !iterator(start, matchCount)) {
869
- return this.applyCountLimitAndMergeRDates(dates, iterator);
870
- }
871
- dates.push(start);
872
- matchCount++;
873
- if (this.shouldBreakForCountLimit(matchCount)) {
874
- return this.applyCountLimitAndMergeRDates(dates, iterator);
875
- }
887
+ let current = start;
888
+ const weekNos = [...this.opts.byWeekNo].sort((a, b) => a - b);
889
+ const interval = this.opts.interval;
890
+ let monthsAdvanced = 0;
891
+ let lastYearProcessed = -1;
892
+ outer_loop: while (true) {
893
+ if (this.shouldBreakForCountLimit(dates.length)) {
894
+ break;
876
895
  }
877
- let year = start.year;
878
- const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
879
- const interval = this.opts.interval;
880
- const startMonthAbs = start.year * 12 + start.month;
881
- outer_loop: while (true) {
882
- if (this.shouldBreakForCountLimit(matchCount)) {
883
- break;
884
- }
885
- if (++iterationCount > this.maxIterations) {
886
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
887
- }
888
- const yearStart = start.with({ year, month: 1, day: 1 });
889
- const lastDayOfYear = yearStart.with({ month: 12, day: 31 }).dayOfYear;
890
- for (const yd of yearDays) {
891
- const dayNum = yd > 0 ? yd : lastDayOfYear + yd + 1;
892
- if (dayNum <= 0 || dayNum > lastDayOfYear) continue;
893
- const baseOcc = yearStart.add({ days: dayNum - 1 });
894
- for (const occ of this.expandByTime(baseOcc)) {
895
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
896
- if (dates.some((d) => import_polyfill.Temporal.ZonedDateTime.compare(d, occ) === 0)) continue;
897
- const occMonthAbs = occ.year * 12 + occ.month;
898
- if ((occMonthAbs - startMonthAbs) % interval !== 0) {
899
- continue;
900
- }
901
- if (!this.matchesByMonth(occ)) {
902
- continue;
903
- }
904
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
905
- break outer_loop;
906
- }
907
- if (iterator && !iterator(occ, matchCount)) {
908
- break outer_loop;
909
- }
910
- dates.push(occ);
911
- matchCount++;
912
- if (this.shouldBreakForCountLimit(matchCount)) {
913
- break outer_loop;
896
+ if (++iterationCount > this.maxIterations) {
897
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
898
+ }
899
+ const year = current.year;
900
+ if (year !== lastYearProcessed && current.month >= start.month) {
901
+ lastYearProcessed = year;
902
+ for (const weekNo of weekNos) {
903
+ const occs = this.generateOccurrencesForWeekInYear(year, weekNo);
904
+ for (const occ of occs) {
905
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) >= 0) {
906
+ if (iterator && !iterator(occ, dates.length)) {
907
+ break outer_loop;
908
+ }
909
+ dates.push(occ);
910
+ if (this.shouldBreakForCountLimit(dates.length)) {
911
+ break outer_loop;
912
+ }
914
913
  }
915
914
  }
916
915
  }
917
- year++;
918
- if (this.opts.until && year > this.opts.until.year + 2) {
919
- break;
920
- }
921
- if (!this.opts.until && this.opts.count) {
922
- const yearsToScan = Math.ceil(this.opts.count / (this.opts.byYearDay.length || 1)) * interval + 5;
923
- if (year > start.year + yearsToScan) {
924
- break;
925
- }
926
- }
927
916
  }
917
+ monthsAdvanced += interval;
918
+ current = start.add({ months: monthsAdvanced });
919
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
920
+ break;
921
+ }
922
+ }
923
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
924
+ }
925
+ _allMonthlyByYearDay(iterator) {
926
+ const dates = [];
927
+ let iterationCount = 0;
928
+ const start = this.originalDtstart;
929
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
928
930
  return this.applyCountLimitAndMergeRDates(dates, iterator);
929
931
  }
930
- if (this.opts.freq === "DAILY" && this.opts.bySetPos) {
931
- let start = this.originalDtstart;
932
- let matchCount = 0;
933
- if (this.includeDtstart && !this.matchesAll(start)) {
934
- if (iterator && !iterator(start, matchCount)) {
935
- return this.applyCountLimitAndMergeRDates(dates, iterator);
936
- }
937
- dates.push(start);
938
- matchCount++;
939
- if (this.shouldBreakForCountLimit(matchCount)) {
940
- return this.applyCountLimitAndMergeRDates(dates, iterator);
941
- }
932
+ let year = start.year;
933
+ const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
934
+ const interval = this.opts.interval;
935
+ const startMonthAbs = start.year * 12 + start.month;
936
+ outer_loop: while (true) {
937
+ if (this.shouldBreakForCountLimit(dates.length)) {
938
+ break;
942
939
  }
943
- let dayCursor = start.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
944
- outer_daily: while (true) {
945
- if (++iterationCount > this.maxIterations) {
946
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
947
- }
948
- let dayOccs = this.expandByTime(dayCursor);
949
- dayOccs = dayOccs.filter((occ) => this.matchesAll(occ));
950
- dayOccs = this.applyBySetPos(dayOccs);
951
- for (const occ of dayOccs) {
940
+ if (++iterationCount > this.maxIterations) {
941
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
942
+ }
943
+ const yearStart = start.with({ year, month: 1, day: 1 });
944
+ const lastDayOfYear = yearStart.with({ month: 12, day: 31 }).dayOfYear;
945
+ for (const yd of yearDays) {
946
+ const dayNum = yd > 0 ? yd : lastDayOfYear + yd + 1;
947
+ if (dayNum <= 0 || dayNum > lastDayOfYear) continue;
948
+ const baseOcc = yearStart.add({ days: dayNum - 1 });
949
+ for (const occ of this.expandByTime(baseOcc)) {
952
950
  if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
951
+ if (dates.some((d) => import_polyfill.Temporal.ZonedDateTime.compare(d, occ) === 0)) continue;
952
+ const occMonthAbs = occ.year * 12 + occ.month;
953
+ if ((occMonthAbs - startMonthAbs) % interval !== 0) {
954
+ continue;
955
+ }
956
+ if (!this.matchesByMonth(occ)) {
957
+ continue;
958
+ }
953
959
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
954
- break outer_daily;
960
+ break outer_loop;
955
961
  }
956
- if (iterator && !iterator(occ, matchCount)) {
957
- break outer_daily;
962
+ if (iterator && !iterator(occ, dates.length)) {
963
+ break outer_loop;
958
964
  }
959
965
  dates.push(occ);
960
- matchCount++;
961
- if (this.shouldBreakForCountLimit(matchCount)) {
962
- break outer_daily;
966
+ if (this.shouldBreakForCountLimit(dates.length)) {
967
+ break outer_loop;
963
968
  }
964
969
  }
965
- dayCursor = dayCursor.add({ days: this.opts.interval });
966
970
  }
967
- } else {
968
- let current = this.computeFirst();
969
- let matchCount = 0;
970
- if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
971
- if (iterator && !iterator(this.originalDtstart, matchCount)) {
972
- return this.applyCountLimitAndMergeRDates(dates, iterator);
973
- }
974
- dates.push(this.originalDtstart);
975
- matchCount++;
976
- if (this.shouldBreakForCountLimit(matchCount)) {
977
- return this.applyCountLimitAndMergeRDates(dates, iterator);
978
- }
971
+ year++;
972
+ if (this.opts.until && year > this.opts.until.year + 2) {
973
+ break;
979
974
  }
980
- while (true) {
981
- if (++iterationCount > this.maxIterations) {
982
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
975
+ if (!this.opts.until && this.opts.count) {
976
+ const yearsToScan = Math.ceil(this.opts.count / (this.opts.byYearDay.length || 1)) * interval + 5;
977
+ if (year > start.year + yearsToScan) {
978
+ break;
983
979
  }
984
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
980
+ }
981
+ }
982
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
983
+ }
984
+ _allDailyMinutelyHourlyWithBySetPos(iterator) {
985
+ const dates = [];
986
+ let iterationCount = 0;
987
+ const start = this.originalDtstart;
988
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
989
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
990
+ }
991
+ let cursor;
992
+ let duration;
993
+ switch (this.opts.freq) {
994
+ case "MINUTELY":
995
+ cursor = start.with({ second: 0, microsecond: 0, nanosecond: 0 });
996
+ duration = { minutes: this.opts.interval };
997
+ break;
998
+ case "HOURLY":
999
+ cursor = start.with({ minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
1000
+ duration = { hours: this.opts.interval };
1001
+ break;
1002
+ case "DAILY":
1003
+ cursor = start.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
1004
+ duration = { days: this.opts.interval };
1005
+ break;
1006
+ default:
1007
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
1008
+ }
1009
+ while (true) {
1010
+ if (++iterationCount > this.maxIterations) {
1011
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1012
+ }
1013
+ let periodOccs = this.expandByTime(cursor);
1014
+ periodOccs = periodOccs.filter((occ) => this.matchesAll(occ));
1015
+ periodOccs = this.applyBySetPos(periodOccs);
1016
+ const { shouldBreak } = this.processOccurrences(periodOccs, dates, start, iterator);
1017
+ if (shouldBreak) {
1018
+ break;
1019
+ }
1020
+ cursor = cursor.add(duration);
1021
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(cursor, this.opts.until) > 0) {
1022
+ break;
1023
+ }
1024
+ }
1025
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
1026
+ }
1027
+ _allFallback(iterator) {
1028
+ const dates = [];
1029
+ let iterationCount = 0;
1030
+ let current = this.computeFirst();
1031
+ if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
1032
+ if (iterator && !iterator(this.originalDtstart, dates.length)) {
1033
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
1034
+ }
1035
+ dates.push(this.originalDtstart);
1036
+ if (this.shouldBreakForCountLimit(dates.length)) {
1037
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
1038
+ }
1039
+ }
1040
+ while (true) {
1041
+ if (++iterationCount > this.maxIterations) {
1042
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
1043
+ }
1044
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
1045
+ break;
1046
+ }
1047
+ if (this.matchesAll(current)) {
1048
+ if (iterator && !iterator(current, dates.length)) {
985
1049
  break;
986
1050
  }
987
- if (this.matchesAll(current)) {
988
- if (iterator && !iterator(current, matchCount)) {
989
- break;
990
- }
991
- dates.push(current);
992
- matchCount++;
993
- if (this.shouldBreakForCountLimit(matchCount)) {
994
- break;
995
- }
1051
+ dates.push(current);
1052
+ if (this.shouldBreakForCountLimit(dates.length)) {
1053
+ break;
996
1054
  }
997
- current = this.nextCandidateSameDate(current);
998
1055
  }
1056
+ current = this.nextCandidateSameDate(current);
999
1057
  }
1000
1058
  return this.applyCountLimitAndMergeRDates(dates, iterator);
1001
1059
  }
1060
+ /**
1061
+ * Returns all occurrences of the rule.
1062
+ * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
1063
+ * @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
1064
+ */
1065
+ all(iterator) {
1066
+ if (this.opts.byWeekNo && this.opts.byYearDay) {
1067
+ const yearStart = this.originalDtstart.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 });
1068
+ const yearDays = this.opts.byYearDay.map((yd) => {
1069
+ const lastDayOfYear = yearStart.with({ month: 12, day: 31 }).dayOfYear;
1070
+ return yd > 0 ? yd : lastDayOfYear + yd + 1;
1071
+ });
1072
+ let possibleDate = false;
1073
+ for (const yd of yearDays) {
1074
+ const date = yearStart.add({ days: yd - 1 });
1075
+ if (this.matchesByWeekNo(date)) {
1076
+ possibleDate = true;
1077
+ break;
1078
+ }
1079
+ }
1080
+ if (!possibleDate) {
1081
+ return [];
1082
+ }
1083
+ }
1084
+ if (!this.opts.count && !this.opts.until && !iterator) {
1085
+ throw new Error("all() requires iterator when no COUNT/UNTIL");
1086
+ }
1087
+ if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay) && !this.opts.byWeekNo) {
1088
+ return this._allMonthlyByDayOrMonthDay(iterator);
1089
+ }
1090
+ if (this.opts.freq === "WEEKLY" && !(this.opts.byYearDay && this.opts.byYearDay.length > 0) && !(this.opts.byWeekNo && this.opts.byWeekNo.length > 0)) {
1091
+ return this._allWeekly(iterator);
1092
+ }
1093
+ if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay) {
1094
+ return this._allMonthlyByMonth(iterator);
1095
+ }
1096
+ if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo) {
1097
+ return this._allYearlyByMonth(iterator);
1098
+ }
1099
+ 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) {
1100
+ return this._allYearlyComplex(iterator);
1101
+ }
1102
+ if ((this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") && (this.opts.byMonth || this.opts.byWeekNo || this.opts.byYearDay || this.opts.byMonthDay || this.opts.byDay)) {
1103
+ return this._allMinutelySecondlyComplex(iterator);
1104
+ }
1105
+ if (this.opts.freq === "MONTHLY" && this.opts.byWeekNo && this.opts.byWeekNo.length > 0) {
1106
+ return this._allMonthlyByWeekNo(iterator);
1107
+ }
1108
+ if (this.opts.freq === "MONTHLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 && !this.opts.byDay && !this.opts.byMonthDay) {
1109
+ return this._allMonthlyByYearDay(iterator);
1110
+ }
1111
+ if ((this.opts.freq === "MINUTELY" || this.opts.freq === "HOURLY" || this.opts.freq === "DAILY") && this.opts.bySetPos) {
1112
+ return this._allDailyMinutelyHourlyWithBySetPos(iterator);
1113
+ }
1114
+ return this._allFallback(iterator);
1115
+ }
1002
1116
  /**
1003
1117
  * Converts rDate entries to ZonedDateTime and merges with existing dates.
1004
1118
  * @param dates - Array of dates to merge with
1005
1119
  * @returns Merged and deduplicated array of dates
1006
1120
  */
1007
1121
  mergeAndDeduplicateRDates(dates) {
1008
- if (!this.opts.rDate) return dates;
1009
- dates.push(...this.opts.rDate);
1122
+ if (this.opts.rDate) {
1123
+ dates.push(...this.opts.rDate);
1124
+ }
1010
1125
  dates.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1011
1126
  const dedup = [];
1012
1127
  for (const d of dates) {
@@ -1153,8 +1268,7 @@ var RRuleTemporal = class _RRuleTemporal {
1153
1268
  if (interval !== 1) rule.push(`INTERVAL=${interval}`);
1154
1269
  if (count !== void 0) rule.push(`COUNT=${count}`);
1155
1270
  if (until) {
1156
- const u = until.toInstant().toString().replace(/[-:]/g, "");
1157
- rule.push(`UNTIL=${u.slice(0, 15)}Z`);
1271
+ rule.push(`UNTIL=${this.formatIcsDateTime(until)}`);
1158
1272
  }
1159
1273
  if (byHour) rule.push(`BYHOUR=${byHour.join(",")}`);
1160
1274
  if (byMinute) rule.push(`BYMINUTE=${byMinute.join(",")}`);
@@ -1175,8 +1289,11 @@ var RRuleTemporal = class _RRuleTemporal {
1175
1289
  }
1176
1290
  return lines.join("\n");
1177
1291
  }
1292
+ formatIcsDateTime(date) {
1293
+ return date.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z";
1294
+ }
1178
1295
  joinDates(dates) {
1179
- return dates.map((d) => d.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z");
1296
+ return dates.map((d) => this.formatIcsDateTime(d));
1180
1297
  }
1181
1298
  /**
1182
1299
  * Given any date in a month, return all the ZonedDateTimes in that month
@@ -1196,22 +1313,12 @@ var RRuleTemporal = class _RRuleTemporal {
1196
1313
  return [];
1197
1314
  }
1198
1315
  const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
1199
- let expanded2 = dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1200
- return expanded2;
1316
+ return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1201
1317
  }
1202
1318
  if (!byDay) {
1203
- const expanded2 = this.expandByTime(sample);
1204
- return expanded2;
1205
- }
1206
- const dayMap = {
1207
- MO: 1,
1208
- TU: 2,
1209
- WE: 3,
1210
- TH: 4,
1211
- FR: 5,
1212
- SA: 6,
1213
- SU: 7
1214
- };
1319
+ return this.expandByTime(sample);
1320
+ }
1321
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1215
1322
  const tokens = byDay.map((tok) => {
1216
1323
  const m = tok.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
1217
1324
  if (!m) return null;
@@ -1244,8 +1351,7 @@ var RRuleTemporal = class _RRuleTemporal {
1244
1351
  finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
1245
1352
  }
1246
1353
  const hits = finalDays.map((d) => sample.with({ day: d }));
1247
- let expanded = hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1248
- return expanded;
1354
+ return hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1249
1355
  }
1250
1356
  /**
1251
1357
  * Given any date in a year, return all ZonedDateTimes in that year matching
@@ -1253,20 +1359,11 @@ var RRuleTemporal = class _RRuleTemporal {
1253
1359
  * if BYMONTH is not specified.
1254
1360
  */
1255
1361
  generateYearlyOccurrences(sample) {
1256
- var _a;
1257
- 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];
1362
+ 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];
1258
1363
  let occs = [];
1259
1364
  const hasOrdinalByDay = this.opts.byDay && this.opts.byDay.some((t) => /^[+-]?\d/.test(t));
1260
1365
  if (hasOrdinalByDay && !this.opts.byMonth) {
1261
- const dayMap = {
1262
- MO: 1,
1263
- TU: 2,
1264
- WE: 3,
1265
- TH: 4,
1266
- FR: 5,
1267
- SA: 6,
1268
- SU: 7
1269
- };
1366
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1270
1367
  for (const tok of this.opts.byDay) {
1271
1368
  const m = tok.match(/^([+-]?\d{1,2})(MO|TU|WE|TH|FR|SA|SU)$/);
1272
1369
  if (!m) continue;
@@ -1294,49 +1391,176 @@ var RRuleTemporal = class _RRuleTemporal {
1294
1391
  const last = sample.with({ month: 12, day: 31 }).dayOfYear;
1295
1392
  for (const d of this.opts.byYearDay) {
1296
1393
  const dayNum = d > 0 ? d : last + d + 1;
1297
- const dt = sample.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1394
+ if (dayNum <= 0 || dayNum > last) continue;
1395
+ 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 });
1298
1396
  if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
1299
1397
  occs.push(...this.expandByTime(dt));
1300
1398
  }
1301
1399
  }
1302
1400
  }
1303
1401
  if (this.opts.byWeekNo) {
1304
- const dayMap = {
1305
- MO: 1,
1306
- TU: 2,
1307
- WE: 3,
1308
- TH: 4,
1309
- FR: 5,
1310
- SA: 6,
1311
- SU: 7
1312
- };
1313
- const wkst = dayMap[this.opts.wkst || "MO"];
1314
- const jan1 = sample.with({ month: 1, day: 1 });
1315
- const delta = (jan1.dayOfWeek - wkst + 7) % 7;
1316
- const firstWeekStart = jan1.subtract({ days: delta });
1317
- const lastWeekDiff = sample.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
1318
- const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
1319
- const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
1320
- var _a2;
1321
- return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
1322
- }) : [this.opts.wkst || "MO"];
1402
+ const { lastWeek, firstWeekStart, tokens } = this.isoWeekByDay(sample);
1323
1403
  for (const weekNo of this.opts.byWeekNo) {
1404
+ if (weekNo > 0 && weekNo > lastWeek || weekNo < 0 && -weekNo > lastWeek) {
1405
+ continue;
1406
+ }
1324
1407
  const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1325
1408
  const weekStart = firstWeekStart.add({ weeks: weekIndex });
1326
- for (const tok of tokens) {
1327
- if (!tok) continue;
1328
- const targetDow = dayMap[tok];
1329
- const inst = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
1330
- if (!this.opts.byMonth || this.opts.byMonth.includes(inst.month)) {
1331
- occs.push(...this.expandByTime(inst));
1332
- }
1333
- }
1409
+ occs.push(...this.addByDay(tokens, weekStart));
1334
1410
  }
1335
1411
  }
1336
1412
  occs = occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1337
1413
  occs = this.applyBySetPos(occs);
1338
1414
  return occs;
1339
1415
  }
1416
+ addByDay(tokens, weekStart) {
1417
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1418
+ const wkst = dayMap[this.opts.wkst || "MO"];
1419
+ const entries = [];
1420
+ for (const tok of tokens) {
1421
+ if (!tok) continue;
1422
+ const targetDow = dayMap[tok];
1423
+ const inst = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
1424
+ if (!this.opts.byMonth || this.opts.byMonth.includes(inst.month)) {
1425
+ entries.push(...this.expandByTime(inst));
1426
+ }
1427
+ }
1428
+ return entries;
1429
+ }
1430
+ /**
1431
+ * Helper to find the next valid value from a sorted array
1432
+ */
1433
+ findNextValidValue(currentValue, validValues, compare) {
1434
+ return validValues.find((v) => compare(v, currentValue) > 0) || null;
1435
+ }
1436
+ /**
1437
+ * Efficiently find the next valid date for MINUTELY and SECONDLY frequency by jumping over
1438
+ * large gaps when BYXXX constraints don't match.
1439
+ */
1440
+ findNextValidDate(current) {
1441
+ if (this.opts.byWeekNo && this.opts.byYearDay) {
1442
+ const yearStart = current.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 });
1443
+ const yearDays = this.opts.byYearDay.map((yd) => {
1444
+ const lastDayOfYear = yearStart.with({ month: 12, day: 31 }).dayOfYear;
1445
+ return yd > 0 ? yd : lastDayOfYear + yd + 1;
1446
+ });
1447
+ for (const yd of yearDays) {
1448
+ const date = yearStart.add({ days: yd - 1 });
1449
+ if (this.matchesByWeekNo(date)) {
1450
+ break;
1451
+ }
1452
+ }
1453
+ }
1454
+ if (this.opts.byMonth && !this.opts.byMonth.includes(current.month)) {
1455
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
1456
+ const nextMonth = this.findNextValidValue(current.month, months, (a, b) => a - b);
1457
+ if (nextMonth) {
1458
+ current = current.with({ month: nextMonth, day: 1, hour: 0, minute: 0, second: 0 });
1459
+ } else {
1460
+ current = current.add({ years: 1 }).with({ month: months[0], day: 1, hour: 0, minute: 0, second: 0 });
1461
+ }
1462
+ current = this.applyTimeOverride(current);
1463
+ return current;
1464
+ }
1465
+ if (this.opts.byWeekNo && !this.matchesByWeekNo(current)) {
1466
+ current = current.add({ weeks: 1 }).with({ hour: 0, minute: 0, second: 0 });
1467
+ current = this.applyTimeOverride(current);
1468
+ return current;
1469
+ }
1470
+ if (this.opts.byYearDay && !this.matchesByYearDay(current)) {
1471
+ const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
1472
+ const currentYearDay = current.dayOfYear;
1473
+ const lastDayOfYear = current.with({ month: 12, day: 31 }).dayOfYear;
1474
+ let nextYearDay = yearDays.find((d) => {
1475
+ const dayNum = d > 0 ? d : lastDayOfYear + d + 1;
1476
+ return dayNum > currentYearDay;
1477
+ });
1478
+ if (nextYearDay) {
1479
+ const dayNum = nextYearDay > 0 ? nextYearDay : lastDayOfYear + nextYearDay + 1;
1480
+ if (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") {
1481
+ current = current.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }).add({ days: dayNum - 1 });
1482
+ } else {
1483
+ current = current.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1484
+ }
1485
+ } else {
1486
+ const nextYear = current.add({ years: 1 });
1487
+ const nextYearLastDay = nextYear.with({ month: 12, day: 31 }).dayOfYear;
1488
+ const firstYearDay = yearDays[0];
1489
+ if (firstYearDay !== void 0) {
1490
+ const dayNum = firstYearDay > 0 ? firstYearDay : nextYearLastDay + firstYearDay + 1;
1491
+ if (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") {
1492
+ current = nextYear.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }).add({ days: dayNum - 1 });
1493
+ } else {
1494
+ current = nextYear.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1495
+ }
1496
+ }
1497
+ }
1498
+ current = this.applyTimeOverride(current);
1499
+ return current;
1500
+ }
1501
+ if (this.opts.byMonthDay && !this.matchesByMonthDay(current)) {
1502
+ const monthDays = [...this.opts.byMonthDay].sort((a, b) => a - b);
1503
+ const lastDayOfMonth = current.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
1504
+ const currentDay = current.day;
1505
+ const validDays = monthDays.map((d) => d > 0 ? d : lastDayOfMonth + d + 1).filter((d) => d > 0 && d <= lastDayOfMonth).sort((a, b) => a - b);
1506
+ const nextDay = this.findNextValidValue(currentDay, validDays, (a, b) => a - b);
1507
+ if (nextDay) {
1508
+ current = current.with({ day: nextDay, hour: 0, minute: 0, second: 0 });
1509
+ } else {
1510
+ const nextMonth = current.add({ months: 1 }).with({ day: 1 });
1511
+ const nextMonthLastDay = nextMonth.add({ months: 1 }).subtract({ days: 1 }).day;
1512
+ const firstMonthDay = monthDays[0];
1513
+ if (firstMonthDay !== void 0) {
1514
+ const dayNum = firstMonthDay > 0 ? firstMonthDay : nextMonthLastDay + firstMonthDay + 1;
1515
+ current = nextMonth.with({
1516
+ day: Math.max(1, Math.min(dayNum, nextMonthLastDay)),
1517
+ hour: 0,
1518
+ minute: 0,
1519
+ second: 0
1520
+ });
1521
+ } else {
1522
+ current = current.add({ months: 1 }).with({ day: 1, hour: 0, minute: 0, second: 0 });
1523
+ }
1524
+ }
1525
+ current = this.applyTimeOverride(current);
1526
+ return current;
1527
+ }
1528
+ if (this.opts.byDay && !this.matchesByDay(current)) {
1529
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1530
+ const targetDays = this.opts.byDay.map((tok) => {
1531
+ var _a;
1532
+ return (_a = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a[1];
1533
+ }).filter(Boolean).map((day) => dayMap[day]).filter(Boolean);
1534
+ const nextDayOfWeek = this.findNextValidValue(current.dayOfWeek, targetDays.sort(), (a, b) => a - b);
1535
+ if (nextDayOfWeek) {
1536
+ const delta = (nextDayOfWeek - current.dayOfWeek + 7) % 7;
1537
+ current = current.add({ days: delta }).with({ hour: 0, minute: 0, second: 0 });
1538
+ } else {
1539
+ const delta = (targetDays[0] - current.dayOfWeek + 7) % 7;
1540
+ current = current.add({ days: delta + 7 }).with({ hour: 0, minute: 0, second: 0 });
1541
+ }
1542
+ current = this.applyTimeOverride(current);
1543
+ return current;
1544
+ }
1545
+ switch (this.opts.freq) {
1546
+ case "SECONDLY":
1547
+ case "MINUTELY":
1548
+ current = current.add({ days: 1 }).with({ hour: 0, minute: 0, second: 0 });
1549
+ break;
1550
+ case "HOURLY":
1551
+ current = current.add({ days: 1 }).with({ hour: 0, minute: 0, second: 0 });
1552
+ break;
1553
+ case "DAILY":
1554
+ case "WEEKLY":
1555
+ current = current.add({ months: 1 }).with({ day: 1, hour: 0, minute: 0, second: 0 });
1556
+ break;
1557
+ case "MONTHLY":
1558
+ case "YEARLY":
1559
+ current = current.add({ years: 1 }).with({ month: 1, day: 1, hour: 0, minute: 0, second: 0 });
1560
+ break;
1561
+ }
1562
+ return this.applyTimeOverride(current);
1563
+ }
1340
1564
  applyBySetPos(list) {
1341
1565
  const { bySetPos } = this.opts;
1342
1566
  if (!bySetPos || !bySetPos.length) return list;
@@ -1349,6 +1573,37 @@ var RRuleTemporal = class _RRuleTemporal {
1349
1573
  }
1350
1574
  return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1351
1575
  }
1576
+ isoWeekByDay(sample) {
1577
+ var _a;
1578
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1579
+ const wkst = dayMap[this.opts.wkst || "MO"];
1580
+ const jan1 = sample.with({ month: 1, day: 1 });
1581
+ const jan4 = sample.with({ month: 1, day: 4 });
1582
+ const delta = (jan4.dayOfWeek - wkst + 7) % 7;
1583
+ const firstWeekStart = jan4.subtract({ days: delta });
1584
+ const isLeapYear = jan1.inLeapYear;
1585
+ const lastWeek = jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52;
1586
+ const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
1587
+ var _a2;
1588
+ return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
1589
+ }) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
1590
+ return { lastWeek, firstWeekStart, tokens };
1591
+ }
1592
+ /**
1593
+ * Generate occurrences for a specific week number in a given year
1594
+ */
1595
+ generateOccurrencesForWeekInYear(year, weekNo) {
1596
+ const occs = [];
1597
+ const sample = this.originalDtstart.with({ year, month: 1, day: 1 });
1598
+ const { lastWeek, firstWeekStart, tokens } = this.isoWeekByDay(sample);
1599
+ if (weekNo > 0 && weekNo > lastWeek || weekNo < 0 && -weekNo > lastWeek) {
1600
+ return occs;
1601
+ }
1602
+ const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1603
+ const weekStart = firstWeekStart.add({ weeks: weekIndex });
1604
+ occs.push(...this.addByDay(tokens, weekStart));
1605
+ return occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1606
+ }
1352
1607
  };
1353
1608
  // Annotate the CommonJS export names for ESM import in node:
1354
1609
  0 && (module.exports = {