rrule-temporal 1.1.2 → 1.1.3

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
@@ -43,21 +43,37 @@ var import_polyfill = require("@js-temporal/polyfill");
43
43
  function unfoldLine(foldedLine) {
44
44
  return foldedLine.replace(/\r?\n[ \t]/g, "");
45
45
  }
46
+ function parseIcsDateTime(dateStr, tzid, valueType) {
47
+ const isDate = valueType === "DATE" || !dateStr.includes("T");
48
+ if (isDate) {
49
+ const isoDate = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}`;
50
+ return import_polyfill.Temporal.PlainDate.from(isoDate).toZonedDateTime({ timeZone: tzid });
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`;
54
+ return import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
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);
58
+ }
59
+ }
46
60
  function parseDateValues(dateValues, tzid, valueType) {
47
61
  const dates = [];
48
62
  for (const dateValue of dateValues) {
49
- const isDate = valueType === "DATE" || !dateValue.includes("T");
50
- if (isDate) {
51
- const isoDate = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}`;
52
- dates.push(import_polyfill.Temporal.PlainDate.from(isoDate).toZonedDateTime({ timeZone: tzid }));
53
- } else {
54
- if (/Z$/.test(dateValue)) {
55
- const iso = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9, 15)}Z`;
56
- dates.push(import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC"));
57
- } else {
58
- const isoDate = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9)}`;
59
- dates.push(import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid));
60
- }
63
+ dates.push(parseIcsDateTime(dateValue, tzid, valueType));
64
+ }
65
+ return dates;
66
+ }
67
+ function parseDateLines(lines, linePrefix, defaultTzid) {
68
+ const dates = [];
69
+ const regex = new RegExp(`^${linePrefix}(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)`, "i");
70
+ for (const line of lines) {
71
+ const match = line.match(regex);
72
+ if (match) {
73
+ const [, valueType, tzid, dateValuesStr] = match;
74
+ const timezone = tzid || defaultTzid;
75
+ const dateValues = dateValuesStr.split(",");
76
+ dates.push(...parseDateValues(dateValues, timezone, valueType));
61
77
  }
62
78
  }
63
79
  return dates;
@@ -80,26 +96,10 @@ function parseRRuleString(input, targetTimezone) {
80
96
  if (!dtMatch) throw new Error("Invalid DTSTART in ICS snippet");
81
97
  const [, valueType, dtTzid, dtValue] = dtMatch;
82
98
  tzid = (_a = dtTzid != null ? dtTzid : targetTimezone) != null ? _a : tzid;
83
- dtstart = parseDateValues([dtValue], tzid, valueType)[0];
99
+ dtstart = parseIcsDateTime(dtValue, tzid, valueType);
84
100
  rruleLine = rrLine;
85
- for (const exLine of exLines) {
86
- const exMatch = exLine.match(/EXDATE(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)/i);
87
- if (exMatch) {
88
- const [, valueType2, exTzid, dateValuesStr] = exMatch;
89
- const exTimezone = exTzid || tzid;
90
- const dateValues = dateValuesStr.split(",");
91
- exDate.push(...parseDateValues(dateValues, exTimezone, valueType2));
92
- }
93
- }
94
- for (const rLine of rLines) {
95
- const rMatch = rLine.match(/RDATE(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)/i);
96
- if (rMatch) {
97
- const [, valueType2, rTzid, dateValuesStr] = rMatch;
98
- const rTimezone = rTzid || tzid;
99
- const dateValues = dateValuesStr.split(",");
100
- rDate.push(...parseDateValues(dateValues, rTimezone, valueType2));
101
- }
102
- }
101
+ exDate = parseDateLines(exLines, "EXDATE", tzid);
102
+ rDate = parseDateLines(rLines, "RDATE", tzid);
103
103
  } else {
104
104
  throw new Error("dtstart required when parsing RRULE alone");
105
105
  }
@@ -124,15 +124,9 @@ function parseRRuleString(input, targetTimezone) {
124
124
  opts.count = parseInt(val, 10);
125
125
  break;
126
126
  case "UNTIL": {
127
- if (/Z$/.test(val)) {
128
- const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}Z`;
129
- opts.until = import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
130
- } else {
131
- const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}`;
132
- opts.until = import_polyfill.Temporal.PlainDateTime.from(iso).toZonedDateTime(tzid || "UTC");
133
- if (tzid !== "UTC") {
134
- throw new Error("UNTIL rule part MUST always be specified as a date with UTC time");
135
- }
127
+ opts.until = parseIcsDateTime(val, tzid || "UTC");
128
+ if (!/Z$/.test(val) && tzid !== "UTC") {
129
+ throw new Error("UNTIL rule part MUST always be specified as a date with UTC time");
136
130
  }
137
131
  break;
138
132
  }
@@ -200,46 +194,30 @@ var RRuleTemporal = class _RRuleTemporal {
200
194
  this.maxIterations = (_d = manual.maxIterations) != null ? _d : 1e4;
201
195
  this.includeDtstart = (_e = manual.includeDtstart) != null ? _e : false;
202
196
  }
197
+ sanitizeNumericArray(arr, min, max, allowZero = false, sort = false) {
198
+ if (!arr) return void 0;
199
+ const sanitized = arr.filter((n) => Number.isInteger(n) && n >= min && n <= max && (allowZero || n !== 0));
200
+ if (sanitized.length === 0) return void 0;
201
+ return sort ? sanitized.sort((a, b) => a - b) : sanitized;
202
+ }
203
203
  sanitizeOpts(opts) {
204
204
  const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
205
205
  if (opts.byDay) {
206
206
  opts.byDay = opts.byDay.filter((d) => validDay.test(d));
207
207
  if (opts.byDay.length === 0) delete opts.byDay;
208
208
  }
209
- if (opts.byMonth) {
210
- opts.byMonth = opts.byMonth.filter((n) => Number.isInteger(n) && n >= 1 && n <= 12);
211
- if (opts.byMonth.length === 0) delete opts.byMonth;
212
- }
213
- if (opts.byMonthDay) {
214
- opts.byMonthDay = opts.byMonthDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -31 && n <= 31);
215
- if (opts.byMonthDay.length === 0) delete opts.byMonthDay;
216
- }
217
- if (opts.byYearDay) {
218
- opts.byYearDay = opts.byYearDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -366 && n <= 366);
219
- if (opts.byYearDay.length === 0) delete opts.byYearDay;
220
- }
221
- if (opts.byWeekNo) {
222
- opts.byWeekNo = opts.byWeekNo.filter((n) => Number.isInteger(n) && n !== 0 && n >= -53 && n <= 53);
223
- if (opts.byWeekNo.length === 0) delete opts.byWeekNo;
224
- }
225
- if (opts.byHour) {
226
- opts.byHour = opts.byHour.filter((n) => Number.isInteger(n) && n >= 0 && n <= 23).sort((a, b) => a - b);
227
- if (opts.byHour.length === 0) delete opts.byHour;
228
- }
229
- if (opts.byMinute) {
230
- opts.byMinute = opts.byMinute.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
231
- if (opts.byMinute.length === 0) delete opts.byMinute;
232
- }
233
- if (opts.bySecond) {
234
- opts.bySecond = opts.bySecond.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
235
- if (opts.bySecond.length === 0) delete opts.bySecond;
236
- }
209
+ opts.byMonth = this.sanitizeNumericArray(opts.byMonth, 1, 12, false, false);
210
+ opts.byMonthDay = this.sanitizeNumericArray(opts.byMonthDay, -31, 31, false, false);
211
+ opts.byYearDay = this.sanitizeNumericArray(opts.byYearDay, -366, 366, false, false);
212
+ opts.byWeekNo = this.sanitizeNumericArray(opts.byWeekNo, -53, 53, false, false);
213
+ opts.byHour = this.sanitizeNumericArray(opts.byHour, 0, 23, true, true);
214
+ opts.byMinute = this.sanitizeNumericArray(opts.byMinute, 0, 59, true, true);
215
+ opts.bySecond = this.sanitizeNumericArray(opts.bySecond, 0, 59, true, true);
237
216
  if (opts.bySetPos) {
238
217
  if (opts.bySetPos.some((p) => p === 0)) {
239
218
  throw new Error("bySetPos may not contain 0");
240
219
  }
241
- opts.bySetPos = opts.bySetPos.filter((n) => Number.isInteger(n) && n !== 0);
242
- if (opts.bySetPos.length === 0) delete opts.bySetPos;
220
+ opts.bySetPos = this.sanitizeNumericArray(opts.bySetPos, -Infinity, Infinity, false, false);
243
221
  }
244
222
  return opts;
245
223
  }
@@ -254,8 +232,14 @@ var RRuleTemporal = class _RRuleTemporal {
254
232
  return zdt.add({ months: interval });
255
233
  case "YEARLY":
256
234
  return zdt.add({ years: interval });
257
- case "HOURLY":
258
- return zdt.add({ hours: interval });
235
+ case "HOURLY": {
236
+ const originalHour = zdt.hour;
237
+ let next = zdt.add({ hours: interval });
238
+ if (next.hour === originalHour && interval === 1) {
239
+ next = next.add({ hours: interval });
240
+ }
241
+ return next;
242
+ }
259
243
  case "MINUTELY":
260
244
  return zdt.add({ minutes: interval });
261
245
  case "SECONDLY":
@@ -291,30 +275,55 @@ var RRuleTemporal = class _RRuleTemporal {
291
275
  if (freq === "MINUTELY" && byMinute && byMinute.length === 1) {
292
276
  return this.applyTimeOverride(zdt.add({ hours: interval }));
293
277
  }
294
- if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute && interval > 1) {
278
+ if (bySecond && bySecond.length > 1) {
279
+ const idx = bySecond.indexOf(zdt.second);
280
+ if (idx !== -1 && idx < bySecond.length - 1) {
281
+ return zdt.with({ second: bySecond[idx + 1] });
282
+ }
283
+ }
284
+ if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute) {
295
285
  const next = zdt.add({ minutes: interval });
296
286
  if (byHour.includes(next.hour)) {
297
- return next;
287
+ return next.with({ second: bySecond ? bySecond[0] : zdt.second });
298
288
  }
299
289
  const nextHour = byHour.find((h) => h > zdt.hour) || byHour[0];
300
290
  if (nextHour && nextHour > zdt.hour) {
301
- return zdt.with({ hour: nextHour, minute: 0 });
291
+ return zdt.with({ hour: nextHour, minute: 0, second: bySecond ? bySecond[0] : zdt.second });
302
292
  }
303
293
  return this.applyTimeOverride(zdt.add({ days: 1 }));
304
294
  }
305
- if (freq === "SECONDLY" && bySecond && bySecond.length === 1) {
306
- return this.applyTimeOverride(zdt.add({ minutes: interval })).with({ second: bySecond[0] });
307
- }
308
- if (freq === "SECONDLY" && byMinute && byMinute.length === 1) {
309
- const next = zdt.add({ seconds: interval });
310
- if (next.minute === byMinute[0]) return next;
311
- return this.applyTimeOverride(zdt.add({ hours: interval })).with({ second: 0 });
312
- }
313
- if (bySecond && bySecond.length > 1) {
314
- const idx = bySecond.indexOf(zdt.second);
315
- if (idx !== -1 && idx < bySecond.length - 1) {
316
- return zdt.with({ second: bySecond[idx + 1] });
295
+ if (freq === "SECONDLY") {
296
+ let next = zdt.add({ seconds: interval });
297
+ 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] });
303
+ }
304
+ }
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 }));
315
+ }
316
+ }
317
317
  }
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 }));
324
+ }
325
+ }
326
+ return next;
318
327
  }
319
328
  if (byMinute && byMinute.length > 1) {
320
329
  const idx = byMinute.indexOf(zdt.minute);
@@ -324,6 +333,24 @@ var RRuleTemporal = class _RRuleTemporal {
324
333
  second: bySecond ? bySecond[0] : zdt.second
325
334
  });
326
335
  }
336
+ if (freq === "MINUTELY" && idx === byMinute.length - 1) {
337
+ if (byHour && byHour.length > 0) {
338
+ const currentHourIdx = byHour.indexOf(zdt.hour);
339
+ if (currentHourIdx !== -1 && currentHourIdx < byHour.length - 1) {
340
+ return zdt.with({
341
+ hour: byHour[currentHourIdx + 1],
342
+ minute: byMinute[0],
343
+ second: bySecond ? bySecond[0] : zdt.second
344
+ });
345
+ } else {
346
+ return this.applyTimeOverride(zdt.add({ days: 1 }));
347
+ }
348
+ }
349
+ return zdt.add({ hours: interval }).with({
350
+ minute: byMinute[0],
351
+ second: bySecond ? bySecond[0] : zdt.second
352
+ });
353
+ }
327
354
  }
328
355
  if (byHour && byHour.length > 1) {
329
356
  const idx = byHour.indexOf(zdt.hour);
@@ -335,6 +362,9 @@ var RRuleTemporal = class _RRuleTemporal {
335
362
  });
336
363
  }
337
364
  }
365
+ if (freq === "HOURLY" && byHour && byHour.length > 1) {
366
+ return this.applyTimeOverride(zdt.add({ days: 1 }));
367
+ }
338
368
  return this.applyTimeOverride(this.rawAdvance(zdt));
339
369
  }
340
370
  applyTimeOverride(zdt) {
@@ -346,7 +376,7 @@ var RRuleTemporal = class _RRuleTemporal {
346
376
  return dt;
347
377
  }
348
378
  computeFirst() {
349
- var _a, _b, _c;
379
+ var _a, _b, _c, _d;
350
380
  let zdt = this.originalDtstart;
351
381
  if (((_a = this.opts.byWeekNo) == null ? void 0 : _a.length) && ["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq)) {
352
382
  let targetWeek = this.opts.byWeekNo[0];
@@ -378,15 +408,7 @@ var RRuleTemporal = class _RRuleTemporal {
378
408
  weekStart = firstThursday.subtract({ days: 3 }).add({ weeks: lastWeek + targetWeek });
379
409
  }
380
410
  if ((_b = this.opts.byDay) == null ? void 0 : _b.length) {
381
- const dayMap = {
382
- MO: 1,
383
- TU: 2,
384
- WE: 3,
385
- TH: 4,
386
- FR: 5,
387
- SA: 6,
388
- SU: 7
389
- };
411
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
390
412
  const targetDays = this.opts.byDay.map((tok) => {
391
413
  var _a2;
392
414
  return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
@@ -413,27 +435,44 @@ var RRuleTemporal = class _RRuleTemporal {
413
435
  }
414
436
  }
415
437
  if (((_c = this.opts.byDay) == null ? void 0 : _c.length) && !this.opts.byWeekNo) {
416
- const dayMap = {
417
- MO: 1,
418
- TU: 2,
419
- WE: 3,
420
- TH: 4,
421
- FR: 5,
422
- SA: 6,
423
- SU: 7
424
- };
425
- let deltas = [];
426
- if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && this.opts.byDay.every((tok) => /^[A-Z]{2}$/.test(tok))) {
427
- deltas = this.opts.byDay.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
438
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
439
+ const hasOrdinalTokens = this.opts.byDay.some((tok) => /^[+-]?\d/.test(tok));
440
+ if (hasOrdinalTokens && this.opts.byMonth && (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY")) {
441
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
442
+ let foundFirst = false;
443
+ for (let year = zdt.year; year <= zdt.year + 10 && !foundFirst; year++) {
444
+ for (const month of months) {
445
+ if (year === zdt.year && month < zdt.month) continue;
446
+ const monthSample = zdt.with({ year, month, day: 1 });
447
+ const monthlyOccs = this.generateMonthlyOccurrences(monthSample);
448
+ for (const occ of monthlyOccs) {
449
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, zdt) >= 0) {
450
+ if (!occ.toPlainDate().equals(zdt.toPlainDate())) {
451
+ zdt = this.applyTimeOverride(occ.with({ hour: 0, minute: 0, second: 0 }));
452
+ } else {
453
+ zdt = occ;
454
+ }
455
+ foundFirst = true;
456
+ break;
457
+ }
458
+ }
459
+ if (foundFirst) break;
460
+ }
461
+ }
428
462
  } else {
429
- deltas = this.opts.byDay.map((tok) => {
430
- var _a2;
431
- const wdTok = (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
432
- return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
433
- }).filter((d) => d !== null);
434
- }
435
- if (deltas.length) {
436
- zdt = zdt.add({ days: Math.min(...deltas) });
463
+ let deltas = [];
464
+ if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && this.opts.byDay.every((tok) => /^[A-Z]{2}$/.test(tok))) {
465
+ deltas = this.opts.byDay.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
466
+ } else {
467
+ deltas = this.opts.byDay.map((tok) => {
468
+ var _a2;
469
+ const wdTok = (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
470
+ return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
471
+ }).filter((d) => d !== null);
472
+ }
473
+ if (deltas.length) {
474
+ zdt = zdt.add({ days: Math.min(...deltas) });
475
+ }
437
476
  }
438
477
  }
439
478
  const { byHour, byMinute, bySecond } = this.opts;
@@ -443,6 +482,18 @@ var RRuleTemporal = class _RRuleTemporal {
443
482
  ) > 0) {
444
483
  zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
445
484
  }
485
+ if (this.opts.freq === "MINUTELY" && !byMinute && import_polyfill.Temporal.ZonedDateTime.compare(
486
+ zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 }),
487
+ this.originalDtstart
488
+ ) > 0) {
489
+ zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
490
+ }
491
+ if (this.opts.freq === "SECONDLY" && ((_d = this.opts.byWeekNo) == null ? void 0 : _d.length) && !bySecond && import_polyfill.Temporal.ZonedDateTime.compare(
492
+ zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 }),
493
+ this.originalDtstart
494
+ ) > 0) {
495
+ zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
496
+ }
446
497
  if (byHour || byMinute || bySecond) {
447
498
  const candidates = this.expandByTime(zdt);
448
499
  for (const candidate of candidates) {
@@ -459,15 +510,7 @@ var RRuleTemporal = class _RRuleTemporal {
459
510
  matchesByDay(zdt) {
460
511
  const { byDay, freq } = this.opts;
461
512
  if (!byDay) return true;
462
- const dayMap = {
463
- MO: 1,
464
- TU: 2,
465
- WE: 3,
466
- TH: 4,
467
- FR: 5,
468
- SA: 6,
469
- SU: 7
470
- };
513
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
471
514
  for (const token of byDay) {
472
515
  const m = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
473
516
  if (!m) continue;
@@ -500,26 +543,57 @@ var RRuleTemporal = class _RRuleTemporal {
500
543
  if (!byMonth) return true;
501
544
  return byMonth.includes(zdt.month);
502
545
  }
546
+ matchesNumericConstraint(value, constraints, maxPositiveValue) {
547
+ return constraints.some((c) => {
548
+ const target = c > 0 ? c : maxPositiveValue + c + 1;
549
+ return value === target;
550
+ });
551
+ }
503
552
  matchesByMonthDay(zdt) {
504
553
  const { byMonthDay } = this.opts;
505
554
  if (!byMonthDay) return true;
506
555
  const lastDay = zdt.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
507
- return byMonthDay.some((d) => d > 0 ? zdt.day === d : zdt.day === lastDay + d + 1);
556
+ return this.matchesNumericConstraint(zdt.day, byMonthDay, lastDay);
557
+ }
558
+ matchesByHour(zdt) {
559
+ const { byHour } = this.opts;
560
+ if (!byHour) return true;
561
+ if (byHour.includes(zdt.hour)) {
562
+ return true;
563
+ }
564
+ for (const h of byHour) {
565
+ const intendedTime = zdt.with({ hour: h });
566
+ if (intendedTime.hour === zdt.hour) {
567
+ return true;
568
+ }
569
+ }
570
+ return false;
571
+ }
572
+ matchesByMinute(zdt) {
573
+ const { byMinute } = this.opts;
574
+ if (!byMinute) return true;
575
+ return byMinute.includes(zdt.minute);
576
+ }
577
+ matchesBySecond(zdt) {
578
+ const { bySecond } = this.opts;
579
+ if (!bySecond) return true;
580
+ return bySecond.includes(zdt.second);
508
581
  }
509
582
  matchesAll(zdt) {
510
- return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt) && this.matchesByYearDay(zdt) && this.matchesByWeekNo(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);
511
584
  }
512
585
  matchesByYearDay(zdt) {
513
586
  const { byYearDay } = this.opts;
514
587
  if (!byYearDay) return true;
515
588
  const dayOfYear = zdt.dayOfYear;
516
589
  const last = zdt.with({ month: 12, day: 31 }).dayOfYear;
517
- return byYearDay.some((d) => d > 0 ? dayOfYear === d : dayOfYear === last + d + 1);
590
+ return this.matchesNumericConstraint(dayOfYear, byYearDay, last);
518
591
  }
519
592
  getIsoWeekInfo(zdt) {
520
593
  const thursday = zdt.add({ days: 4 - zdt.dayOfWeek });
521
594
  const year = thursday.year;
522
- const firstThursday = thursday.with({ month: 1, day: 1 }).add({ days: (4 - thursday.with({ month: 1, day: 1 }).dayOfWeek + 7) % 7 });
595
+ const jan1 = zdt.with({ year, month: 1, day: 1 });
596
+ const firstThursday = jan1.add({ days: (4 - jan1.dayOfWeek + 7) % 7 });
523
597
  const diffDays = thursday.toPlainDate().since(firstThursday.toPlainDate()).days;
524
598
  const week = Math.floor(diffDays / 7) + 1;
525
599
  return { week, year };
@@ -545,6 +619,41 @@ var RRuleTemporal = class _RRuleTemporal {
545
619
  options() {
546
620
  return this.opts;
547
621
  }
622
+ addDtstartIfNeeded(dates, iterator) {
623
+ if (this.includeDtstart && !this.matchesAll(this.originalDtstart)) {
624
+ if (iterator && !iterator(this.originalDtstart, dates.length)) {
625
+ return false;
626
+ }
627
+ dates.push(this.originalDtstart);
628
+ if (this.shouldBreakForCountLimit(dates.length)) {
629
+ return false;
630
+ }
631
+ }
632
+ return true;
633
+ }
634
+ processOccurrences(occs, dates, start, iterator, extraFilters) {
635
+ let shouldBreak = false;
636
+ for (const occ of occs) {
637
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
638
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
639
+ shouldBreak = true;
640
+ break;
641
+ }
642
+ if (extraFilters && !extraFilters(occ)) {
643
+ continue;
644
+ }
645
+ if (iterator && !iterator(occ, dates.length)) {
646
+ shouldBreak = true;
647
+ break;
648
+ }
649
+ dates.push(occ);
650
+ if (this.shouldBreakForCountLimit(dates.length)) {
651
+ shouldBreak = true;
652
+ break;
653
+ }
654
+ }
655
+ return { shouldBreak };
656
+ }
548
657
  /**
549
658
  * Returns all occurrences of the rule.
550
659
  * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
@@ -559,19 +668,11 @@ var RRuleTemporal = class _RRuleTemporal {
559
668
  let iterationCount = 0;
560
669
  if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
561
670
  const start = this.originalDtstart;
562
- let monthCursor = start.with({ day: 1 });
563
- let matchCount = 0;
564
- if (this.includeDtstart && !this.matchesAll(start)) {
565
- if (iterator && !iterator(start, matchCount)) {
566
- return this.applyCountLimitAndMergeRDates(dates, iterator);
567
- }
568
- dates.push(start);
569
- matchCount++;
570
- if (this.shouldBreakForCountLimit(matchCount)) {
571
- return this.applyCountLimitAndMergeRDates(dates, iterator);
572
- }
671
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
672
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
573
673
  }
574
- outer_month: while (true) {
674
+ let monthCursor = start.with({ day: 1 });
675
+ while (true) {
575
676
  if (++iterationCount > this.maxIterations) {
576
677
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
577
678
  }
@@ -579,85 +680,22 @@ var RRuleTemporal = class _RRuleTemporal {
579
680
  occs = this.applyBySetPos(occs);
580
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)) {
581
682
  monthCursor = monthCursor.add({ months: this.opts.interval });
582
- continue outer_month;
683
+ continue;
583
684
  }
584
- for (const occ of occs) {
585
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
586
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
587
- break outer_month;
588
- }
589
- if (iterator && !iterator(occ, matchCount)) {
590
- break outer_month;
591
- }
592
- dates.push(occ);
593
- matchCount++;
594
- if (this.shouldBreakForCountLimit(matchCount)) {
595
- break outer_month;
596
- }
685
+ const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
686
+ if (shouldBreak) {
687
+ break;
597
688
  }
598
689
  monthCursor = monthCursor.add({ months: this.opts.interval });
599
690
  }
600
691
  return this.applyCountLimitAndMergeRDates(dates, iterator);
601
692
  }
602
- if (this.opts.freq === "WEEKLY") {
603
- if (this.opts.byYearDay && this.opts.byYearDay.length > 0) {
604
- const start2 = this.originalDtstart;
605
- if (this.includeDtstart && !this.matchesAll(start2)) {
606
- if (iterator && !iterator(start2, 0)) {
607
- return this.applyCountLimitAndMergeRDates(dates, iterator);
608
- }
609
- dates.push(start2);
610
- if (this.shouldBreakForCountLimit(1)) {
611
- return this.applyCountLimitAndMergeRDates(dates, iterator);
612
- }
613
- }
614
- let yearCursor = start2.with({ month: 1, day: 1 });
615
- let matchCount2 = dates.length;
616
- outer_year_for_weekly: while (true) {
617
- if (++iterationCount > this.maxIterations) {
618
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
619
- }
620
- const occs = this.generateYearlyOccurrences(yearCursor);
621
- for (const occ of occs) {
622
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start2) < 0) continue;
623
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
624
- break outer_year_for_weekly;
625
- }
626
- if (iterator && !iterator(occ, matchCount2)) {
627
- break outer_year_for_weekly;
628
- }
629
- dates.push(occ);
630
- matchCount2++;
631
- if (this.shouldBreakForCountLimit(matchCount2)) {
632
- break outer_year_for_weekly;
633
- }
634
- }
635
- if (this.opts.until && yearCursor.year > this.opts.until.year) {
636
- break;
637
- }
638
- yearCursor = yearCursor.add({ years: 1 });
639
- }
640
- return this.applyCountLimitAndMergeRDates(dates, iterator);
641
- }
693
+ if (this.opts.freq === "WEEKLY" && !(this.opts.byYearDay && this.opts.byYearDay.length > 0)) {
642
694
  const start = this.originalDtstart;
643
- if (this.includeDtstart && !this.matchesAll(start)) {
644
- if (iterator && !iterator(start, 0)) {
645
- return this.applyCountLimitAndMergeRDates(dates, iterator);
646
- }
647
- dates.push(start);
648
- if (this.shouldBreakForCountLimit(1)) {
649
- return this.applyCountLimitAndMergeRDates(dates, iterator);
650
- }
695
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
696
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
651
697
  }
652
- const dayMap = {
653
- MO: 1,
654
- TU: 2,
655
- WE: 3,
656
- TH: 4,
657
- FR: 5,
658
- SA: 6,
659
- SU: 7
660
- };
698
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
661
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]];
662
700
  const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
663
701
  const firstWeekDates = dows.map((dw) => {
@@ -668,8 +706,7 @@ var RRuleTemporal = class _RRuleTemporal {
668
706
  const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
669
707
  const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
670
708
  let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
671
- let matchCount = 0;
672
- outer_week: while (true) {
709
+ while (true) {
673
710
  if (++iterationCount > this.maxIterations) {
674
711
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
675
712
  }
@@ -679,25 +716,15 @@ var RRuleTemporal = class _RRuleTemporal {
679
716
  return this.expandByTime(sameDate);
680
717
  }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
681
718
  occs = this.applyBySetPos(occs);
682
- for (const occ of occs) {
683
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
684
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
685
- break outer_week;
686
- }
687
- if (!this.matchesByMonth(occ)) {
688
- continue;
689
- }
690
- if (!this.matchesByMonthDay(occ)) {
691
- continue;
692
- }
693
- if (iterator && !iterator(occ, matchCount)) {
694
- break outer_week;
695
- }
696
- dates.push(occ);
697
- matchCount++;
698
- if (this.shouldBreakForCountLimit(matchCount)) {
699
- break outer_week;
700
- }
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;
701
728
  }
702
729
  weekCursor = weekCursor.add({ weeks: this.opts.interval });
703
730
  }
@@ -705,18 +732,11 @@ var RRuleTemporal = class _RRuleTemporal {
705
732
  }
706
733
  if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay) {
707
734
  const start = this.originalDtstart;
708
- if (this.includeDtstart && !this.matchesAll(start)) {
709
- if (iterator && !iterator(start, 0)) {
710
- return this.applyCountLimitAndMergeRDates(dates, iterator);
711
- }
712
- dates.push(start);
713
- if (this.shouldBreakForCountLimit(1)) {
714
- return this.applyCountLimitAndMergeRDates(dates, iterator);
715
- }
735
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
736
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
716
737
  }
717
738
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
718
739
  let monthOffset = 0;
719
- let matchCount = dates.length;
720
740
  let startMonthIndex = months.findIndex((m) => m >= start.month);
721
741
  if (startMonthIndex === -1) {
722
742
  startMonthIndex = 0;
@@ -737,12 +757,11 @@ var RRuleTemporal = class _RRuleTemporal {
737
757
  break;
738
758
  }
739
759
  if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
740
- if (iterator && !iterator(candidate, matchCount)) {
760
+ if (iterator && !iterator(candidate, dates.length)) {
741
761
  break;
742
762
  }
743
763
  dates.push(candidate);
744
- matchCount++;
745
- if (this.shouldBreakForCountLimit(matchCount)) {
764
+ if (this.shouldBreakForCountLimit(dates.length)) {
746
765
  break;
747
766
  }
748
767
  }
@@ -752,18 +771,11 @@ var RRuleTemporal = class _RRuleTemporal {
752
771
  }
753
772
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo) {
754
773
  const start = this.originalDtstart;
755
- if (this.includeDtstart && !this.matchesAll(start)) {
756
- if (iterator && !iterator(start, 0)) {
757
- return this.applyCountLimitAndMergeRDates(dates, iterator);
758
- }
759
- dates.push(start);
760
- if (this.shouldBreakForCountLimit(1)) {
761
- return this.applyCountLimitAndMergeRDates(dates, iterator);
762
- }
774
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
775
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
763
776
  }
764
777
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
765
778
  let yearOffset = 0;
766
- let matchCount = 0;
767
779
  while (true) {
768
780
  if (++iterationCount > this.maxIterations) {
769
781
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
@@ -778,12 +790,11 @@ var RRuleTemporal = class _RRuleTemporal {
778
790
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
779
791
  return dates;
780
792
  }
781
- if (iterator && !iterator(occ, matchCount)) {
793
+ if (iterator && !iterator(occ, dates.length)) {
782
794
  return dates;
783
795
  }
784
796
  dates.push(occ);
785
- matchCount++;
786
- if (this.shouldBreakForCountLimit(matchCount)) {
797
+ if (this.shouldBreakForCountLimit(dates.length)) {
787
798
  return this.applyCountLimitAndMergeRDates(dates, iterator);
788
799
  }
789
800
  }
@@ -791,97 +802,67 @@ var RRuleTemporal = class _RRuleTemporal {
791
802
  }
792
803
  return this.applyCountLimitAndMergeRDates(dates, iterator);
793
804
  }
794
- if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
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) {
795
806
  const start = this.originalDtstart;
796
- if (this.includeDtstart && !this.matchesAll(start)) {
797
- if (iterator && !iterator(start, 0)) {
798
- return this.applyCountLimitAndMergeRDates(dates, iterator);
799
- }
800
- dates.push(start);
801
- if (this.shouldBreakForCountLimit(1)) {
802
- return this.applyCountLimitAndMergeRDates(dates, iterator);
803
- }
807
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
808
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
804
809
  }
805
810
  let yearCursor = start.with({ month: 1, day: 1 });
806
- let matchCount = 0;
807
- outer_year: while (true) {
811
+ while (true) {
808
812
  if (++iterationCount > this.maxIterations) {
809
813
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
810
814
  }
811
815
  const occs = this.generateYearlyOccurrences(yearCursor);
812
- for (const occ of occs) {
813
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
814
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
815
- break outer_year;
816
- }
817
- if (iterator && !iterator(occ, matchCount)) {
818
- break outer_year;
819
- }
820
- dates.push(occ);
821
- matchCount++;
822
- if (this.shouldBreakForCountLimit(matchCount)) {
823
- break outer_year;
824
- }
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) {
823
+ break;
825
824
  }
826
- yearCursor = yearCursor.add({ years: this.opts.interval });
827
825
  }
828
826
  return this.applyCountLimitAndMergeRDates(dates, iterator);
829
827
  }
830
- if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
831
- const start = this.originalDtstart;
832
- if (this.includeDtstart && !this.matchesAll(start)) {
833
- if (iterator && !iterator(start, 0)) {
834
- return this.applyCountLimitAndMergeRDates(dates, iterator);
835
- }
836
- dates.push(start);
837
- if (this.shouldBreakForCountLimit(1)) {
838
- return this.applyCountLimitAndMergeRDates(dates, iterator);
839
- }
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)) {
830
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
840
831
  }
841
- let yearCursor = start.with({ month: 1, day: 1 });
842
- let matchCount = 0;
843
- outer_year2: while (true) {
832
+ let current = this.computeFirst();
833
+ while (true) {
844
834
  if (++iterationCount > this.maxIterations) {
845
835
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
846
836
  }
847
- const occs = this.generateYearlyOccurrences(yearCursor);
848
- for (const occ of occs) {
849
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
850
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
851
- break outer_year2;
852
- }
853
- if (iterator && !iterator(occ, matchCount)) {
854
- break outer_year2;
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;
855
843
  }
856
- dates.push(occ);
857
- matchCount++;
858
- if (this.shouldBreakForCountLimit(matchCount)) {
859
- break outer_year2;
844
+ dates.push(current);
845
+ if (this.shouldBreakForCountLimit(dates.length)) {
846
+ break;
860
847
  }
848
+ current = this.nextCandidateSameDate(current);
849
+ } else {
850
+ current = this.findNextValidDate(current);
861
851
  }
862
- yearCursor = yearCursor.add({ years: this.opts.interval });
863
852
  }
864
853
  return this.applyCountLimitAndMergeRDates(dates, iterator);
865
854
  }
866
855
  if (this.opts.freq === "MONTHLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 && !this.opts.byDay && !this.opts.byMonthDay) {
867
856
  const start = this.originalDtstart;
868
- let matchCount = 0;
869
- if (this.includeDtstart && !this.matchesAll(start)) {
870
- if (iterator && !iterator(start, matchCount)) {
871
- return this.applyCountLimitAndMergeRDates(dates, iterator);
872
- }
873
- dates.push(start);
874
- matchCount++;
875
- if (this.shouldBreakForCountLimit(matchCount)) {
876
- return this.applyCountLimitAndMergeRDates(dates, iterator);
877
- }
857
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
858
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
878
859
  }
879
860
  let year = start.year;
880
861
  const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
881
862
  const interval = this.opts.interval;
882
863
  const startMonthAbs = start.year * 12 + start.month;
883
864
  outer_loop: while (true) {
884
- if (this.shouldBreakForCountLimit(matchCount)) {
865
+ if (this.shouldBreakForCountLimit(dates.length)) {
885
866
  break;
886
867
  }
887
868
  if (++iterationCount > this.maxIterations) {
@@ -906,12 +887,11 @@ var RRuleTemporal = class _RRuleTemporal {
906
887
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
907
888
  break outer_loop;
908
889
  }
909
- if (iterator && !iterator(occ, matchCount)) {
890
+ if (iterator && !iterator(occ, dates.length)) {
910
891
  break outer_loop;
911
892
  }
912
893
  dates.push(occ);
913
- matchCount++;
914
- if (this.shouldBreakForCountLimit(matchCount)) {
894
+ if (this.shouldBreakForCountLimit(dates.length)) {
915
895
  break outer_loop;
916
896
  }
917
897
  }
@@ -929,53 +909,53 @@ var RRuleTemporal = class _RRuleTemporal {
929
909
  }
930
910
  return this.applyCountLimitAndMergeRDates(dates, iterator);
931
911
  }
932
- if (this.opts.freq === "DAILY" && this.opts.bySetPos) {
933
- let start = this.originalDtstart;
934
- let matchCount = 0;
935
- if (this.includeDtstart && !this.matchesAll(start)) {
936
- if (iterator && !iterator(start, matchCount)) {
937
- return this.applyCountLimitAndMergeRDates(dates, iterator);
938
- }
939
- dates.push(start);
940
- matchCount++;
941
- if (this.shouldBreakForCountLimit(matchCount)) {
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);
916
+ }
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:
942
933
  return this.applyCountLimitAndMergeRDates(dates, iterator);
943
- }
944
934
  }
945
- let dayCursor = start.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
946
- outer_daily: while (true) {
935
+ while (true) {
947
936
  if (++iterationCount > this.maxIterations) {
948
937
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
949
938
  }
950
- let dayOccs = this.expandByTime(dayCursor);
951
- dayOccs = dayOccs.filter((occ) => this.matchesAll(occ));
952
- dayOccs = this.applyBySetPos(dayOccs);
953
- for (const occ of dayOccs) {
954
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
955
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
956
- break outer_daily;
957
- }
958
- if (iterator && !iterator(occ, matchCount)) {
959
- break outer_daily;
960
- }
961
- dates.push(occ);
962
- matchCount++;
963
- if (this.shouldBreakForCountLimit(matchCount)) {
964
- break outer_daily;
965
- }
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) {
944
+ break;
945
+ }
946
+ cursor = cursor.add(duration);
947
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(cursor, this.opts.until) > 0) {
948
+ break;
966
949
  }
967
- dayCursor = dayCursor.add({ days: this.opts.interval });
968
950
  }
969
951
  } else {
970
952
  let current = this.computeFirst();
971
- let matchCount = 0;
972
953
  if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
973
- if (iterator && !iterator(this.originalDtstart, matchCount)) {
954
+ if (iterator && !iterator(this.originalDtstart, dates.length)) {
974
955
  return this.applyCountLimitAndMergeRDates(dates, iterator);
975
956
  }
976
957
  dates.push(this.originalDtstart);
977
- matchCount++;
978
- if (this.shouldBreakForCountLimit(matchCount)) {
958
+ if (this.shouldBreakForCountLimit(dates.length)) {
979
959
  return this.applyCountLimitAndMergeRDates(dates, iterator);
980
960
  }
981
961
  }
@@ -987,12 +967,11 @@ var RRuleTemporal = class _RRuleTemporal {
987
967
  break;
988
968
  }
989
969
  if (this.matchesAll(current)) {
990
- if (iterator && !iterator(current, matchCount)) {
970
+ if (iterator && !iterator(current, dates.length)) {
991
971
  break;
992
972
  }
993
973
  dates.push(current);
994
- matchCount++;
995
- if (this.shouldBreakForCountLimit(matchCount)) {
974
+ if (this.shouldBreakForCountLimit(dates.length)) {
996
975
  break;
997
976
  }
998
977
  }
@@ -1155,8 +1134,7 @@ var RRuleTemporal = class _RRuleTemporal {
1155
1134
  if (interval !== 1) rule.push(`INTERVAL=${interval}`);
1156
1135
  if (count !== void 0) rule.push(`COUNT=${count}`);
1157
1136
  if (until) {
1158
- const u = until.toInstant().toString().replace(/[-:]/g, "");
1159
- rule.push(`UNTIL=${u.slice(0, 15)}Z`);
1137
+ rule.push(`UNTIL=${this.formatIcsDateTime(until)}`);
1160
1138
  }
1161
1139
  if (byHour) rule.push(`BYHOUR=${byHour.join(",")}`);
1162
1140
  if (byMinute) rule.push(`BYMINUTE=${byMinute.join(",")}`);
@@ -1177,8 +1155,11 @@ var RRuleTemporal = class _RRuleTemporal {
1177
1155
  }
1178
1156
  return lines.join("\n");
1179
1157
  }
1158
+ formatIcsDateTime(date) {
1159
+ return date.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z";
1160
+ }
1180
1161
  joinDates(dates) {
1181
- return dates.map((d) => d.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z");
1162
+ return dates.map((d) => this.formatIcsDateTime(d));
1182
1163
  }
1183
1164
  /**
1184
1165
  * Given any date in a month, return all the ZonedDateTimes in that month
@@ -1198,22 +1179,12 @@ var RRuleTemporal = class _RRuleTemporal {
1198
1179
  return [];
1199
1180
  }
1200
1181
  const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
1201
- let expanded2 = dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1202
- return expanded2;
1182
+ return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1203
1183
  }
1204
1184
  if (!byDay) {
1205
- const expanded2 = this.expandByTime(sample);
1206
- return expanded2;
1185
+ return this.expandByTime(sample);
1207
1186
  }
1208
- const dayMap = {
1209
- MO: 1,
1210
- TU: 2,
1211
- WE: 3,
1212
- TH: 4,
1213
- FR: 5,
1214
- SA: 6,
1215
- SU: 7
1216
- };
1187
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1217
1188
  const tokens = byDay.map((tok) => {
1218
1189
  const m = tok.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
1219
1190
  if (!m) return null;
@@ -1246,8 +1217,7 @@ var RRuleTemporal = class _RRuleTemporal {
1246
1217
  finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
1247
1218
  }
1248
1219
  const hits = finalDays.map((d) => sample.with({ day: d }));
1249
- let expanded = hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1250
- return expanded;
1220
+ return hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1251
1221
  }
1252
1222
  /**
1253
1223
  * Given any date in a year, return all ZonedDateTimes in that year matching
@@ -1260,15 +1230,7 @@ var RRuleTemporal = class _RRuleTemporal {
1260
1230
  let occs = [];
1261
1231
  const hasOrdinalByDay = this.opts.byDay && this.opts.byDay.some((t) => /^[+-]?\d/.test(t));
1262
1232
  if (hasOrdinalByDay && !this.opts.byMonth) {
1263
- const dayMap = {
1264
- MO: 1,
1265
- TU: 2,
1266
- WE: 3,
1267
- TH: 4,
1268
- FR: 5,
1269
- SA: 6,
1270
- SU: 7
1271
- };
1233
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1272
1234
  for (const tok of this.opts.byDay) {
1273
1235
  const m = tok.match(/^([+-]?\d{1,2})(MO|TU|WE|TH|FR|SA|SU)$/);
1274
1236
  if (!m) continue;
@@ -1296,32 +1258,24 @@ var RRuleTemporal = class _RRuleTemporal {
1296
1258
  const last = sample.with({ month: 12, day: 31 }).dayOfYear;
1297
1259
  for (const d of this.opts.byYearDay) {
1298
1260
  const dayNum = d > 0 ? d : last + d + 1;
1299
- const dt = sample.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1261
+ 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 });
1300
1262
  if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
1301
1263
  occs.push(...this.expandByTime(dt));
1302
1264
  }
1303
1265
  }
1304
1266
  }
1305
1267
  if (this.opts.byWeekNo) {
1306
- const dayMap = {
1307
- MO: 1,
1308
- TU: 2,
1309
- WE: 3,
1310
- TH: 4,
1311
- FR: 5,
1312
- SA: 6,
1313
- SU: 7
1314
- };
1268
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1315
1269
  const wkst = dayMap[this.opts.wkst || "MO"];
1316
1270
  const jan1 = sample.with({ month: 1, day: 1 });
1317
1271
  const delta = (jan1.dayOfWeek - wkst + 7) % 7;
1318
1272
  const firstWeekStart = jan1.subtract({ days: delta });
1319
- const lastWeekDiff = sample.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
1320
- const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
1273
+ const isLeapYear = jan1.inLeapYear;
1274
+ const lastWeek = jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52;
1321
1275
  const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
1322
1276
  var _a2;
1323
1277
  return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
1324
- }) : [this.opts.wkst || "MO"];
1278
+ }) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
1325
1279
  for (const weekNo of this.opts.byWeekNo) {
1326
1280
  const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1327
1281
  const weekStart = firstWeekStart.add({ weeks: weekIndex });
@@ -1339,6 +1293,112 @@ var RRuleTemporal = class _RRuleTemporal {
1339
1293
  occs = this.applyBySetPos(occs);
1340
1294
  return occs;
1341
1295
  }
1296
+ /**
1297
+ * Helper to find the next valid value from a sorted array
1298
+ */
1299
+ findNextValidValue(currentValue, validValues, compare) {
1300
+ return validValues.find((v) => compare(v, currentValue) > 0) || null;
1301
+ }
1302
+ /**
1303
+ * Efficiently find the next valid date for MINUTELY and SECONDLY frequency by jumping over
1304
+ * large gaps when BYXXX constraints don't match.
1305
+ */
1306
+ findNextValidDate(current) {
1307
+ if (this.opts.byMonth && !this.opts.byMonth.includes(current.month)) {
1308
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
1309
+ const nextMonth = this.findNextValidValue(current.month, months, (a, b) => a - b);
1310
+ if (nextMonth) {
1311
+ current = current.with({ month: nextMonth, day: 1, hour: 0, minute: 0, second: 0 });
1312
+ } else {
1313
+ current = current.add({ years: 1 }).with({ month: months[0], day: 1, hour: 0, minute: 0, second: 0 });
1314
+ }
1315
+ current = this.applyTimeOverride(current);
1316
+ return current;
1317
+ }
1318
+ if (this.opts.byYearDay && !this.matchesByYearDay(current)) {
1319
+ const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
1320
+ const currentYearDay = current.dayOfYear;
1321
+ const lastDayOfYear = current.with({ month: 12, day: 31 }).dayOfYear;
1322
+ let nextYearDay = yearDays.find((d) => {
1323
+ const dayNum = d > 0 ? d : lastDayOfYear + d + 1;
1324
+ return dayNum > currentYearDay;
1325
+ });
1326
+ if (nextYearDay) {
1327
+ const dayNum = nextYearDay > 0 ? nextYearDay : lastDayOfYear + nextYearDay + 1;
1328
+ if (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") {
1329
+ current = current.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }).add({ days: dayNum - 1 });
1330
+ } else {
1331
+ current = current.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1332
+ }
1333
+ } else {
1334
+ const nextYear = current.add({ years: 1 });
1335
+ const nextYearLastDay = nextYear.with({ month: 12, day: 31 }).dayOfYear;
1336
+ const firstYearDay = yearDays[0];
1337
+ if (firstYearDay !== void 0) {
1338
+ const dayNum = firstYearDay > 0 ? firstYearDay : nextYearLastDay + firstYearDay + 1;
1339
+ if (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") {
1340
+ current = nextYear.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }).add({ days: dayNum - 1 });
1341
+ } else {
1342
+ current = nextYear.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1343
+ }
1344
+ }
1345
+ }
1346
+ current = this.applyTimeOverride(current);
1347
+ return current;
1348
+ }
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
+ if (this.opts.byMonthDay && !this.matchesByMonthDay(current)) {
1355
+ const monthDays = [...this.opts.byMonthDay].sort((a, b) => a - b);
1356
+ const lastDayOfMonth = current.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
1357
+ const currentDay = current.day;
1358
+ const validDays = monthDays.map((d) => d > 0 ? d : lastDayOfMonth + d + 1).filter((d) => d > 0 && d <= lastDayOfMonth).sort((a, b) => a - b);
1359
+ const nextDay = this.findNextValidValue(currentDay, validDays, (a, b) => a - b);
1360
+ if (nextDay) {
1361
+ current = current.with({ day: nextDay, hour: 0, minute: 0, second: 0 });
1362
+ } else {
1363
+ const nextMonth = current.add({ months: 1 }).with({ day: 1 });
1364
+ const nextMonthLastDay = nextMonth.add({ months: 1 }).subtract({ days: 1 }).day;
1365
+ const firstMonthDay = monthDays[0];
1366
+ if (firstMonthDay !== void 0) {
1367
+ const dayNum = firstMonthDay > 0 ? firstMonthDay : nextMonthLastDay + firstMonthDay + 1;
1368
+ current = nextMonth.with({
1369
+ day: Math.max(1, Math.min(dayNum, nextMonthLastDay)),
1370
+ hour: 0,
1371
+ minute: 0,
1372
+ second: 0
1373
+ });
1374
+ }
1375
+ }
1376
+ current = this.applyTimeOverride(current);
1377
+ return current;
1378
+ }
1379
+ if (this.opts.byDay && !this.matchesByDay(current)) {
1380
+ 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 });
1394
+ } else {
1395
+ current = current.add({ days: 7 - currentDow + 1 }).with({ hour: 0, minute: 0, second: 0 });
1396
+ }
1397
+ current = this.applyTimeOverride(current);
1398
+ return current;
1399
+ }
1400
+ return this.nextCandidateSameDate(current);
1401
+ }
1342
1402
  applyBySetPos(list2) {
1343
1403
  const { bySetPos } = this.opts;
1344
1404
  if (!bySetPos || !bySetPos.length) return list2;