rrule-temporal 1.1.1 → 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/index.cjs CHANGED
@@ -41,21 +41,37 @@ 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 parseIcsDateTime(dateStr, tzid, valueType) {
45
+ const isDate = valueType === "DATE" || !dateStr.includes("T");
46
+ if (isDate) {
47
+ const isoDate = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}`;
48
+ return import_polyfill.Temporal.PlainDate.from(isoDate).toZonedDateTime({ timeZone: tzid });
49
+ }
50
+ if (/Z$/.test(dateStr)) {
51
+ const iso = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}T${dateStr.slice(9, 15)}Z`;
52
+ return import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
53
+ } else {
54
+ const isoDate = `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}T${dateStr.slice(9)}`;
55
+ return import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid);
56
+ }
57
+ }
44
58
  function parseDateValues(dateValues, tzid, valueType) {
45
59
  const dates = [];
46
60
  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
- }
61
+ dates.push(parseIcsDateTime(dateValue, tzid, valueType));
62
+ }
63
+ return dates;
64
+ }
65
+ function parseDateLines(lines, linePrefix, defaultTzid) {
66
+ const dates = [];
67
+ const regex = new RegExp(`^${linePrefix}(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)`, "i");
68
+ for (const line of lines) {
69
+ const match = line.match(regex);
70
+ if (match) {
71
+ const [, valueType, tzid, dateValuesStr] = match;
72
+ const timezone = tzid || defaultTzid;
73
+ const dateValues = dateValuesStr.split(",");
74
+ dates.push(...parseDateValues(dateValues, timezone, valueType));
59
75
  }
60
76
  }
61
77
  return dates;
@@ -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,15 +122,9 @@ 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 (!/Z$/.test(val) && 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
  }
@@ -198,46 +192,30 @@ 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
  }
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
201
  sanitizeOpts(opts) {
202
202
  const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
203
203
  if (opts.byDay) {
204
204
  opts.byDay = opts.byDay.filter((d) => validDay.test(d));
205
205
  if (opts.byDay.length === 0) delete opts.byDay;
206
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;
234
- }
207
+ opts.byMonth = this.sanitizeNumericArray(opts.byMonth, 1, 12, false, false);
208
+ opts.byMonthDay = this.sanitizeNumericArray(opts.byMonthDay, -31, 31, false, false);
209
+ opts.byYearDay = this.sanitizeNumericArray(opts.byYearDay, -366, 366, false, false);
210
+ opts.byWeekNo = this.sanitizeNumericArray(opts.byWeekNo, -53, 53, false, false);
211
+ opts.byHour = this.sanitizeNumericArray(opts.byHour, 0, 23, true, true);
212
+ opts.byMinute = this.sanitizeNumericArray(opts.byMinute, 0, 59, true, true);
213
+ opts.bySecond = this.sanitizeNumericArray(opts.bySecond, 0, 59, true, true);
235
214
  if (opts.bySetPos) {
236
215
  if (opts.bySetPos.some((p) => p === 0)) {
237
216
  throw new Error("bySetPos may not contain 0");
238
217
  }
239
- opts.bySetPos = opts.bySetPos.filter((n) => Number.isInteger(n) && n !== 0);
240
- if (opts.bySetPos.length === 0) delete opts.bySetPos;
218
+ opts.bySetPos = this.sanitizeNumericArray(opts.bySetPos, -Infinity, Infinity, false, false);
241
219
  }
242
220
  return opts;
243
221
  }
@@ -252,8 +230,14 @@ var RRuleTemporal = class _RRuleTemporal {
252
230
  return zdt.add({ months: interval });
253
231
  case "YEARLY":
254
232
  return zdt.add({ years: interval });
255
- case "HOURLY":
256
- return zdt.add({ hours: interval });
233
+ case "HOURLY": {
234
+ const originalHour = zdt.hour;
235
+ let next = zdt.add({ hours: interval });
236
+ if (next.hour === originalHour && interval === 1) {
237
+ next = next.add({ hours: interval });
238
+ }
239
+ return next;
240
+ }
257
241
  case "MINUTELY":
258
242
  return zdt.add({ minutes: interval });
259
243
  case "SECONDLY":
@@ -289,30 +273,55 @@ var RRuleTemporal = class _RRuleTemporal {
289
273
  if (freq === "MINUTELY" && byMinute && byMinute.length === 1) {
290
274
  return this.applyTimeOverride(zdt.add({ hours: interval }));
291
275
  }
292
- if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute && interval > 1) {
276
+ if (bySecond && bySecond.length > 1) {
277
+ const idx = bySecond.indexOf(zdt.second);
278
+ if (idx !== -1 && idx < bySecond.length - 1) {
279
+ return zdt.with({ second: bySecond[idx + 1] });
280
+ }
281
+ }
282
+ if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute) {
293
283
  const next = zdt.add({ minutes: interval });
294
284
  if (byHour.includes(next.hour)) {
295
- return next;
285
+ return next.with({ second: bySecond ? bySecond[0] : zdt.second });
296
286
  }
297
287
  const nextHour = byHour.find((h) => h > zdt.hour) || byHour[0];
298
288
  if (nextHour && nextHour > zdt.hour) {
299
- return zdt.with({ hour: nextHour, minute: 0 });
289
+ return zdt.with({ hour: nextHour, minute: 0, second: bySecond ? bySecond[0] : zdt.second });
300
290
  }
301
291
  return this.applyTimeOverride(zdt.add({ days: 1 }));
302
292
  }
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] });
293
+ if (freq === "SECONDLY") {
294
+ let next = zdt.add({ seconds: interval });
295
+ if (bySecond && bySecond.length > 0) {
296
+ const nextSecond = bySecond.find((s) => s > zdt.second);
297
+ if (nextSecond !== void 0) {
298
+ next = zdt.with({ second: nextSecond });
299
+ } else {
300
+ next = zdt.add({ minutes: 1 }).with({ second: bySecond[0] });
301
+ }
302
+ }
303
+ if (byMinute && byMinute.length > 0 && !byMinute.includes(next.minute)) {
304
+ const nextMinute = byMinute.find((m) => m > next.minute);
305
+ if (nextMinute !== void 0) {
306
+ next = next.with({ minute: nextMinute, second: bySecond ? bySecond[0] : 0 });
307
+ } else {
308
+ const nextHour = byHour ? byHour.find((h) => h > next.hour) : void 0;
309
+ if (nextHour !== void 0) {
310
+ next = next.with({ hour: nextHour, minute: byMinute[0], second: bySecond ? bySecond[0] : 0 });
311
+ } else {
312
+ next = this.applyTimeOverride(next.add({ days: 1 }));
313
+ }
314
+ }
315
315
  }
316
+ if (byHour && byHour.length > 0 && !byHour.includes(next.hour)) {
317
+ const nextHour = byHour.find((h) => h > next.hour);
318
+ if (nextHour !== void 0) {
319
+ next = next.with({ hour: nextHour, minute: byMinute ? byMinute[0] : 0, second: bySecond ? bySecond[0] : 0 });
320
+ } else {
321
+ next = this.applyTimeOverride(next.add({ days: 1 }));
322
+ }
323
+ }
324
+ return next;
316
325
  }
317
326
  if (byMinute && byMinute.length > 1) {
318
327
  const idx = byMinute.indexOf(zdt.minute);
@@ -322,6 +331,24 @@ var RRuleTemporal = class _RRuleTemporal {
322
331
  second: bySecond ? bySecond[0] : zdt.second
323
332
  });
324
333
  }
334
+ if (freq === "MINUTELY" && idx === byMinute.length - 1) {
335
+ if (byHour && byHour.length > 0) {
336
+ const currentHourIdx = byHour.indexOf(zdt.hour);
337
+ if (currentHourIdx !== -1 && currentHourIdx < byHour.length - 1) {
338
+ return zdt.with({
339
+ hour: byHour[currentHourIdx + 1],
340
+ minute: byMinute[0],
341
+ second: bySecond ? bySecond[0] : zdt.second
342
+ });
343
+ } else {
344
+ return this.applyTimeOverride(zdt.add({ days: 1 }));
345
+ }
346
+ }
347
+ return zdt.add({ hours: interval }).with({
348
+ minute: byMinute[0],
349
+ second: bySecond ? bySecond[0] : zdt.second
350
+ });
351
+ }
325
352
  }
326
353
  if (byHour && byHour.length > 1) {
327
354
  const idx = byHour.indexOf(zdt.hour);
@@ -333,6 +360,9 @@ var RRuleTemporal = class _RRuleTemporal {
333
360
  });
334
361
  }
335
362
  }
363
+ if (freq === "HOURLY" && byHour && byHour.length > 1) {
364
+ return this.applyTimeOverride(zdt.add({ days: 1 }));
365
+ }
336
366
  return this.applyTimeOverride(this.rawAdvance(zdt));
337
367
  }
338
368
  applyTimeOverride(zdt) {
@@ -344,7 +374,7 @@ var RRuleTemporal = class _RRuleTemporal {
344
374
  return dt;
345
375
  }
346
376
  computeFirst() {
347
- var _a, _b, _c;
377
+ var _a, _b, _c, _d;
348
378
  let zdt = this.originalDtstart;
349
379
  if (((_a = this.opts.byWeekNo) == null ? void 0 : _a.length) && ["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq)) {
350
380
  let targetWeek = this.opts.byWeekNo[0];
@@ -376,15 +406,7 @@ var RRuleTemporal = class _RRuleTemporal {
376
406
  weekStart = firstThursday.subtract({ days: 3 }).add({ weeks: lastWeek + targetWeek });
377
407
  }
378
408
  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
- };
409
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
388
410
  const targetDays = this.opts.byDay.map((tok) => {
389
411
  var _a2;
390
412
  return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
@@ -411,27 +433,44 @@ var RRuleTemporal = class _RRuleTemporal {
411
433
  }
412
434
  }
413
435
  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);
436
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
437
+ const hasOrdinalTokens = this.opts.byDay.some((tok) => /^[+-]?\d/.test(tok));
438
+ if (hasOrdinalTokens && this.opts.byMonth && (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY")) {
439
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
440
+ let foundFirst = false;
441
+ for (let year = zdt.year; year <= zdt.year + 10 && !foundFirst; year++) {
442
+ for (const month of months) {
443
+ if (year === zdt.year && month < zdt.month) continue;
444
+ const monthSample = zdt.with({ year, month, day: 1 });
445
+ const monthlyOccs = this.generateMonthlyOccurrences(monthSample);
446
+ for (const occ of monthlyOccs) {
447
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, zdt) >= 0) {
448
+ if (!occ.toPlainDate().equals(zdt.toPlainDate())) {
449
+ zdt = this.applyTimeOverride(occ.with({ hour: 0, minute: 0, second: 0 }));
450
+ } else {
451
+ zdt = occ;
452
+ }
453
+ foundFirst = true;
454
+ break;
455
+ }
456
+ }
457
+ if (foundFirst) break;
458
+ }
459
+ }
426
460
  } 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) });
461
+ let deltas = [];
462
+ if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && this.opts.byDay.every((tok) => /^[A-Z]{2}$/.test(tok))) {
463
+ deltas = this.opts.byDay.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
464
+ } else {
465
+ deltas = this.opts.byDay.map((tok) => {
466
+ var _a2;
467
+ const wdTok = (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
468
+ return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
469
+ }).filter((d) => d !== null);
470
+ }
471
+ if (deltas.length) {
472
+ zdt = zdt.add({ days: Math.min(...deltas) });
473
+ }
435
474
  }
436
475
  }
437
476
  const { byHour, byMinute, bySecond } = this.opts;
@@ -441,6 +480,18 @@ var RRuleTemporal = class _RRuleTemporal {
441
480
  ) > 0) {
442
481
  zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
443
482
  }
483
+ if (this.opts.freq === "MINUTELY" && !byMinute && import_polyfill.Temporal.ZonedDateTime.compare(
484
+ zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 }),
485
+ this.originalDtstart
486
+ ) > 0) {
487
+ zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
488
+ }
489
+ if (this.opts.freq === "SECONDLY" && ((_d = this.opts.byWeekNo) == null ? void 0 : _d.length) && !bySecond && import_polyfill.Temporal.ZonedDateTime.compare(
490
+ zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 }),
491
+ this.originalDtstart
492
+ ) > 0) {
493
+ zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
494
+ }
444
495
  if (byHour || byMinute || bySecond) {
445
496
  const candidates = this.expandByTime(zdt);
446
497
  for (const candidate of candidates) {
@@ -457,15 +508,7 @@ var RRuleTemporal = class _RRuleTemporal {
457
508
  matchesByDay(zdt) {
458
509
  const { byDay, freq } = this.opts;
459
510
  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
- };
511
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
469
512
  for (const token of byDay) {
470
513
  const m = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
471
514
  if (!m) continue;
@@ -498,26 +541,57 @@ var RRuleTemporal = class _RRuleTemporal {
498
541
  if (!byMonth) return true;
499
542
  return byMonth.includes(zdt.month);
500
543
  }
544
+ matchesNumericConstraint(value, constraints, maxPositiveValue) {
545
+ return constraints.some((c) => {
546
+ const target = c > 0 ? c : maxPositiveValue + c + 1;
547
+ return value === target;
548
+ });
549
+ }
501
550
  matchesByMonthDay(zdt) {
502
551
  const { byMonthDay } = this.opts;
503
552
  if (!byMonthDay) return true;
504
553
  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);
554
+ return this.matchesNumericConstraint(zdt.day, byMonthDay, lastDay);
555
+ }
556
+ matchesByHour(zdt) {
557
+ const { byHour } = this.opts;
558
+ if (!byHour) return true;
559
+ if (byHour.includes(zdt.hour)) {
560
+ return true;
561
+ }
562
+ for (const h of byHour) {
563
+ const intendedTime = zdt.with({ hour: h });
564
+ if (intendedTime.hour === zdt.hour) {
565
+ return true;
566
+ }
567
+ }
568
+ return false;
569
+ }
570
+ matchesByMinute(zdt) {
571
+ const { byMinute } = this.opts;
572
+ if (!byMinute) return true;
573
+ return byMinute.includes(zdt.minute);
574
+ }
575
+ matchesBySecond(zdt) {
576
+ const { bySecond } = this.opts;
577
+ if (!bySecond) return true;
578
+ return bySecond.includes(zdt.second);
506
579
  }
507
580
  matchesAll(zdt) {
508
- return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt) && this.matchesByYearDay(zdt) && this.matchesByWeekNo(zdt);
581
+ 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);
509
582
  }
510
583
  matchesByYearDay(zdt) {
511
584
  const { byYearDay } = this.opts;
512
585
  if (!byYearDay) return true;
513
586
  const dayOfYear = zdt.dayOfYear;
514
587
  const last = zdt.with({ month: 12, day: 31 }).dayOfYear;
515
- return byYearDay.some((d) => d > 0 ? dayOfYear === d : dayOfYear === last + d + 1);
588
+ return this.matchesNumericConstraint(dayOfYear, byYearDay, last);
516
589
  }
517
590
  getIsoWeekInfo(zdt) {
518
591
  const thursday = zdt.add({ days: 4 - zdt.dayOfWeek });
519
592
  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 });
593
+ const jan1 = zdt.with({ year, month: 1, day: 1 });
594
+ const firstThursday = jan1.add({ days: (4 - jan1.dayOfWeek + 7) % 7 });
521
595
  const diffDays = thursday.toPlainDate().since(firstThursday.toPlainDate()).days;
522
596
  const week = Math.floor(diffDays / 7) + 1;
523
597
  return { week, year };
@@ -543,6 +617,41 @@ var RRuleTemporal = class _RRuleTemporal {
543
617
  options() {
544
618
  return this.opts;
545
619
  }
620
+ addDtstartIfNeeded(dates, iterator) {
621
+ if (this.includeDtstart && !this.matchesAll(this.originalDtstart)) {
622
+ if (iterator && !iterator(this.originalDtstart, dates.length)) {
623
+ return false;
624
+ }
625
+ dates.push(this.originalDtstart);
626
+ if (this.shouldBreakForCountLimit(dates.length)) {
627
+ return false;
628
+ }
629
+ }
630
+ return true;
631
+ }
632
+ processOccurrences(occs, dates, start, iterator, extraFilters) {
633
+ let shouldBreak = false;
634
+ for (const occ of occs) {
635
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
636
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
637
+ shouldBreak = true;
638
+ break;
639
+ }
640
+ if (extraFilters && !extraFilters(occ)) {
641
+ continue;
642
+ }
643
+ if (iterator && !iterator(occ, dates.length)) {
644
+ shouldBreak = true;
645
+ break;
646
+ }
647
+ dates.push(occ);
648
+ if (this.shouldBreakForCountLimit(dates.length)) {
649
+ shouldBreak = true;
650
+ break;
651
+ }
652
+ }
653
+ return { shouldBreak };
654
+ }
546
655
  /**
547
656
  * Returns all occurrences of the rule.
548
657
  * @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
@@ -557,19 +666,11 @@ var RRuleTemporal = class _RRuleTemporal {
557
666
  let iterationCount = 0;
558
667
  if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
559
668
  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
- }
669
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
670
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
571
671
  }
572
- outer_month: while (true) {
672
+ let monthCursor = start.with({ day: 1 });
673
+ while (true) {
573
674
  if (++iterationCount > this.maxIterations) {
574
675
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
575
676
  }
@@ -577,85 +678,22 @@ var RRuleTemporal = class _RRuleTemporal {
577
678
  occs = this.applyBySetPos(occs);
578
679
  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
680
  monthCursor = monthCursor.add({ months: this.opts.interval });
580
- continue outer_month;
681
+ continue;
581
682
  }
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
- }
683
+ const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
684
+ if (shouldBreak) {
685
+ break;
595
686
  }
596
687
  monthCursor = monthCursor.add({ months: this.opts.interval });
597
688
  }
598
689
  return this.applyCountLimitAndMergeRDates(dates, iterator);
599
690
  }
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
- }
691
+ if (this.opts.freq === "WEEKLY" && !(this.opts.byYearDay && this.opts.byYearDay.length > 0)) {
640
692
  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
- }
693
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
694
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
649
695
  }
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
- };
696
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
659
697
  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
698
  const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
661
699
  const firstWeekDates = dows.map((dw) => {
@@ -666,8 +704,7 @@ var RRuleTemporal = class _RRuleTemporal {
666
704
  const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
667
705
  const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
668
706
  let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
669
- let matchCount = 0;
670
- outer_week: while (true) {
707
+ while (true) {
671
708
  if (++iterationCount > this.maxIterations) {
672
709
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
673
710
  }
@@ -677,25 +714,15 @@ var RRuleTemporal = class _RRuleTemporal {
677
714
  return this.expandByTime(sameDate);
678
715
  }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
679
716
  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
- }
717
+ const { shouldBreak } = this.processOccurrences(
718
+ occs,
719
+ dates,
720
+ start,
721
+ iterator,
722
+ (occ) => this.matchesByMonth(occ) && this.matchesByMonthDay(occ)
723
+ );
724
+ if (shouldBreak) {
725
+ break;
699
726
  }
700
727
  weekCursor = weekCursor.add({ weeks: this.opts.interval });
701
728
  }
@@ -703,18 +730,11 @@ var RRuleTemporal = class _RRuleTemporal {
703
730
  }
704
731
  if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay) {
705
732
  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
- }
733
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
734
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
714
735
  }
715
736
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
716
737
  let monthOffset = 0;
717
- let matchCount = dates.length;
718
738
  let startMonthIndex = months.findIndex((m) => m >= start.month);
719
739
  if (startMonthIndex === -1) {
720
740
  startMonthIndex = 0;
@@ -735,12 +755,11 @@ var RRuleTemporal = class _RRuleTemporal {
735
755
  break;
736
756
  }
737
757
  if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
738
- if (iterator && !iterator(candidate, matchCount)) {
758
+ if (iterator && !iterator(candidate, dates.length)) {
739
759
  break;
740
760
  }
741
761
  dates.push(candidate);
742
- matchCount++;
743
- if (this.shouldBreakForCountLimit(matchCount)) {
762
+ if (this.shouldBreakForCountLimit(dates.length)) {
744
763
  break;
745
764
  }
746
765
  }
@@ -750,18 +769,11 @@ var RRuleTemporal = class _RRuleTemporal {
750
769
  }
751
770
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo) {
752
771
  const start = this.originalDtstart;
753
- if (this.includeDtstart && !this.matchesAll(start)) {
754
- if (iterator && !iterator(start, 0)) {
755
- return this.applyCountLimitAndMergeRDates(dates, iterator);
756
- }
757
- dates.push(start);
758
- if (this.shouldBreakForCountLimit(1)) {
759
- return this.applyCountLimitAndMergeRDates(dates, iterator);
760
- }
772
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
773
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
761
774
  }
762
775
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
763
776
  let yearOffset = 0;
764
- let matchCount = 0;
765
777
  while (true) {
766
778
  if (++iterationCount > this.maxIterations) {
767
779
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
@@ -776,12 +788,11 @@ var RRuleTemporal = class _RRuleTemporal {
776
788
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
777
789
  return dates;
778
790
  }
779
- if (iterator && !iterator(occ, matchCount)) {
791
+ if (iterator && !iterator(occ, dates.length)) {
780
792
  return dates;
781
793
  }
782
794
  dates.push(occ);
783
- matchCount++;
784
- if (this.shouldBreakForCountLimit(matchCount)) {
795
+ if (this.shouldBreakForCountLimit(dates.length)) {
785
796
  return this.applyCountLimitAndMergeRDates(dates, iterator);
786
797
  }
787
798
  }
@@ -789,97 +800,67 @@ var RRuleTemporal = class _RRuleTemporal {
789
800
  }
790
801
  return this.applyCountLimitAndMergeRDates(dates, iterator);
791
802
  }
792
- if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
803
+ 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) {
793
804
  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
- }
805
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
806
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
802
807
  }
803
808
  let yearCursor = start.with({ month: 1, day: 1 });
804
- let matchCount = 0;
805
- outer_year: while (true) {
809
+ while (true) {
806
810
  if (++iterationCount > this.maxIterations) {
807
811
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
808
812
  }
809
813
  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;
822
- }
814
+ const { shouldBreak } = this.processOccurrences(occs, dates, start, iterator);
815
+ if (shouldBreak) {
816
+ break;
817
+ }
818
+ const interval = this.opts.freq === "WEEKLY" ? 1 : this.opts.interval;
819
+ yearCursor = yearCursor.add({ years: interval });
820
+ if (this.opts.freq === "WEEKLY" && this.opts.until && yearCursor.year > this.opts.until.year) {
821
+ break;
823
822
  }
824
- yearCursor = yearCursor.add({ years: this.opts.interval });
825
823
  }
826
824
  return this.applyCountLimitAndMergeRDates(dates, iterator);
827
825
  }
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
- }
826
+ if ((this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") && (this.opts.byMonth || this.opts.byWeekNo || this.opts.byYearDay || this.opts.byMonthDay || this.opts.byDay)) {
827
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
828
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
838
829
  }
839
- let yearCursor = start.with({ month: 1, day: 1 });
840
- let matchCount = 0;
841
- outer_year2: while (true) {
830
+ let current = this.computeFirst();
831
+ while (true) {
842
832
  if (++iterationCount > this.maxIterations) {
843
833
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
844
834
  }
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;
835
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
836
+ break;
837
+ }
838
+ if (this.matchesAll(current)) {
839
+ if (iterator && !iterator(current, dates.length)) {
840
+ break;
853
841
  }
854
- dates.push(occ);
855
- matchCount++;
856
- if (this.shouldBreakForCountLimit(matchCount)) {
857
- break outer_year2;
842
+ dates.push(current);
843
+ if (this.shouldBreakForCountLimit(dates.length)) {
844
+ break;
858
845
  }
846
+ current = this.nextCandidateSameDate(current);
847
+ } else {
848
+ current = this.findNextValidDate(current);
859
849
  }
860
- yearCursor = yearCursor.add({ years: this.opts.interval });
861
850
  }
862
851
  return this.applyCountLimitAndMergeRDates(dates, iterator);
863
852
  }
864
853
  if (this.opts.freq === "MONTHLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 && !this.opts.byDay && !this.opts.byMonthDay) {
865
854
  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
- }
855
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
856
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
876
857
  }
877
858
  let year = start.year;
878
859
  const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
879
860
  const interval = this.opts.interval;
880
861
  const startMonthAbs = start.year * 12 + start.month;
881
862
  outer_loop: while (true) {
882
- if (this.shouldBreakForCountLimit(matchCount)) {
863
+ if (this.shouldBreakForCountLimit(dates.length)) {
883
864
  break;
884
865
  }
885
866
  if (++iterationCount > this.maxIterations) {
@@ -904,12 +885,11 @@ var RRuleTemporal = class _RRuleTemporal {
904
885
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
905
886
  break outer_loop;
906
887
  }
907
- if (iterator && !iterator(occ, matchCount)) {
888
+ if (iterator && !iterator(occ, dates.length)) {
908
889
  break outer_loop;
909
890
  }
910
891
  dates.push(occ);
911
- matchCount++;
912
- if (this.shouldBreakForCountLimit(matchCount)) {
892
+ if (this.shouldBreakForCountLimit(dates.length)) {
913
893
  break outer_loop;
914
894
  }
915
895
  }
@@ -927,53 +907,53 @@ var RRuleTemporal = class _RRuleTemporal {
927
907
  }
928
908
  return this.applyCountLimitAndMergeRDates(dates, iterator);
929
909
  }
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)) {
910
+ if ((this.opts.freq === "MINUTELY" || this.opts.freq === "HOURLY" || this.opts.freq === "DAILY") && this.opts.bySetPos) {
911
+ const start = this.originalDtstart;
912
+ if (!this.addDtstartIfNeeded(dates, iterator)) {
913
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
914
+ }
915
+ let cursor;
916
+ let duration;
917
+ switch (this.opts.freq) {
918
+ case "MINUTELY":
919
+ cursor = start.with({ second: 0, microsecond: 0, nanosecond: 0 });
920
+ duration = { minutes: this.opts.interval };
921
+ break;
922
+ case "HOURLY":
923
+ cursor = start.with({ minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
924
+ duration = { hours: this.opts.interval };
925
+ break;
926
+ case "DAILY":
927
+ cursor = start.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
928
+ duration = { days: this.opts.interval };
929
+ break;
930
+ default:
940
931
  return this.applyCountLimitAndMergeRDates(dates, iterator);
941
- }
942
932
  }
943
- let dayCursor = start.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
944
- outer_daily: while (true) {
933
+ while (true) {
945
934
  if (++iterationCount > this.maxIterations) {
946
935
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
947
936
  }
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) {
952
- if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
953
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
954
- break outer_daily;
955
- }
956
- if (iterator && !iterator(occ, matchCount)) {
957
- break outer_daily;
958
- }
959
- dates.push(occ);
960
- matchCount++;
961
- if (this.shouldBreakForCountLimit(matchCount)) {
962
- break outer_daily;
963
- }
937
+ let periodOccs = this.expandByTime(cursor);
938
+ periodOccs = periodOccs.filter((occ) => this.matchesAll(occ));
939
+ periodOccs = this.applyBySetPos(periodOccs);
940
+ const { shouldBreak } = this.processOccurrences(periodOccs, dates, start, iterator);
941
+ if (shouldBreak) {
942
+ break;
943
+ }
944
+ cursor = cursor.add(duration);
945
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(cursor, this.opts.until) > 0) {
946
+ break;
964
947
  }
965
- dayCursor = dayCursor.add({ days: this.opts.interval });
966
948
  }
967
949
  } else {
968
950
  let current = this.computeFirst();
969
- let matchCount = 0;
970
951
  if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
971
- if (iterator && !iterator(this.originalDtstart, matchCount)) {
952
+ if (iterator && !iterator(this.originalDtstart, dates.length)) {
972
953
  return this.applyCountLimitAndMergeRDates(dates, iterator);
973
954
  }
974
955
  dates.push(this.originalDtstart);
975
- matchCount++;
976
- if (this.shouldBreakForCountLimit(matchCount)) {
956
+ if (this.shouldBreakForCountLimit(dates.length)) {
977
957
  return this.applyCountLimitAndMergeRDates(dates, iterator);
978
958
  }
979
959
  }
@@ -985,12 +965,11 @@ var RRuleTemporal = class _RRuleTemporal {
985
965
  break;
986
966
  }
987
967
  if (this.matchesAll(current)) {
988
- if (iterator && !iterator(current, matchCount)) {
968
+ if (iterator && !iterator(current, dates.length)) {
989
969
  break;
990
970
  }
991
971
  dates.push(current);
992
- matchCount++;
993
- if (this.shouldBreakForCountLimit(matchCount)) {
972
+ if (this.shouldBreakForCountLimit(dates.length)) {
994
973
  break;
995
974
  }
996
975
  }
@@ -1153,8 +1132,7 @@ var RRuleTemporal = class _RRuleTemporal {
1153
1132
  if (interval !== 1) rule.push(`INTERVAL=${interval}`);
1154
1133
  if (count !== void 0) rule.push(`COUNT=${count}`);
1155
1134
  if (until) {
1156
- const u = until.toInstant().toString().replace(/[-:]/g, "");
1157
- rule.push(`UNTIL=${u.slice(0, 15)}Z`);
1135
+ rule.push(`UNTIL=${this.formatIcsDateTime(until)}`);
1158
1136
  }
1159
1137
  if (byHour) rule.push(`BYHOUR=${byHour.join(",")}`);
1160
1138
  if (byMinute) rule.push(`BYMINUTE=${byMinute.join(",")}`);
@@ -1175,8 +1153,11 @@ var RRuleTemporal = class _RRuleTemporal {
1175
1153
  }
1176
1154
  return lines.join("\n");
1177
1155
  }
1156
+ formatIcsDateTime(date) {
1157
+ return date.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z";
1158
+ }
1178
1159
  joinDates(dates) {
1179
- return dates.map((d) => d.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z");
1160
+ return dates.map((d) => this.formatIcsDateTime(d));
1180
1161
  }
1181
1162
  /**
1182
1163
  * Given any date in a month, return all the ZonedDateTimes in that month
@@ -1196,22 +1177,12 @@ var RRuleTemporal = class _RRuleTemporal {
1196
1177
  return [];
1197
1178
  }
1198
1179
  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;
1180
+ return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1201
1181
  }
1202
1182
  if (!byDay) {
1203
- const expanded2 = this.expandByTime(sample);
1204
- return expanded2;
1183
+ return this.expandByTime(sample);
1205
1184
  }
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
- };
1185
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1215
1186
  const tokens = byDay.map((tok) => {
1216
1187
  const m = tok.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
1217
1188
  if (!m) return null;
@@ -1244,8 +1215,7 @@ var RRuleTemporal = class _RRuleTemporal {
1244
1215
  finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
1245
1216
  }
1246
1217
  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;
1218
+ return hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1249
1219
  }
1250
1220
  /**
1251
1221
  * Given any date in a year, return all ZonedDateTimes in that year matching
@@ -1258,15 +1228,7 @@ var RRuleTemporal = class _RRuleTemporal {
1258
1228
  let occs = [];
1259
1229
  const hasOrdinalByDay = this.opts.byDay && this.opts.byDay.some((t) => /^[+-]?\d/.test(t));
1260
1230
  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
- };
1231
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1270
1232
  for (const tok of this.opts.byDay) {
1271
1233
  const m = tok.match(/^([+-]?\d{1,2})(MO|TU|WE|TH|FR|SA|SU)$/);
1272
1234
  if (!m) continue;
@@ -1294,32 +1256,24 @@ var RRuleTemporal = class _RRuleTemporal {
1294
1256
  const last = sample.with({ month: 12, day: 31 }).dayOfYear;
1295
1257
  for (const d of this.opts.byYearDay) {
1296
1258
  const dayNum = d > 0 ? d : last + d + 1;
1297
- const dt = sample.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1259
+ 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
1260
  if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
1299
1261
  occs.push(...this.expandByTime(dt));
1300
1262
  }
1301
1263
  }
1302
1264
  }
1303
1265
  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
- };
1266
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1313
1267
  const wkst = dayMap[this.opts.wkst || "MO"];
1314
1268
  const jan1 = sample.with({ month: 1, day: 1 });
1315
1269
  const delta = (jan1.dayOfWeek - wkst + 7) % 7;
1316
1270
  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;
1271
+ const isLeapYear = jan1.inLeapYear;
1272
+ const lastWeek = jan1.dayOfWeek === 4 || isLeapYear && jan1.dayOfWeek === 3 ? 53 : 52;
1319
1273
  const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
1320
1274
  var _a2;
1321
1275
  return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
1322
- }) : [this.opts.wkst || "MO"];
1276
+ }) : [Object.entries(dayMap).find(([, d]) => d === this.originalDtstart.dayOfWeek)[0]];
1323
1277
  for (const weekNo of this.opts.byWeekNo) {
1324
1278
  const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1325
1279
  const weekStart = firstWeekStart.add({ weeks: weekIndex });
@@ -1337,6 +1291,112 @@ var RRuleTemporal = class _RRuleTemporal {
1337
1291
  occs = this.applyBySetPos(occs);
1338
1292
  return occs;
1339
1293
  }
1294
+ /**
1295
+ * Helper to find the next valid value from a sorted array
1296
+ */
1297
+ findNextValidValue(currentValue, validValues, compare) {
1298
+ return validValues.find((v) => compare(v, currentValue) > 0) || null;
1299
+ }
1300
+ /**
1301
+ * Efficiently find the next valid date for MINUTELY and SECONDLY frequency by jumping over
1302
+ * large gaps when BYXXX constraints don't match.
1303
+ */
1304
+ findNextValidDate(current) {
1305
+ if (this.opts.byMonth && !this.opts.byMonth.includes(current.month)) {
1306
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
1307
+ const nextMonth = this.findNextValidValue(current.month, months, (a, b) => a - b);
1308
+ if (nextMonth) {
1309
+ current = current.with({ month: nextMonth, day: 1, hour: 0, minute: 0, second: 0 });
1310
+ } else {
1311
+ current = current.add({ years: 1 }).with({ month: months[0], day: 1, hour: 0, minute: 0, second: 0 });
1312
+ }
1313
+ current = this.applyTimeOverride(current);
1314
+ return current;
1315
+ }
1316
+ if (this.opts.byYearDay && !this.matchesByYearDay(current)) {
1317
+ const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
1318
+ const currentYearDay = current.dayOfYear;
1319
+ const lastDayOfYear = current.with({ month: 12, day: 31 }).dayOfYear;
1320
+ let nextYearDay = yearDays.find((d) => {
1321
+ const dayNum = d > 0 ? d : lastDayOfYear + d + 1;
1322
+ return dayNum > currentYearDay;
1323
+ });
1324
+ if (nextYearDay) {
1325
+ const dayNum = nextYearDay > 0 ? nextYearDay : lastDayOfYear + nextYearDay + 1;
1326
+ if (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") {
1327
+ current = current.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }).add({ days: dayNum - 1 });
1328
+ } else {
1329
+ current = current.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1330
+ }
1331
+ } else {
1332
+ const nextYear = current.add({ years: 1 });
1333
+ const nextYearLastDay = nextYear.with({ month: 12, day: 31 }).dayOfYear;
1334
+ const firstYearDay = yearDays[0];
1335
+ if (firstYearDay !== void 0) {
1336
+ const dayNum = firstYearDay > 0 ? firstYearDay : nextYearLastDay + firstYearDay + 1;
1337
+ if (this.opts.freq === "MINUTELY" || this.opts.freq === "SECONDLY") {
1338
+ current = nextYear.with({ month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }).add({ days: dayNum - 1 });
1339
+ } else {
1340
+ current = nextYear.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
1341
+ }
1342
+ }
1343
+ }
1344
+ current = this.applyTimeOverride(current);
1345
+ return current;
1346
+ }
1347
+ if (this.opts.byWeekNo && !this.matchesByWeekNo(current)) {
1348
+ current = current.add({ weeks: 1 }).with({ hour: 0, minute: 0, second: 0 });
1349
+ current = this.applyTimeOverride(current);
1350
+ return current;
1351
+ }
1352
+ if (this.opts.byMonthDay && !this.matchesByMonthDay(current)) {
1353
+ const monthDays = [...this.opts.byMonthDay].sort((a, b) => a - b);
1354
+ const lastDayOfMonth = current.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
1355
+ const currentDay = current.day;
1356
+ const validDays = monthDays.map((d) => d > 0 ? d : lastDayOfMonth + d + 1).filter((d) => d > 0 && d <= lastDayOfMonth).sort((a, b) => a - b);
1357
+ const nextDay = this.findNextValidValue(currentDay, validDays, (a, b) => a - b);
1358
+ if (nextDay) {
1359
+ current = current.with({ day: nextDay, hour: 0, minute: 0, second: 0 });
1360
+ } else {
1361
+ const nextMonth = current.add({ months: 1 }).with({ day: 1 });
1362
+ const nextMonthLastDay = nextMonth.add({ months: 1 }).subtract({ days: 1 }).day;
1363
+ const firstMonthDay = monthDays[0];
1364
+ if (firstMonthDay !== void 0) {
1365
+ const dayNum = firstMonthDay > 0 ? firstMonthDay : nextMonthLastDay + firstMonthDay + 1;
1366
+ current = nextMonth.with({
1367
+ day: Math.max(1, Math.min(dayNum, nextMonthLastDay)),
1368
+ hour: 0,
1369
+ minute: 0,
1370
+ second: 0
1371
+ });
1372
+ }
1373
+ }
1374
+ current = this.applyTimeOverride(current);
1375
+ return current;
1376
+ }
1377
+ if (this.opts.byDay && !this.matchesByDay(current)) {
1378
+ const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
1379
+ const currentDow = current.dayOfWeek;
1380
+ let nextDow = 8;
1381
+ for (const token of this.opts.byDay) {
1382
+ const match = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
1383
+ if (match && !match[1]) {
1384
+ const dow = dayMap[match[2]];
1385
+ if (dow && dow > currentDow) {
1386
+ nextDow = Math.min(nextDow, dow);
1387
+ }
1388
+ }
1389
+ }
1390
+ if (nextDow <= 7) {
1391
+ current = current.add({ days: nextDow - currentDow }).with({ hour: 0, minute: 0, second: 0 });
1392
+ } else {
1393
+ current = current.add({ days: 7 - currentDow + 1 }).with({ hour: 0, minute: 0, second: 0 });
1394
+ }
1395
+ current = this.applyTimeOverride(current);
1396
+ return current;
1397
+ }
1398
+ return this.nextCandidateSameDate(current);
1399
+ }
1340
1400
  applyBySetPos(list) {
1341
1401
  const { bySetPos } = this.opts;
1342
1402
  if (!bySetPos || !bySetPos.length) return list;