rrule-temporal 1.0.0 → 1.1.1

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
@@ -2,7 +2,21 @@
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
6
20
  var __export = (target, all) => {
7
21
  for (var name in all)
8
22
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -27,62 +41,65 @@ var import_polyfill = require("@js-temporal/polyfill");
27
41
  function unfoldLine(foldedLine) {
28
42
  return foldedLine.replace(/\r?\n[ \t]/g, "");
29
43
  }
30
- function parseDateValues(dateValues, tzid) {
44
+ function parseDateValues(dateValues, tzid, valueType) {
31
45
  const dates = [];
32
46
  for (const dateValue of dateValues) {
33
- if (/Z$/.test(dateValue)) {
34
- const iso = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9, 15)}Z`;
35
- dates.push(import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(
36
- tzid || "UTC"
37
- ));
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 }));
38
51
  } else {
39
- const isoDate = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9)}`;
40
- dates.push(import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid));
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
+ }
41
59
  }
42
60
  }
43
61
  return dates;
44
62
  }
45
- function parseRRuleString(input, fallbackDtstart) {
46
- const unfoldedInput = unfoldLine(input);
63
+ function parseRRuleString(input, targetTimezone) {
64
+ var _a;
65
+ const unfoldedInput = unfoldLine(input).trim();
47
66
  let dtstart;
48
67
  let tzid = "UTC";
49
68
  let rruleLine;
50
69
  let exDate = [];
51
70
  let rDate = [];
52
- if (/^DTSTART/mi.test(unfoldedInput)) {
53
- const lines = unfoldedInput.split(/\r?\n/);
54
- const dtLine = lines[0];
71
+ if (/^DTSTART/im.test(unfoldedInput)) {
72
+ const lines = unfoldedInput.split(/\s+/);
73
+ const dtLine = lines.find((line) => line.match(/^DTSTART/i));
55
74
  const rrLine = lines.find((line) => line.match(/^RRULE:/i));
56
75
  const exLines = lines.filter((line) => line.match(/^EXDATE/i));
57
76
  const rLines = lines.filter((line) => line.match(/^RDATE/i));
58
- const m = dtLine?.match(/DTSTART(?:;TZID=([^:]+))?:(\d{8}T\d{6})/i);
59
- if (!m) throw new Error("Invalid DTSTART in ICS snippet");
60
- tzid = m[1] || tzid;
61
- const isoDate = `${m[2].slice(0, 4)}-${m[2].slice(4, 6)}-${m[2].slice(6, 8)}T${m[2].slice(9)}`;
62
- dtstart = import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid);
77
+ const dtMatch = dtLine.match(/DTSTART(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)/i);
78
+ if (!dtMatch) throw new Error("Invalid DTSTART in ICS snippet");
79
+ const [, valueType, dtTzid, dtValue] = dtMatch;
80
+ tzid = (_a = dtTzid != null ? dtTzid : targetTimezone) != null ? _a : tzid;
81
+ dtstart = parseDateValues([dtValue], tzid, valueType)[0];
63
82
  rruleLine = rrLine;
64
83
  for (const exLine of exLines) {
65
- const exMatch = exLine.match(/EXDATE(?:;TZID=([^:]+))?:(.+)/i);
84
+ const exMatch = exLine.match(/EXDATE(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)/i);
66
85
  if (exMatch) {
67
- const exTzid = exMatch[1] || tzid;
68
- const dateValues = exMatch[2].split(",");
69
- exDate.push(...parseDateValues(dateValues, exTzid));
86
+ const [, valueType2, exTzid, dateValuesStr] = exMatch;
87
+ const exTimezone = exTzid || tzid;
88
+ const dateValues = dateValuesStr.split(",");
89
+ exDate.push(...parseDateValues(dateValues, exTimezone, valueType2));
70
90
  }
71
91
  }
72
92
  for (const rLine of rLines) {
73
- const rMatch = rLine.match(/RDATE(?:;TZID=([^:]+))?:(.+)/i);
93
+ const rMatch = rLine.match(/RDATE(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)/i);
74
94
  if (rMatch) {
75
- const rTzid = rMatch[1] || tzid;
76
- const dateValues = rMatch[2].split(",");
77
- rDate.push(...parseDateValues(dateValues, rTzid));
95
+ const [, valueType2, rTzid, dateValuesStr] = rMatch;
96
+ const rTimezone = rTzid || tzid;
97
+ const dateValues = dateValuesStr.split(",");
98
+ rDate.push(...parseDateValues(dateValues, rTimezone, valueType2));
78
99
  }
79
100
  }
80
101
  } else {
81
- if (!fallbackDtstart)
82
- throw new Error("dtstart required when parsing RRULE alone");
83
- dtstart = fallbackDtstart;
84
- tzid = fallbackDtstart.timeZoneId;
85
- rruleLine = unfoldedInput.replace(/^RRULE:/i, "RRULE:");
102
+ throw new Error("dtstart required when parsing RRULE alone");
86
103
  }
87
104
  const parts = rruleLine ? rruleLine.replace(/^RRULE:/i, "").split(";") : [];
88
105
  const opts = {
@@ -107,14 +124,13 @@ function parseRRuleString(input, fallbackDtstart) {
107
124
  case "UNTIL": {
108
125
  if (/Z$/.test(val)) {
109
126
  const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}Z`;
110
- opts.until = import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(
111
- tzid || "UTC"
112
- );
127
+ opts.until = import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
113
128
  } else {
114
129
  const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}`;
115
- opts.until = import_polyfill.Temporal.PlainDateTime.from(iso).toZonedDateTime(
116
- tzid || "UTC"
117
- );
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
+ }
118
134
  }
119
135
  break;
120
136
  }
@@ -152,20 +168,17 @@ function parseRRuleString(input, fallbackDtstart) {
152
168
  }
153
169
  return opts;
154
170
  }
155
- var RRuleTemporal = class {
156
- tzid;
157
- originalDtstart;
158
- opts;
159
- maxIterations;
160
- includeDtstart;
171
+ var RRuleTemporal = class _RRuleTemporal {
161
172
  constructor(params) {
173
+ var _a, _b, _c, _d, _e;
162
174
  let manual;
163
175
  if ("rruleString" in params) {
164
- manual = { ...params, ...parseRRuleString(params.rruleString) };
165
- this.tzid = manual.tzid || "UTC";
166
- this.originalDtstart = manual.dtstart;
176
+ const parsed = parseRRuleString(params.rruleString, params.tzid);
177
+ this.tzid = (_b = (_a = parsed.tzid) != null ? _a : params.tzid) != null ? _b : "UTC";
178
+ this.originalDtstart = parsed.dtstart;
179
+ manual = __spreadValues(__spreadValues({}, params), parsed);
167
180
  } else {
168
- manual = { ...params };
181
+ manual = __spreadValues({}, params);
169
182
  if (typeof manual.dtstart === "string") {
170
183
  throw new Error("Manual dtstart must be a ZonedDateTime");
171
184
  }
@@ -174,7 +187,7 @@ var RRuleTemporal = class {
174
187
  this.originalDtstart = manual.dtstart;
175
188
  }
176
189
  if (!manual.freq) throw new Error("RRULE must include FREQ");
177
- manual.interval = manual.interval ?? 1;
190
+ manual.interval = (_c = manual.interval) != null ? _c : 1;
178
191
  if (manual.interval <= 0) {
179
192
  throw new Error("Cannot create RRule: interval must be greater than 0");
180
193
  }
@@ -182,8 +195,8 @@ var RRuleTemporal = class {
182
195
  throw new Error("Manual until must be a ZonedDateTime");
183
196
  }
184
197
  this.opts = this.sanitizeOpts(manual);
185
- this.maxIterations = manual.maxIterations ?? 1e4;
186
- this.includeDtstart = manual.includeDtstart ?? false;
198
+ this.maxIterations = (_d = manual.maxIterations) != null ? _d : 1e4;
199
+ this.includeDtstart = (_e = manual.includeDtstart) != null ? _e : false;
187
200
  }
188
201
  sanitizeOpts(opts) {
189
202
  const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
@@ -220,6 +233,9 @@ var RRuleTemporal = class {
220
233
  if (opts.bySecond.length === 0) delete opts.bySecond;
221
234
  }
222
235
  if (opts.bySetPos) {
236
+ if (opts.bySetPos.some((p) => p === 0)) {
237
+ throw new Error("bySetPos may not contain 0");
238
+ }
223
239
  opts.bySetPos = opts.bySetPos.filter((n) => Number.isInteger(n) && n !== 0);
224
240
  if (opts.bySetPos.length === 0) delete opts.bySetPos;
225
241
  }
@@ -251,9 +267,10 @@ var RRuleTemporal = class {
251
267
  * present the original date is returned unchanged.
252
268
  */
253
269
  expandByTime(base) {
254
- const hours = this.opts.byHour ?? [base.hour];
255
- const minutes = this.opts.byMinute ?? [base.minute];
256
- const seconds = this.opts.bySecond ?? [base.second];
270
+ var _a, _b, _c;
271
+ const hours = (_a = this.opts.byHour) != null ? _a : [base.hour];
272
+ const minutes = (_b = this.opts.byMinute) != null ? _b : [base.minute];
273
+ const seconds = (_c = this.opts.bySecond) != null ? _c : [base.second];
257
274
  const out = [];
258
275
  for (const h of hours) {
259
276
  for (const m of minutes) {
@@ -327,8 +344,73 @@ var RRuleTemporal = class {
327
344
  return dt;
328
345
  }
329
346
  computeFirst() {
347
+ var _a, _b, _c;
330
348
  let zdt = this.originalDtstart;
331
- if (this.opts.byDay?.length) {
349
+ if (((_a = this.opts.byWeekNo) == null ? void 0 : _a.length) && ["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq)) {
350
+ let targetWeek = this.opts.byWeekNo[0];
351
+ let targetYear = zdt.year;
352
+ while (targetYear <= zdt.year + 10) {
353
+ const jan1 = zdt.with({ year: targetYear, month: 1, day: 1 });
354
+ const dec31 = zdt.with({ year: targetYear, month: 12, day: 31 });
355
+ let hasTargetWeek = false;
356
+ if (targetWeek > 0) {
357
+ let maxWeek = 52;
358
+ if (jan1.dayOfWeek === 4 || dec31.dayOfWeek === 4) {
359
+ maxWeek = 53;
360
+ }
361
+ hasTargetWeek = targetWeek <= maxWeek;
362
+ } else {
363
+ let maxWeek = 52;
364
+ if (jan1.dayOfWeek === 4 || dec31.dayOfWeek === 4) {
365
+ maxWeek = 53;
366
+ }
367
+ hasTargetWeek = -targetWeek <= maxWeek;
368
+ }
369
+ if (hasTargetWeek) {
370
+ const firstThursday = jan1.add({ days: (4 - jan1.dayOfWeek + 7) % 7 });
371
+ let weekStart;
372
+ if (targetWeek > 0) {
373
+ weekStart = firstThursday.subtract({ days: 3 }).add({ weeks: targetWeek - 1 });
374
+ } else {
375
+ const lastWeek = jan1.dayOfWeek === 4 || dec31.dayOfWeek === 4 ? 53 : 52;
376
+ weekStart = firstThursday.subtract({ days: 3 }).add({ weeks: lastWeek + targetWeek });
377
+ }
378
+ 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
+ };
388
+ const targetDays = this.opts.byDay.map((tok) => {
389
+ var _a2;
390
+ return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
391
+ }).filter(Boolean).map((day) => dayMap[day]).filter(Boolean);
392
+ if (targetDays.length) {
393
+ const candidates = targetDays.map((dayOfWeek) => {
394
+ const delta = (dayOfWeek - weekStart.dayOfWeek + 7) % 7;
395
+ return weekStart.add({ days: delta });
396
+ });
397
+ const firstCandidate = candidates.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b))[0];
398
+ if (firstCandidate && import_polyfill.Temporal.ZonedDateTime.compare(firstCandidate, this.originalDtstart) >= 0) {
399
+ zdt = firstCandidate;
400
+ break;
401
+ }
402
+ }
403
+ } else {
404
+ if (import_polyfill.Temporal.ZonedDateTime.compare(weekStart, this.originalDtstart) >= 0) {
405
+ zdt = weekStart;
406
+ break;
407
+ }
408
+ }
409
+ }
410
+ targetYear++;
411
+ }
412
+ }
413
+ if (((_c = this.opts.byDay) == null ? void 0 : _c.length) && !this.opts.byWeekNo) {
332
414
  const dayMap = {
333
415
  MO: 1,
334
416
  TU: 2,
@@ -340,12 +422,11 @@ var RRuleTemporal = class {
340
422
  };
341
423
  let deltas = [];
342
424
  if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && this.opts.byDay.every((tok) => /^[A-Z]{2}$/.test(tok))) {
343
- deltas = this.opts.byDay.map(
344
- (tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7
345
- );
425
+ deltas = this.opts.byDay.map((tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7);
346
426
  } else {
347
427
  deltas = this.opts.byDay.map((tok) => {
348
- const wdTok = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)?.[1];
428
+ var _a2;
429
+ const wdTok = (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
349
430
  return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
350
431
  }).filter((d) => d !== null);
351
432
  }
@@ -354,41 +435,27 @@ var RRuleTemporal = class {
354
435
  }
355
436
  }
356
437
  const { byHour, byMinute, bySecond } = this.opts;
438
+ if (this.opts.freq === "HOURLY" && !byHour && import_polyfill.Temporal.ZonedDateTime.compare(
439
+ zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 }),
440
+ this.originalDtstart
441
+ ) > 0) {
442
+ zdt = zdt.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
443
+ }
357
444
  if (byHour || byMinute || bySecond) {
358
- zdt = this.applyTimeOverride(zdt);
359
- if (import_polyfill.Temporal.Instant.compare(
360
- zdt.toInstant(),
361
- this.originalDtstart.toInstant()
362
- ) < 0) {
363
- zdt = this.applyTimeOverride(this.rawAdvance(this.originalDtstart));
445
+ const candidates = this.expandByTime(zdt);
446
+ for (const candidate of candidates) {
447
+ if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, this.originalDtstart) >= 0) {
448
+ return candidate;
449
+ }
364
450
  }
451
+ zdt = this.applyTimeOverride(this.rawAdvance(zdt));
365
452
  }
366
453
  return zdt;
367
454
  }
368
- // inside class RRuleTemporal:
369
- generateWeeklyOccurrences(sample) {
370
- const dayMap = {
371
- MO: 1,
372
- TU: 2,
373
- WE: 3,
374
- TH: 4,
375
- FR: 5,
376
- SA: 6,
377
- SU: 7
378
- };
379
- const tokens = this.opts.byDay?.length ? [...this.opts.byDay] : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
380
- const occs = tokens.flatMap((tok) => {
381
- const targetDow = dayMap[tok];
382
- const delta = (targetDow - sample.dayOfWeek + 7) % 7;
383
- const sameDate = sample.add({ days: delta });
384
- return this.expandByTime(sameDate);
385
- });
386
- return occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
387
- }
388
455
  // --- NEW: constraint checks ---
389
456
  // 2) Replace your matchesByDay with this:
390
457
  matchesByDay(zdt) {
391
- const { byDay } = this.opts;
458
+ const { byDay, freq } = this.opts;
392
459
  if (!byDay) return true;
393
460
  const dayMap = {
394
461
  MO: 1,
@@ -406,6 +473,10 @@ var RRuleTemporal = class {
406
473
  const weekday = m[2];
407
474
  if (!weekday) continue;
408
475
  const wd = dayMap[weekday];
476
+ if (freq === "DAILY") {
477
+ if (zdt.dayOfWeek === wd) return true;
478
+ continue;
479
+ }
409
480
  if (ord === 0) {
410
481
  if (zdt.dayOfWeek === wd) return true;
411
482
  continue;
@@ -431,9 +502,7 @@ var RRuleTemporal = class {
431
502
  const { byMonthDay } = this.opts;
432
503
  if (!byMonthDay) return true;
433
504
  const lastDay = zdt.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
434
- return byMonthDay.some(
435
- (d) => d > 0 ? zdt.day === d : zdt.day === lastDay + d + 1
436
- );
505
+ return byMonthDay.some((d) => d > 0 ? zdt.day === d : zdt.day === lastDay + d + 1);
437
506
  }
438
507
  matchesAll(zdt) {
439
508
  return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt) && this.matchesByYearDay(zdt) && this.matchesByWeekNo(zdt);
@@ -445,19 +514,31 @@ var RRuleTemporal = class {
445
514
  const last = zdt.with({ month: 12, day: 31 }).dayOfYear;
446
515
  return byYearDay.some((d) => d > 0 ? dayOfYear === d : dayOfYear === last + d + 1);
447
516
  }
517
+ getIsoWeekInfo(zdt) {
518
+ const thursday = zdt.add({ days: 4 - zdt.dayOfWeek });
519
+ 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 });
521
+ const diffDays = thursday.toPlainDate().since(firstThursday.toPlainDate()).days;
522
+ const week = Math.floor(diffDays / 7) + 1;
523
+ return { week, year };
524
+ }
448
525
  matchesByWeekNo(zdt) {
449
- const { byWeekNo, wkst } = this.opts;
526
+ const { byWeekNo } = this.opts;
450
527
  if (!byWeekNo) return true;
451
- const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
452
- const startDow = dayMap[wkst ?? "MO"];
453
- const jan1 = zdt.with({ month: 1, day: 1 });
454
- const delta = (jan1.dayOfWeek - startDow + 7) % 7;
455
- const firstWeekStart = jan1.subtract({ days: delta });
456
- const diffDays = zdt.toPlainDate().since(firstWeekStart.toPlainDate()).days;
457
- const week = Math.floor(diffDays / 7) + 1;
458
- const lastWeekDiff = zdt.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
459
- const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
460
- return byWeekNo.some((n) => n > 0 ? week === n : week === lastWeek + n + 1);
528
+ const { week, year } = this.getIsoWeekInfo(zdt);
529
+ const jan1 = zdt.with({ year, month: 1, day: 1 });
530
+ const dec31 = zdt.with({ year, month: 12, day: 31 });
531
+ let lastWeek = 52;
532
+ if (jan1.dayOfWeek === 4 || dec31.dayOfWeek === 4) {
533
+ lastWeek = 53;
534
+ }
535
+ return byWeekNo.some((wn) => {
536
+ if (wn > 0) {
537
+ return week === wn;
538
+ } else {
539
+ return week === lastWeek + wn + 1;
540
+ }
541
+ });
461
542
  }
462
543
  options() {
463
544
  return this.opts;
@@ -468,6 +549,7 @@ var RRuleTemporal = class {
468
549
  * @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
469
550
  */
470
551
  all(iterator) {
552
+ var _a;
471
553
  if (!this.opts.count && !this.opts.until && !iterator) {
472
554
  throw new Error("all() requires iterator when no COUNT/UNTIL");
473
555
  }
@@ -476,14 +558,14 @@ var RRuleTemporal = class {
476
558
  if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
477
559
  const start = this.originalDtstart;
478
560
  let monthCursor = start.with({ day: 1 });
479
- let matchCount2 = 0;
561
+ let matchCount = 0;
480
562
  if (this.includeDtstart && !this.matchesAll(start)) {
481
- if (iterator && !iterator(start, matchCount2)) {
563
+ if (iterator && !iterator(start, matchCount)) {
482
564
  return this.applyCountLimitAndMergeRDates(dates, iterator);
483
565
  }
484
566
  dates.push(start);
485
- matchCount2++;
486
- if (this.shouldBreakForCountLimit(matchCount2)) {
567
+ matchCount++;
568
+ if (this.shouldBreakForCountLimit(matchCount)) {
487
569
  return this.applyCountLimitAndMergeRDates(dates, iterator);
488
570
  }
489
571
  }
@@ -491,8 +573,9 @@ var RRuleTemporal = class {
491
573
  if (++iterationCount > this.maxIterations) {
492
574
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
493
575
  }
494
- const occs = this.generateMonthlyOccurrences(monthCursor);
495
- if (monthCursor.month === start.month && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) < 0)) {
576
+ let occs = this.generateMonthlyOccurrences(monthCursor);
577
+ occs = this.applyBySetPos(occs);
578
+ if (monthCursor.month === start.month && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) < 0) && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) === 0)) {
496
579
  monthCursor = monthCursor.add({ months: this.opts.interval });
497
580
  continue outer_month;
498
581
  }
@@ -501,12 +584,12 @@ var RRuleTemporal = class {
501
584
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
502
585
  break outer_month;
503
586
  }
504
- if (iterator && !iterator(occ, matchCount2)) {
587
+ if (iterator && !iterator(occ, matchCount)) {
505
588
  break outer_month;
506
589
  }
507
590
  dates.push(occ);
508
- matchCount2++;
509
- if (this.shouldBreakForCountLimit(matchCount2)) {
591
+ matchCount++;
592
+ if (this.shouldBreakForCountLimit(matchCount)) {
510
593
  break outer_month;
511
594
  }
512
595
  }
@@ -515,6 +598,45 @@ var RRuleTemporal = class {
515
598
  return this.applyCountLimitAndMergeRDates(dates, iterator);
516
599
  }
517
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
+ }
518
640
  const start = this.originalDtstart;
519
641
  if (this.includeDtstart && !this.matchesAll(start)) {
520
642
  if (iterator && !iterator(start, 0)) {
@@ -534,39 +656,44 @@ var RRuleTemporal = class {
534
656
  SA: 6,
535
657
  SU: 7
536
658
  };
537
- const tokens = this.opts.byDay ? [...this.opts.byDay] : [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
538
- const dows = tokens.map((tok) => dayMap[tok]).filter((d) => d !== void 0).sort((a, b) => a - b);
659
+ const tokens = this.opts.byDay ? [...this.opts.byDay] : this.opts.byMonthDay && this.opts.byMonthDay.length > 0 ? Object.keys(dayMap) : [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
660
+ const dows = tokens.map((tok) => dayMap[tok.slice(-2)]).filter((d) => d !== void 0).sort((a, b) => a - b);
539
661
  const firstWeekDates = dows.map((dw) => {
540
662
  const delta = (dw - start.dayOfWeek + 7) % 7;
541
663
  return start.add({ days: delta });
542
664
  });
543
- let firstOccurrence = firstWeekDates.reduce(
544
- (a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b
545
- );
546
- const wkstDay = dayMap[this.opts.wkst || "MO"] ?? 1;
665
+ let firstOccurrence = firstWeekDates.reduce((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
666
+ const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
547
667
  const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
548
668
  let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
549
- let matchCount2 = 0;
669
+ let matchCount = 0;
550
670
  outer_week: while (true) {
551
671
  if (++iterationCount > this.maxIterations) {
552
672
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
553
673
  }
554
- const occs = dows.flatMap((dw) => {
674
+ let occs = dows.flatMap((dw) => {
555
675
  const delta = (dw - wkstDay + 7) % 7;
556
676
  const sameDate = weekCursor.add({ days: delta });
557
677
  return this.expandByTime(sameDate);
558
678
  }).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
679
+ occs = this.applyBySetPos(occs);
559
680
  for (const occ of occs) {
560
681
  if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
561
682
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
562
683
  break outer_week;
563
684
  }
564
- if (iterator && !iterator(occ, matchCount2)) {
685
+ if (!this.matchesByMonth(occ)) {
686
+ continue;
687
+ }
688
+ if (!this.matchesByMonthDay(occ)) {
689
+ continue;
690
+ }
691
+ if (iterator && !iterator(occ, matchCount)) {
565
692
  break outer_week;
566
693
  }
567
694
  dates.push(occ);
568
- matchCount2++;
569
- if (this.shouldBreakForCountLimit(matchCount2)) {
695
+ matchCount++;
696
+ if (this.shouldBreakForCountLimit(matchCount)) {
570
697
  break outer_week;
571
698
  }
572
699
  }
@@ -574,7 +701,7 @@ var RRuleTemporal = class {
574
701
  }
575
702
  return this.applyCountLimitAndMergeRDates(dates, iterator);
576
703
  }
577
- if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
704
+ if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay) {
578
705
  const start = this.originalDtstart;
579
706
  if (this.includeDtstart && !this.matchesAll(start)) {
580
707
  if (iterator && !iterator(start, 0)) {
@@ -587,7 +714,7 @@ var RRuleTemporal = class {
587
714
  }
588
715
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
589
716
  let monthOffset = 0;
590
- let matchCount2 = dates.length;
717
+ let matchCount = dates.length;
591
718
  let startMonthIndex = months.findIndex((m) => m >= start.month);
592
719
  if (startMonthIndex === -1) {
593
720
  startMonthIndex = 0;
@@ -608,12 +735,12 @@ var RRuleTemporal = class {
608
735
  break;
609
736
  }
610
737
  if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
611
- if (iterator && !iterator(candidate, matchCount2)) {
738
+ if (iterator && !iterator(candidate, matchCount)) {
612
739
  break;
613
740
  }
614
741
  dates.push(candidate);
615
- matchCount2++;
616
- if (this.shouldBreakForCountLimit(matchCount2)) {
742
+ matchCount++;
743
+ if (this.shouldBreakForCountLimit(matchCount)) {
617
744
  break;
618
745
  }
619
746
  }
@@ -621,7 +748,7 @@ var RRuleTemporal = class {
621
748
  }
622
749
  return this.applyCountLimitAndMergeRDates(dates, iterator);
623
750
  }
624
- if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
751
+ if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay && !this.opts.byYearDay && !this.opts.byWeekNo) {
625
752
  const start = this.originalDtstart;
626
753
  if (this.includeDtstart && !this.matchesAll(start)) {
627
754
  if (iterator && !iterator(start, 0)) {
@@ -634,7 +761,7 @@ var RRuleTemporal = class {
634
761
  }
635
762
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
636
763
  let yearOffset = 0;
637
- let matchCount2 = 0;
764
+ let matchCount = 0;
638
765
  while (true) {
639
766
  if (++iterationCount > this.maxIterations) {
640
767
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
@@ -649,12 +776,12 @@ var RRuleTemporal = class {
649
776
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
650
777
  return dates;
651
778
  }
652
- if (iterator && !iterator(occ, matchCount2)) {
779
+ if (iterator && !iterator(occ, matchCount)) {
653
780
  return dates;
654
781
  }
655
782
  dates.push(occ);
656
- matchCount2++;
657
- if (this.shouldBreakForCountLimit(matchCount2)) {
783
+ matchCount++;
784
+ if (this.shouldBreakForCountLimit(matchCount)) {
658
785
  return this.applyCountLimitAndMergeRDates(dates, iterator);
659
786
  }
660
787
  }
@@ -674,7 +801,7 @@ var RRuleTemporal = class {
674
801
  }
675
802
  }
676
803
  let yearCursor = start.with({ month: 1, day: 1 });
677
- let matchCount2 = 0;
804
+ let matchCount = 0;
678
805
  outer_year: while (true) {
679
806
  if (++iterationCount > this.maxIterations) {
680
807
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
@@ -685,12 +812,12 @@ var RRuleTemporal = class {
685
812
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
686
813
  break outer_year;
687
814
  }
688
- if (iterator && !iterator(occ, matchCount2)) {
815
+ if (iterator && !iterator(occ, matchCount)) {
689
816
  break outer_year;
690
817
  }
691
818
  dates.push(occ);
692
- matchCount2++;
693
- if (this.shouldBreakForCountLimit(matchCount2)) {
819
+ matchCount++;
820
+ if (this.shouldBreakForCountLimit(matchCount)) {
694
821
  break outer_year;
695
822
  }
696
823
  }
@@ -710,7 +837,7 @@ var RRuleTemporal = class {
710
837
  }
711
838
  }
712
839
  let yearCursor = start.with({ month: 1, day: 1 });
713
- let matchCount2 = 0;
840
+ let matchCount = 0;
714
841
  outer_year2: while (true) {
715
842
  if (++iterationCount > this.maxIterations) {
716
843
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
@@ -721,12 +848,12 @@ var RRuleTemporal = class {
721
848
  if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
722
849
  break outer_year2;
723
850
  }
724
- if (iterator && !iterator(occ, matchCount2)) {
851
+ if (iterator && !iterator(occ, matchCount)) {
725
852
  break outer_year2;
726
853
  }
727
854
  dates.push(occ);
728
- matchCount2++;
729
- if (this.shouldBreakForCountLimit(matchCount2)) {
855
+ matchCount++;
856
+ if (this.shouldBreakForCountLimit(matchCount)) {
730
857
  break outer_year2;
731
858
  }
732
859
  }
@@ -734,36 +861,141 @@ var RRuleTemporal = class {
734
861
  }
735
862
  return this.applyCountLimitAndMergeRDates(dates, iterator);
736
863
  }
737
- let current = this.computeFirst();
738
- let matchCount = 0;
739
- if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
740
- if (iterator && !iterator(this.originalDtstart, matchCount)) {
741
- return this.applyCountLimitAndMergeRDates(dates, iterator);
864
+ if (this.opts.freq === "MONTHLY" && this.opts.byYearDay && this.opts.byYearDay.length > 0 && !this.opts.byDay && !this.opts.byMonthDay) {
865
+ const start = this.originalDtstart;
866
+ let matchCount = 0;
867
+ if (this.includeDtstart && !this.matchesAll(start)) {
868
+ if (iterator && !iterator(start, matchCount)) {
869
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
870
+ }
871
+ dates.push(start);
872
+ matchCount++;
873
+ if (this.shouldBreakForCountLimit(matchCount)) {
874
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
875
+ }
742
876
  }
743
- dates.push(this.originalDtstart);
744
- matchCount++;
745
- if (this.shouldBreakForCountLimit(matchCount)) {
746
- return this.applyCountLimitAndMergeRDates(dates, iterator);
877
+ let year = start.year;
878
+ const yearDays = [...this.opts.byYearDay].sort((a, b) => a - b);
879
+ const interval = this.opts.interval;
880
+ const startMonthAbs = start.year * 12 + start.month;
881
+ outer_loop: while (true) {
882
+ if (this.shouldBreakForCountLimit(matchCount)) {
883
+ break;
884
+ }
885
+ if (++iterationCount > this.maxIterations) {
886
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
887
+ }
888
+ const yearStart = start.with({ year, month: 1, day: 1 });
889
+ const lastDayOfYear = yearStart.with({ month: 12, day: 31 }).dayOfYear;
890
+ for (const yd of yearDays) {
891
+ const dayNum = yd > 0 ? yd : lastDayOfYear + yd + 1;
892
+ if (dayNum <= 0 || dayNum > lastDayOfYear) continue;
893
+ const baseOcc = yearStart.add({ days: dayNum - 1 });
894
+ for (const occ of this.expandByTime(baseOcc)) {
895
+ if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
896
+ if (dates.some((d) => import_polyfill.Temporal.ZonedDateTime.compare(d, occ) === 0)) continue;
897
+ const occMonthAbs = occ.year * 12 + occ.month;
898
+ if ((occMonthAbs - startMonthAbs) % interval !== 0) {
899
+ continue;
900
+ }
901
+ if (!this.matchesByMonth(occ)) {
902
+ continue;
903
+ }
904
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
905
+ break outer_loop;
906
+ }
907
+ if (iterator && !iterator(occ, matchCount)) {
908
+ break outer_loop;
909
+ }
910
+ dates.push(occ);
911
+ matchCount++;
912
+ if (this.shouldBreakForCountLimit(matchCount)) {
913
+ break outer_loop;
914
+ }
915
+ }
916
+ }
917
+ year++;
918
+ if (this.opts.until && year > this.opts.until.year + 2) {
919
+ break;
920
+ }
921
+ if (!this.opts.until && this.opts.count) {
922
+ const yearsToScan = Math.ceil(this.opts.count / (this.opts.byYearDay.length || 1)) * interval + 5;
923
+ if (year > start.year + yearsToScan) {
924
+ break;
925
+ }
926
+ }
747
927
  }
928
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
748
929
  }
749
- while (true) {
750
- if (++iterationCount > this.maxIterations) {
751
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
930
+ if (this.opts.freq === "DAILY" && this.opts.bySetPos) {
931
+ let start = this.originalDtstart;
932
+ let matchCount = 0;
933
+ if (this.includeDtstart && !this.matchesAll(start)) {
934
+ if (iterator && !iterator(start, matchCount)) {
935
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
936
+ }
937
+ dates.push(start);
938
+ matchCount++;
939
+ if (this.shouldBreakForCountLimit(matchCount)) {
940
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
941
+ }
752
942
  }
753
- if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
754
- break;
943
+ let dayCursor = start.with({ hour: 0, minute: 0, second: 0, microsecond: 0, nanosecond: 0 });
944
+ outer_daily: while (true) {
945
+ if (++iterationCount > this.maxIterations) {
946
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
947
+ }
948
+ let dayOccs = this.expandByTime(dayCursor);
949
+ dayOccs = dayOccs.filter((occ) => this.matchesAll(occ));
950
+ dayOccs = this.applyBySetPos(dayOccs);
951
+ for (const occ of dayOccs) {
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
+ }
964
+ }
965
+ dayCursor = dayCursor.add({ days: this.opts.interval });
755
966
  }
756
- if (this.matchesAll(current)) {
757
- if (iterator && !iterator(current, matchCount)) {
758
- break;
967
+ } else {
968
+ let current = this.computeFirst();
969
+ let matchCount = 0;
970
+ if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
971
+ if (iterator && !iterator(this.originalDtstart, matchCount)) {
972
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
759
973
  }
760
- dates.push(current);
974
+ dates.push(this.originalDtstart);
761
975
  matchCount++;
762
976
  if (this.shouldBreakForCountLimit(matchCount)) {
977
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
978
+ }
979
+ }
980
+ while (true) {
981
+ if (++iterationCount > this.maxIterations) {
982
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
983
+ }
984
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
763
985
  break;
764
986
  }
987
+ if (this.matchesAll(current)) {
988
+ if (iterator && !iterator(current, matchCount)) {
989
+ break;
990
+ }
991
+ dates.push(current);
992
+ matchCount++;
993
+ if (this.shouldBreakForCountLimit(matchCount)) {
994
+ break;
995
+ }
996
+ }
997
+ current = this.nextCandidateSameDate(current);
765
998
  }
766
- current = this.nextCandidateSameDate(current);
767
999
  }
768
1000
  return this.applyCountLimitAndMergeRDates(dates, iterator);
769
1001
  }
@@ -792,9 +1024,7 @@ var RRuleTemporal = class {
792
1024
  excludeExDates(dates) {
793
1025
  if (!this.opts.exDate || this.opts.exDate.length === 0) return dates;
794
1026
  return dates.filter((date) => {
795
- return !this.opts.exDate.some(
796
- (exDate) => import_polyfill.Temporal.ZonedDateTime.compare(date, exDate) === 0
797
- );
1027
+ return !this.opts.exDate.some((exDate) => import_polyfill.Temporal.ZonedDateTime.compare(date, exDate) === 0);
798
1028
  });
799
1029
  }
800
1030
  /**
@@ -844,140 +1074,20 @@ var RRuleTemporal = class {
844
1074
  between(after, before, inc = false) {
845
1075
  const startInst = after instanceof Date ? import_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
846
1076
  const endInst = before instanceof Date ? import_polyfill.Temporal.Instant.from(before.toISOString()) : before.toInstant();
847
- const results = [];
848
- let iterationCount = 0;
849
- if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
850
- let monthCursor = this.computeFirst().with({ day: 1 });
851
- outer: while (true) {
852
- if (++iterationCount > this.maxIterations) {
853
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
854
- }
855
- const occs = this.generateMonthlyOccurrences(monthCursor);
856
- for (const occ of occs) {
857
- const inst = occ.toInstant();
858
- if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
859
- break outer;
860
- }
861
- const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
862
- if (startOk) {
863
- results.push(occ);
864
- }
865
- }
866
- monthCursor = monthCursor.add({ months: this.opts.interval });
867
- }
868
- return this.mergeRDates(results, startInst, endInst, inc);
869
- }
870
- if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
871
- const start = this.originalDtstart;
872
- const months = [...this.opts.byMonth].sort((a, b) => a - b);
873
- let yearOffset = 0;
874
- outer: while (true) {
875
- if (++iterationCount > this.maxIterations) {
876
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
877
- }
878
- const year = start.year + yearOffset * this.opts.interval;
879
- for (const month of months) {
880
- let occ = start.with({ year, month });
881
- occ = this.applyTimeOverride(occ);
882
- const inst = occ.toInstant();
883
- if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
884
- break outer;
885
- }
886
- const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
887
- if (startOk) {
888
- results.push(occ);
889
- }
890
- }
891
- yearOffset++;
892
- }
893
- return this.mergeRDates(results, startInst, endInst, inc);
894
- }
895
- if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
896
- const start = this.originalDtstart;
897
- let yearCursor = start.with({ month: 1, day: 1 });
898
- outer_year: while (true) {
899
- if (++iterationCount > this.maxIterations) {
900
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
901
- }
902
- const occs = this.generateYearlyOccurrences(yearCursor);
903
- for (const occ of occs) {
904
- const inst = occ.toInstant();
905
- if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
906
- break outer_year;
907
- }
908
- const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
909
- if (startOk) {
910
- results.push(occ);
911
- }
912
- }
913
- yearCursor = yearCursor.add({ years: this.opts.interval });
914
- }
915
- return this.mergeRDates(results, startInst, endInst, inc);
916
- }
917
- if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
918
- const start = this.originalDtstart;
919
- let yearCursor = start.with({ month: 1, day: 1 });
920
- outer_year2: while (true) {
921
- if (++iterationCount > this.maxIterations) {
922
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
923
- }
924
- const occs = this.generateYearlyOccurrences(yearCursor);
925
- for (const occ of occs) {
926
- const inst = occ.toInstant();
927
- if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
928
- break outer_year2;
929
- }
930
- const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
931
- if (startOk) {
932
- results.push(occ);
933
- }
934
- }
935
- yearCursor = yearCursor.add({ years: this.opts.interval });
936
- }
937
- return this.mergeRDates(results, startInst, endInst, inc);
938
- }
939
- if (this.opts.freq === "WEEKLY") {
940
- const startInst2 = after instanceof Date ? import_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
941
- const endInst2 = before instanceof Date ? import_polyfill.Temporal.Instant.from(before.toISOString()) : before.toInstant();
942
- let weekCursor = this.computeFirst();
943
- const results2 = [];
944
- outer: while (true) {
945
- if (++iterationCount > this.maxIterations) {
946
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
947
- }
948
- const occs = this.generateWeeklyOccurrences(weekCursor);
949
- for (const occ of occs) {
950
- const inst = occ.toInstant();
951
- if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst2) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst2) >= 0) {
952
- break outer;
953
- }
954
- const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst2) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst2) > 0;
955
- if (startOk) {
956
- results2.push(occ);
957
- }
958
- }
959
- weekCursor = weekCursor.add({ weeks: this.opts.interval });
960
- }
961
- return this.mergeRDates(results2, startInst2, endInst2, inc);
1077
+ const beforeZdt = import_polyfill.Temporal.Instant.from(endInst).toZonedDateTimeISO(this.tzid);
1078
+ const tempOpts = __spreadValues({}, this.opts);
1079
+ if (!tempOpts.until || import_polyfill.Temporal.ZonedDateTime.compare(beforeZdt, tempOpts.until) < 0) {
1080
+ tempOpts.until = beforeZdt;
962
1081
  }
963
- let current = this.computeFirst();
964
- while (true) {
965
- if (++iterationCount > this.maxIterations) {
966
- throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
967
- }
968
- const inst = current.toInstant();
969
- if (inc) {
970
- if (import_polyfill.Temporal.Instant.compare(inst, endInst) > 0) break;
971
- } else {
972
- if (import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) break;
973
- }
974
- const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
975
- if (startOk && this.matchesAll(current)) {
976
- results.push(current);
977
- }
978
- current = this.nextCandidateSameDate(current);
979
- }
980
- return this.mergeRDates(results, startInst, endInst, inc);
1082
+ delete tempOpts.count;
1083
+ const tempRule = new _RRuleTemporal(tempOpts);
1084
+ const allDates = tempRule.all();
1085
+ return allDates.filter((date) => {
1086
+ const inst = date.toInstant();
1087
+ const afterStart = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
1088
+ const beforeEnd = inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) <= 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) < 0;
1089
+ return afterStart && beforeEnd;
1090
+ });
981
1091
  }
982
1092
  /**
983
1093
  * Returns the next occurrence of the rule after a specified date.
@@ -1020,7 +1130,7 @@ var RRuleTemporal = class {
1020
1130
  toString() {
1021
1131
  const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
1022
1132
  const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
1023
- const parts = [];
1133
+ const rule = [];
1024
1134
  const {
1025
1135
  freq,
1026
1136
  interval,
@@ -1028,29 +1138,52 @@ var RRuleTemporal = class {
1028
1138
  until,
1029
1139
  byHour,
1030
1140
  byMinute,
1141
+ bySecond,
1031
1142
  byDay,
1032
1143
  byMonth,
1033
- byMonthDay
1144
+ byMonthDay,
1145
+ bySetPos,
1146
+ byWeekNo,
1147
+ byYearDay,
1148
+ wkst,
1149
+ rDate,
1150
+ exDate
1034
1151
  } = this.opts;
1035
- parts.push(`FREQ=${freq}`);
1036
- if (interval !== 1) parts.push(`INTERVAL=${interval}`);
1037
- if (count !== void 0) parts.push(`COUNT=${count}`);
1152
+ rule.push(`FREQ=${freq}`);
1153
+ if (interval !== 1) rule.push(`INTERVAL=${interval}`);
1154
+ if (count !== void 0) rule.push(`COUNT=${count}`);
1038
1155
  if (until) {
1039
1156
  const u = until.toInstant().toString().replace(/[-:]/g, "");
1040
- parts.push(`UNTIL=${u.slice(0, 15)}Z`);
1157
+ rule.push(`UNTIL=${u.slice(0, 15)}Z`);
1158
+ }
1159
+ if (byHour) rule.push(`BYHOUR=${byHour.join(",")}`);
1160
+ if (byMinute) rule.push(`BYMINUTE=${byMinute.join(",")}`);
1161
+ if (bySecond) rule.push(`BYSECOND=${bySecond.join(",")}`);
1162
+ if (byDay) rule.push(`BYDAY=${byDay.join(",")}`);
1163
+ if (byMonth) rule.push(`BYMONTH=${byMonth.join(",")}`);
1164
+ if (byMonthDay) rule.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
1165
+ if (bySetPos) rule.push(`BYSETPOS=${bySetPos.join(",")}`);
1166
+ if (byWeekNo) rule.push(`BYWEEKNO=${byWeekNo.join(",")}`);
1167
+ if (byYearDay) rule.push(`BYYEARDAY=${byYearDay.join(",")}`);
1168
+ if (wkst) rule.push(`WKST=${wkst}`);
1169
+ const lines = [dtLine, `RRULE:${rule.join(";")}`];
1170
+ if (rDate) {
1171
+ lines.push(`RDATE:${this.joinDates(rDate)}`);
1041
1172
  }
1042
- if (byHour) parts.push(`BYHOUR=${byHour.join(",")}`);
1043
- if (byMinute) parts.push(`BYMINUTE=${byMinute.join(",")}`);
1044
- if (byDay) parts.push(`BYDAY=${byDay.join(",")}`);
1045
- if (byMonth) parts.push(`BYMONTH=${byMonth.join(",")}`);
1046
- if (byMonthDay) parts.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
1047
- return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
1173
+ if (exDate) {
1174
+ lines.push(`EXDATE:${this.joinDates(exDate)}`);
1175
+ }
1176
+ return lines.join("\n");
1177
+ }
1178
+ joinDates(dates) {
1179
+ return dates.map((d) => d.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z");
1048
1180
  }
1049
1181
  /**
1050
1182
  * Given any date in a month, return all the ZonedDateTimes in that month
1051
1183
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
1052
1184
  */
1053
1185
  generateMonthlyOccurrences(sample) {
1186
+ var _a;
1054
1187
  const { byDay, byMonth, byMonthDay } = this.opts;
1055
1188
  if (byMonth && !byMonth.includes(sample.month)) return [];
1056
1189
  const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
@@ -1063,10 +1196,12 @@ var RRuleTemporal = class {
1063
1196
  return [];
1064
1197
  }
1065
1198
  const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
1066
- return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1199
+ let expanded2 = dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1200
+ return expanded2;
1067
1201
  }
1068
1202
  if (!byDay) {
1069
- return this.expandByTime(sample);
1203
+ const expanded2 = this.expandByTime(sample);
1204
+ return expanded2;
1070
1205
  }
1071
1206
  const dayMap = {
1072
1207
  MO: 1,
@@ -1086,12 +1221,12 @@ var RRuleTemporal = class {
1086
1221
  let cursor = sample.with({ day: 1 });
1087
1222
  while (cursor.month === sample.month) {
1088
1223
  const dow = cursor.dayOfWeek;
1089
- (buckets[dow] ||= []).push(cursor.day);
1224
+ (buckets[dow] || (buckets[dow] = [])).push(cursor.day);
1090
1225
  cursor = cursor.add({ days: 1 });
1091
1226
  }
1092
1227
  const byDayHits = [];
1093
1228
  for (const { ord, wd } of tokens) {
1094
- const list = buckets[wd] ?? [];
1229
+ const list = (_a = buckets[wd]) != null ? _a : [];
1095
1230
  if (!list.length) continue;
1096
1231
  if (ord === 0) {
1097
1232
  for (const d of list) byDayHits.push(d);
@@ -1110,7 +1245,6 @@ var RRuleTemporal = class {
1110
1245
  }
1111
1246
  const hits = finalDays.map((d) => sample.with({ day: d }));
1112
1247
  let expanded = hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1113
- expanded = this.applyBySetPos(expanded);
1114
1248
  return expanded;
1115
1249
  }
1116
1250
  /**
@@ -1119,9 +1253,11 @@ var RRuleTemporal = class {
1119
1253
  * if BYMONTH is not specified.
1120
1254
  */
1121
1255
  generateYearlyOccurrences(sample) {
1122
- const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
1256
+ var _a;
1257
+ const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : this.opts.byMonthDay ? [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] : [this.originalDtstart.month];
1123
1258
  let occs = [];
1124
- if (this.opts.byDay && this.opts.byDay.some((t) => /\d{2}/.test(t))) {
1259
+ const hasOrdinalByDay = this.opts.byDay && this.opts.byDay.some((t) => /^[+-]?\d/.test(t));
1260
+ if (hasOrdinalByDay && !this.opts.byMonth) {
1125
1261
  const dayMap = {
1126
1262
  MO: 1,
1127
1263
  TU: 2,
@@ -1146,9 +1282,7 @@ var RRuleTemporal = class {
1146
1282
  const delta = (dec31.dayOfWeek - wd + 7) % 7;
1147
1283
  dt = dec31.subtract({ days: delta + 7 * (-ord - 1) });
1148
1284
  }
1149
- if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
1150
- occs.push(...this.expandByTime(dt));
1151
- }
1285
+ occs.push(...this.expandByTime(dt));
1152
1286
  }
1153
1287
  } else if (!this.opts.byYearDay && !this.opts.byWeekNo) {
1154
1288
  occs = months.flatMap((m) => {
@@ -1182,7 +1316,10 @@ var RRuleTemporal = class {
1182
1316
  const firstWeekStart = jan1.subtract({ days: delta });
1183
1317
  const lastWeekDiff = sample.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
1184
1318
  const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
1185
- const tokens = this.opts.byDay?.length ? this.opts.byDay.map((tok) => tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)?.[1]) : [this.opts.wkst || "MO"];
1319
+ const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
1320
+ var _a2;
1321
+ return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
1322
+ }) : [this.opts.wkst || "MO"];
1186
1323
  for (const weekNo of this.opts.byWeekNo) {
1187
1324
  const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1188
1325
  const weekStart = firstWeekStart.add({ weeks: weekIndex });
@@ -1212,23 +1349,6 @@ var RRuleTemporal = class {
1212
1349
  }
1213
1350
  return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1214
1351
  }
1215
- mergeRDates(list, startInst, endInst, inc) {
1216
- if (!this.opts.rDate) return this.excludeExDates(list);
1217
- for (const z of this.opts.rDate) {
1218
- const inst = z.toInstant();
1219
- const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
1220
- const endOk = inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) <= 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) < 0;
1221
- if (startOk && endOk) list.push(z);
1222
- }
1223
- list.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1224
- const dedup = [];
1225
- for (const d of list) {
1226
- if (dedup.length === 0 || import_polyfill.Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
1227
- dedup.push(d);
1228
- }
1229
- }
1230
- return this.excludeExDates(dedup);
1231
- }
1232
1352
  };
1233
1353
  // Annotate the CommonJS export names for ESM import in node:
1234
1354
  0 && (module.exports = {