rrule-temporal 0.3.2 → 1.1.0

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 });
@@ -24,60 +38,6 @@ __export(index_exports, {
24
38
  });
25
39
  module.exports = __toCommonJS(index_exports);
26
40
  var import_polyfill = require("@js-temporal/polyfill");
27
- var WEEKDAY_NAMES = [
28
- "Monday",
29
- "Tuesday",
30
- "Wednesday",
31
- "Thursday",
32
- "Friday",
33
- "Saturday",
34
- "Sunday"
35
- ];
36
- var MONTH_NAMES = [
37
- "January",
38
- "February",
39
- "March",
40
- "April",
41
- "May",
42
- "June",
43
- "July",
44
- "August",
45
- "September",
46
- "October",
47
- "November",
48
- "December"
49
- ];
50
- var defaultDateFormatter = (year, month, day) => `${month} ${day}, ${year}`;
51
- function ordinal(n) {
52
- const abs = Math.abs(n);
53
- const suffix = abs % 10 === 1 && abs % 100 !== 11 ? "st" : abs % 10 === 2 && abs % 100 !== 12 ? "nd" : abs % 10 === 3 && abs % 100 !== 13 ? "rd" : "th";
54
- return n < 0 ? `last` : `${abs}${suffix}`;
55
- }
56
- function list(arr, mapFn = (x) => `${x}`, final = "and") {
57
- const mapped = arr.map(mapFn);
58
- if (mapped.length === 1) return mapped[0];
59
- return mapped.slice(0, -1).join(", ") + ` ${final} ` + mapped[mapped.length - 1];
60
- }
61
- function formatByDayToken(tok) {
62
- if (typeof tok === "number") return tok.toString();
63
- const m = tok.match(/^([+-]?\d+)?(MO|TU|WE|TH|FR|SA|SU)$/);
64
- if (!m) return tok;
65
- const ord = m[1] ? parseInt(m[1], 10) : 0;
66
- const weekdayMap = {
67
- MO: 0,
68
- TU: 1,
69
- WE: 2,
70
- TH: 3,
71
- FR: 4,
72
- SA: 5,
73
- SU: 6
74
- };
75
- const idx = weekdayMap[m[2]];
76
- const name = WEEKDAY_NAMES[idx];
77
- if (ord === 0) return name;
78
- if (ord === -1) return `last ${name}`;
79
- return `${ordinal(ord)} ${name}`;
80
- }
81
41
  function unfoldLine(foldedLine) {
82
42
  return foldedLine.replace(/\r?\n[ \t]/g, "");
83
43
  }
@@ -86,9 +46,7 @@ function parseDateValues(dateValues, tzid) {
86
46
  for (const dateValue of dateValues) {
87
47
  if (/Z$/.test(dateValue)) {
88
48
  const iso = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9, 15)}Z`;
89
- dates.push(import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(
90
- tzid || "UTC"
91
- ));
49
+ dates.push(import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC"));
92
50
  } else {
93
51
  const isoDate = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9)}`;
94
52
  dates.push(import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid));
@@ -96,24 +54,24 @@ function parseDateValues(dateValues, tzid) {
96
54
  }
97
55
  return dates;
98
56
  }
99
- function parseRRuleString(input, fallbackDtstart) {
100
- const unfoldedInput = unfoldLine(input);
57
+ function parseRRuleString(input, targetTimezone) {
58
+ var _a, _b;
59
+ const unfoldedInput = unfoldLine(input).trim();
101
60
  let dtstart;
102
61
  let tzid = "UTC";
103
62
  let rruleLine;
104
63
  let exDate = [];
105
64
  let rDate = [];
106
65
  if (/^DTSTART/mi.test(unfoldedInput)) {
107
- const lines = unfoldedInput.split(/\r?\n/);
66
+ const lines = unfoldedInput.split(/\s+/);
108
67
  const dtLine = lines[0];
109
68
  const rrLine = lines.find((line) => line.match(/^RRULE:/i));
110
69
  const exLines = lines.filter((line) => line.match(/^EXDATE/i));
111
70
  const rLines = lines.filter((line) => line.match(/^RDATE/i));
112
- const m = dtLine?.match(/DTSTART(?:;TZID=([^:]+))?:(\d{8}T\d{6})/i);
71
+ const m = dtLine == null ? void 0 : dtLine.match(/DTSTART(?:;TZID=([^:]+))?:(\d{8}T\d{6}Z?)/i);
113
72
  if (!m) throw new Error("Invalid DTSTART in ICS snippet");
114
- tzid = m[1] || tzid;
115
- const isoDate = `${m[2].slice(0, 4)}-${m[2].slice(4, 6)}-${m[2].slice(6, 8)}T${m[2].slice(9)}`;
116
- dtstart = import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid);
73
+ tzid = (_b = (_a = m[1]) != null ? _a : targetTimezone) != null ? _b : tzid;
74
+ dtstart = parseDateValues([m[2]], tzid)[0];
117
75
  rruleLine = rrLine;
118
76
  for (const exLine of exLines) {
119
77
  const exMatch = exLine.match(/EXDATE(?:;TZID=([^:]+))?:(.+)/i);
@@ -132,11 +90,7 @@ function parseRRuleString(input, fallbackDtstart) {
132
90
  }
133
91
  }
134
92
  } else {
135
- if (!fallbackDtstart)
136
- throw new Error("dtstart required when parsing RRULE alone");
137
- dtstart = fallbackDtstart;
138
- tzid = fallbackDtstart.timeZoneId;
139
- rruleLine = unfoldedInput.replace(/^RRULE:/i, "RRULE:");
93
+ throw new Error("dtstart required when parsing RRULE alone");
140
94
  }
141
95
  const parts = rruleLine ? rruleLine.replace(/^RRULE:/i, "").split(";") : [];
142
96
  const opts = {
@@ -206,36 +160,17 @@ function parseRRuleString(input, fallbackDtstart) {
206
160
  }
207
161
  return opts;
208
162
  }
209
- function formatTime(hour, minute = 0) {
210
- const hr12 = (hour + 11) % 12 + 1;
211
- const ampm = hour < 12 ? "AM" : "PM";
212
- if (minute) {
213
- return `${hr12}:${String(minute).padStart(2, "0")} ${ampm}`;
214
- }
215
- return `${hr12} ${ampm}`;
216
- }
217
- function tzAbbreviation(zdt) {
218
- const parts = new Intl.DateTimeFormat("en-US", {
219
- timeZone: zdt.timeZoneId,
220
- timeZoneName: "short",
221
- hour: "numeric"
222
- }).formatToParts(new Date(zdt.epochMilliseconds));
223
- const tzPart = parts.find((p) => p.type === "timeZoneName");
224
- return tzPart?.value || zdt.timeZoneId;
225
- }
226
163
  var RRuleTemporal = class {
227
- tzid;
228
- originalDtstart;
229
- opts;
230
- maxIterations;
231
164
  constructor(params) {
165
+ var _a, _b, _c, _d, _e;
232
166
  let manual;
233
167
  if ("rruleString" in params) {
234
- manual = parseRRuleString(params.rruleString);
235
- this.tzid = manual.tzid || "UTC";
236
- this.originalDtstart = manual.dtstart;
168
+ const parsed = parseRRuleString(params.rruleString, params.tzid);
169
+ this.tzid = (_b = (_a = parsed.tzid) != null ? _a : params.tzid) != null ? _b : "UTC";
170
+ this.originalDtstart = parsed.dtstart;
171
+ manual = __spreadValues(__spreadValues({}, params), parsed);
237
172
  } else {
238
- manual = { ...params };
173
+ manual = __spreadValues({}, params);
239
174
  if (typeof manual.dtstart === "string") {
240
175
  throw new Error("Manual dtstart must be a ZonedDateTime");
241
176
  }
@@ -244,7 +179,7 @@ var RRuleTemporal = class {
244
179
  this.originalDtstart = manual.dtstart;
245
180
  }
246
181
  if (!manual.freq) throw new Error("RRULE must include FREQ");
247
- manual.interval = manual.interval ?? 1;
182
+ manual.interval = (_c = manual.interval) != null ? _c : 1;
248
183
  if (manual.interval <= 0) {
249
184
  throw new Error("Cannot create RRule: interval must be greater than 0");
250
185
  }
@@ -252,7 +187,8 @@ var RRuleTemporal = class {
252
187
  throw new Error("Manual until must be a ZonedDateTime");
253
188
  }
254
189
  this.opts = this.sanitizeOpts(manual);
255
- this.maxIterations = manual.maxIterations ?? 1e4;
190
+ this.maxIterations = (_d = manual.maxIterations) != null ? _d : 1e4;
191
+ this.includeDtstart = (_e = manual.includeDtstart) != null ? _e : false;
256
192
  }
257
193
  sanitizeOpts(opts) {
258
194
  const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
@@ -320,9 +256,10 @@ var RRuleTemporal = class {
320
256
  * present the original date is returned unchanged.
321
257
  */
322
258
  expandByTime(base) {
323
- const hours = this.opts.byHour ?? [base.hour];
324
- const minutes = this.opts.byMinute ?? [base.minute];
325
- const seconds = this.opts.bySecond ?? [base.second];
259
+ var _a, _b, _c;
260
+ const hours = (_a = this.opts.byHour) != null ? _a : [base.hour];
261
+ const minutes = (_b = this.opts.byMinute) != null ? _b : [base.minute];
262
+ const seconds = (_c = this.opts.bySecond) != null ? _c : [base.second];
326
263
  const out = [];
327
264
  for (const h of hours) {
328
265
  for (const m of minutes) {
@@ -396,8 +333,9 @@ var RRuleTemporal = class {
396
333
  return dt;
397
334
  }
398
335
  computeFirst() {
336
+ var _a;
399
337
  let zdt = this.originalDtstart;
400
- if (this.opts.byDay?.length) {
338
+ if ((_a = this.opts.byDay) == null ? void 0 : _a.length) {
401
339
  const dayMap = {
402
340
  MO: 1,
403
341
  TU: 2,
@@ -414,7 +352,8 @@ var RRuleTemporal = class {
414
352
  );
415
353
  } else {
416
354
  deltas = this.opts.byDay.map((tok) => {
417
- const wdTok = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)?.[1];
355
+ var _a2;
356
+ const wdTok = (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
418
357
  return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
419
358
  }).filter((d) => d !== null);
420
359
  }
@@ -436,6 +375,7 @@ var RRuleTemporal = class {
436
375
  }
437
376
  // inside class RRuleTemporal:
438
377
  generateWeeklyOccurrences(sample) {
378
+ var _a;
439
379
  const dayMap = {
440
380
  MO: 1,
441
381
  TU: 2,
@@ -445,7 +385,7 @@ var RRuleTemporal = class {
445
385
  SA: 6,
446
386
  SU: 7
447
387
  };
448
- const tokens = this.opts.byDay?.length ? [...this.opts.byDay] : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
388
+ const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? [...this.opts.byDay] : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
449
389
  const occs = tokens.flatMap((tok) => {
450
390
  const targetDow = dayMap[tok];
451
391
  const delta = (targetDow - sample.dayOfWeek + 7) % 7;
@@ -518,7 +458,7 @@ var RRuleTemporal = class {
518
458
  const { byWeekNo, wkst } = this.opts;
519
459
  if (!byWeekNo) return true;
520
460
  const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
521
- const startDow = dayMap[wkst ?? "MO"];
461
+ const startDow = dayMap[wkst != null ? wkst : "MO"];
522
462
  const jan1 = zdt.with({ month: 1, day: 1 });
523
463
  const delta = (jan1.dayOfWeek - startDow + 7) % 7;
524
464
  const firstWeekStart = jan1.subtract({ days: delta });
@@ -537,6 +477,7 @@ var RRuleTemporal = class {
537
477
  * @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
538
478
  */
539
479
  all(iterator) {
480
+ var _a;
540
481
  if (!this.opts.count && !this.opts.until && !iterator) {
541
482
  throw new Error("all() requires iterator when no COUNT/UNTIL");
542
483
  }
@@ -546,12 +487,22 @@ var RRuleTemporal = class {
546
487
  const start = this.originalDtstart;
547
488
  let monthCursor = start.with({ day: 1 });
548
489
  let matchCount2 = 0;
490
+ if (this.includeDtstart && !this.matchesAll(start)) {
491
+ if (iterator && !iterator(start, matchCount2)) {
492
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
493
+ }
494
+ dates.push(start);
495
+ matchCount2++;
496
+ if (this.shouldBreakForCountLimit(matchCount2)) {
497
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
498
+ }
499
+ }
549
500
  outer_month: while (true) {
550
501
  if (++iterationCount > this.maxIterations) {
551
502
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
552
503
  }
553
504
  const occs = this.generateMonthlyOccurrences(monthCursor);
554
- if (monthCursor.month === start.month && occs.some((o) => import_polyfill.Temporal.ZonedDateTime.compare(o, start) < 0)) {
505
+ 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)) {
555
506
  monthCursor = monthCursor.add({ months: this.opts.interval });
556
507
  continue outer_month;
557
508
  }
@@ -575,6 +526,15 @@ var RRuleTemporal = class {
575
526
  }
576
527
  if (this.opts.freq === "WEEKLY") {
577
528
  const start = this.originalDtstart;
529
+ if (this.includeDtstart && !this.matchesAll(start)) {
530
+ if (iterator && !iterator(start, 0)) {
531
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
532
+ }
533
+ dates.push(start);
534
+ if (this.shouldBreakForCountLimit(1)) {
535
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
536
+ }
537
+ }
578
538
  const dayMap = {
579
539
  MO: 1,
580
540
  TU: 2,
@@ -593,7 +553,7 @@ var RRuleTemporal = class {
593
553
  let firstOccurrence = firstWeekDates.reduce(
594
554
  (a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b
595
555
  );
596
- const wkstDay = dayMap[this.opts.wkst || "MO"] ?? 1;
556
+ const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
597
557
  const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
598
558
  let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
599
559
  let matchCount2 = 0;
@@ -624,8 +584,64 @@ var RRuleTemporal = class {
624
584
  }
625
585
  return this.applyCountLimitAndMergeRDates(dates, iterator);
626
586
  }
587
+ if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
588
+ const start = this.originalDtstart;
589
+ if (this.includeDtstart && !this.matchesAll(start)) {
590
+ if (iterator && !iterator(start, 0)) {
591
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
592
+ }
593
+ dates.push(start);
594
+ if (this.shouldBreakForCountLimit(1)) {
595
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
596
+ }
597
+ }
598
+ const months = [...this.opts.byMonth].sort((a, b) => a - b);
599
+ let monthOffset = 0;
600
+ let matchCount2 = dates.length;
601
+ let startMonthIndex = months.findIndex((m) => m >= start.month);
602
+ if (startMonthIndex === -1) {
603
+ startMonthIndex = 0;
604
+ monthOffset = 1;
605
+ }
606
+ while (true) {
607
+ if (++iterationCount > this.maxIterations) {
608
+ throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
609
+ }
610
+ const monthIndex = startMonthIndex + monthOffset;
611
+ const targetMonth = months[monthIndex % months.length];
612
+ const yearsToAdd = Math.floor(monthIndex / months.length);
613
+ const candidate = start.with({
614
+ year: start.year + yearsToAdd,
615
+ month: targetMonth
616
+ });
617
+ if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(candidate, this.opts.until) > 0) {
618
+ break;
619
+ }
620
+ if (import_polyfill.Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
621
+ if (iterator && !iterator(candidate, matchCount2)) {
622
+ break;
623
+ }
624
+ dates.push(candidate);
625
+ matchCount2++;
626
+ if (this.shouldBreakForCountLimit(matchCount2)) {
627
+ break;
628
+ }
629
+ }
630
+ monthOffset++;
631
+ }
632
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
633
+ }
627
634
  if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
628
635
  const start = this.originalDtstart;
636
+ if (this.includeDtstart && !this.matchesAll(start)) {
637
+ if (iterator && !iterator(start, 0)) {
638
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
639
+ }
640
+ dates.push(start);
641
+ if (this.shouldBreakForCountLimit(1)) {
642
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
643
+ }
644
+ }
629
645
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
630
646
  let yearOffset = 0;
631
647
  let matchCount2 = 0;
@@ -658,6 +674,15 @@ var RRuleTemporal = class {
658
674
  }
659
675
  if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
660
676
  const start = this.originalDtstart;
677
+ if (this.includeDtstart && !this.matchesAll(start)) {
678
+ if (iterator && !iterator(start, 0)) {
679
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
680
+ }
681
+ dates.push(start);
682
+ if (this.shouldBreakForCountLimit(1)) {
683
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
684
+ }
685
+ }
661
686
  let yearCursor = start.with({ month: 1, day: 1 });
662
687
  let matchCount2 = 0;
663
688
  outer_year: while (true) {
@@ -685,6 +710,15 @@ var RRuleTemporal = class {
685
710
  }
686
711
  if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
687
712
  const start = this.originalDtstart;
713
+ if (this.includeDtstart && !this.matchesAll(start)) {
714
+ if (iterator && !iterator(start, 0)) {
715
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
716
+ }
717
+ dates.push(start);
718
+ if (this.shouldBreakForCountLimit(1)) {
719
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
720
+ }
721
+ }
688
722
  let yearCursor = start.with({ month: 1, day: 1 });
689
723
  let matchCount2 = 0;
690
724
  outer_year2: while (true) {
@@ -712,6 +746,16 @@ var RRuleTemporal = class {
712
746
  }
713
747
  let current = this.computeFirst();
714
748
  let matchCount = 0;
749
+ if (this.includeDtstart && import_polyfill.Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
750
+ if (iterator && !iterator(this.originalDtstart, matchCount)) {
751
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
752
+ }
753
+ dates.push(this.originalDtstart);
754
+ matchCount++;
755
+ if (this.shouldBreakForCountLimit(matchCount)) {
756
+ return this.applyCountLimitAndMergeRDates(dates, iterator);
757
+ }
758
+ }
715
759
  while (true) {
716
760
  if (++iterationCount > this.maxIterations) {
717
761
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
@@ -986,7 +1030,7 @@ var RRuleTemporal = class {
986
1030
  toString() {
987
1031
  const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
988
1032
  const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
989
- const parts = [];
1033
+ const rule = [];
990
1034
  const {
991
1035
  freq,
992
1036
  interval,
@@ -994,104 +1038,52 @@ var RRuleTemporal = class {
994
1038
  until,
995
1039
  byHour,
996
1040
  byMinute,
1041
+ bySecond,
997
1042
  byDay,
998
1043
  byMonth,
999
- byMonthDay
1044
+ byMonthDay,
1045
+ bySetPos,
1046
+ byWeekNo,
1047
+ byYearDay,
1048
+ wkst,
1049
+ rDate,
1050
+ exDate
1000
1051
  } = this.opts;
1001
- parts.push(`FREQ=${freq}`);
1002
- if (interval !== 1) parts.push(`INTERVAL=${interval}`);
1003
- if (count !== void 0) parts.push(`COUNT=${count}`);
1052
+ rule.push(`FREQ=${freq}`);
1053
+ if (interval !== 1) rule.push(`INTERVAL=${interval}`);
1054
+ if (count !== void 0) rule.push(`COUNT=${count}`);
1004
1055
  if (until) {
1005
1056
  const u = until.toInstant().toString().replace(/[-:]/g, "");
1006
- parts.push(`UNTIL=${u.slice(0, 15)}Z`);
1007
- }
1008
- if (byHour) parts.push(`BYHOUR=${byHour.join(",")}`);
1009
- if (byMinute) parts.push(`BYMINUTE=${byMinute.join(",")}`);
1010
- if (byDay) parts.push(`BYDAY=${byDay.join(",")}`);
1011
- if (byMonth) parts.push(`BYMONTH=${byMonth.join(",")}`);
1012
- if (byMonthDay) parts.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
1013
- return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
1057
+ rule.push(`UNTIL=${u.slice(0, 15)}Z`);
1058
+ }
1059
+ if (byHour) rule.push(`BYHOUR=${byHour.join(",")}`);
1060
+ if (byMinute) rule.push(`BYMINUTE=${byMinute.join(",")}`);
1061
+ if (bySecond) rule.push(`BYSECOND=${bySecond.join(",")}`);
1062
+ if (byDay) rule.push(`BYDAY=${byDay.join(",")}`);
1063
+ if (byMonth) rule.push(`BYMONTH=${byMonth.join(",")}`);
1064
+ if (byMonthDay) rule.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
1065
+ if (bySetPos) rule.push(`BYSETPOS=${bySetPos.join(",")}`);
1066
+ if (byWeekNo) rule.push(`BYWEEKNO=${byWeekNo.join(",")}`);
1067
+ if (byYearDay) rule.push(`BYYEARDAY=${byYearDay.join(",")}`);
1068
+ if (wkst) rule.push(`WKST=${wkst}`);
1069
+ const lines = [dtLine, `RRULE:${rule.join(";")}`];
1070
+ if (rDate) {
1071
+ lines.push(`RDATE:${this.joinDates(rDate)}`);
1072
+ }
1073
+ if (exDate) {
1074
+ lines.push(`EXDATE:${this.joinDates(exDate)}`);
1075
+ }
1076
+ return lines.join("\n");
1014
1077
  }
1015
- /**
1016
- * Convert this rule to a human readable English description.
1017
- */
1018
- toText(formatter = defaultDateFormatter) {
1019
- const {
1020
- freq,
1021
- interval = 1,
1022
- count,
1023
- until,
1024
- byDay,
1025
- byHour,
1026
- byMinute,
1027
- byMonth,
1028
- byMonthDay
1029
- } = this.opts;
1030
- const parts = ["every"];
1031
- const base = {
1032
- YEARLY: "year",
1033
- MONTHLY: "month",
1034
- WEEKLY: "week",
1035
- DAILY: "day",
1036
- HOURLY: "hour",
1037
- MINUTELY: "minute",
1038
- SECONDLY: "second"
1039
- }[freq];
1040
- const daysNormalized = byDay?.map((d) => d.toUpperCase());
1041
- const isWeekdays = daysNormalized && daysNormalized.length === 5 && ["MO", "TU", "WE", "TH", "FR"].every((d) => daysNormalized.includes(d));
1042
- const isEveryday = daysNormalized && daysNormalized.length === 7 && ["MO", "TU", "WE", "TH", "FR", "SA", "SU"].every(
1043
- (d) => daysNormalized.includes(d)
1044
- );
1045
- if (freq === "WEEKLY" && interval === 1 && isWeekdays) {
1046
- parts.push("weekday");
1047
- } else if (freq === "WEEKLY" && interval === 1 && isEveryday) {
1048
- parts.push("day");
1049
- } else {
1050
- if (interval !== 1) {
1051
- parts.push(interval.toString(), base + "s");
1052
- } else {
1053
- parts.push(base);
1054
- }
1055
- }
1056
- if (freq === "WEEKLY" && byDay && !isWeekdays && !isEveryday) {
1057
- parts.push("on", list(byDay, formatByDayToken));
1058
- } else if (byDay && freq !== "WEEKLY") {
1059
- parts.push("on", list(byDay, formatByDayToken));
1060
- }
1061
- if (byMonth) {
1062
- parts.push(
1063
- "in",
1064
- list(byMonth, (m) => MONTH_NAMES[m - 1])
1065
- );
1066
- }
1067
- if (byMonthDay) {
1068
- parts.push(
1069
- "on the",
1070
- list(byMonthDay, (d) => ordinal(d)),
1071
- "day of the month"
1072
- );
1073
- }
1074
- if (byHour) {
1075
- const minutes = byMinute ?? [0];
1076
- const times = byHour.flatMap((h) => minutes.map((m) => formatTime(h, m)));
1077
- parts.push("at", list(times));
1078
- parts.push(tzAbbreviation(this.originalDtstart));
1079
- }
1080
- if (until) {
1081
- parts.push(
1082
- "until",
1083
- formatter(until.year, MONTH_NAMES[until.month - 1], until.day)
1084
- );
1085
- } else if (count !== void 0) {
1086
- parts.push("for", count.toString(), count === 1 ? "time" : "times");
1087
- }
1088
- return parts.join(" ");
1078
+ joinDates(dates) {
1079
+ return dates.map((d) => d.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z");
1089
1080
  }
1090
1081
  /**
1091
1082
  * Given any date in a month, return all the ZonedDateTimes in that month
1092
1083
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
1093
1084
  */
1094
1085
  generateMonthlyOccurrences(sample) {
1086
+ var _a;
1095
1087
  const { byDay, byMonth, byMonthDay } = this.opts;
1096
1088
  if (byMonth && !byMonth.includes(sample.month)) return [];
1097
1089
  const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
@@ -1127,18 +1119,18 @@ var RRuleTemporal = class {
1127
1119
  let cursor = sample.with({ day: 1 });
1128
1120
  while (cursor.month === sample.month) {
1129
1121
  const dow = cursor.dayOfWeek;
1130
- (buckets[dow] ||= []).push(cursor.day);
1122
+ (buckets[dow] || (buckets[dow] = [])).push(cursor.day);
1131
1123
  cursor = cursor.add({ days: 1 });
1132
1124
  }
1133
1125
  const byDayHits = [];
1134
1126
  for (const { ord, wd } of tokens) {
1135
- const list2 = buckets[wd] ?? [];
1136
- if (!list2.length) continue;
1127
+ const list = (_a = buckets[wd]) != null ? _a : [];
1128
+ if (!list.length) continue;
1137
1129
  if (ord === 0) {
1138
- for (const d of list2) byDayHits.push(d);
1130
+ for (const d of list) byDayHits.push(d);
1139
1131
  } else {
1140
- const idx = ord > 0 ? ord - 1 : list2.length + ord;
1141
- const dayN = list2[idx];
1132
+ const idx = ord > 0 ? ord - 1 : list.length + ord;
1133
+ const dayN = list[idx];
1142
1134
  if (dayN) byDayHits.push(dayN);
1143
1135
  }
1144
1136
  }
@@ -1160,6 +1152,7 @@ var RRuleTemporal = class {
1160
1152
  * if BYMONTH is not specified.
1161
1153
  */
1162
1154
  generateYearlyOccurrences(sample) {
1155
+ var _a;
1163
1156
  const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
1164
1157
  let occs = [];
1165
1158
  if (this.opts.byDay && this.opts.byDay.some((t) => /\d{2}/.test(t))) {
@@ -1223,7 +1216,10 @@ var RRuleTemporal = class {
1223
1216
  const firstWeekStart = jan1.subtract({ days: delta });
1224
1217
  const lastWeekDiff = sample.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
1225
1218
  const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
1226
- 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"];
1219
+ const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
1220
+ var _a2;
1221
+ return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
1222
+ }) : [this.opts.wkst || "MO"];
1227
1223
  for (const weekNo of this.opts.byWeekNo) {
1228
1224
  const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1229
1225
  const weekStart = firstWeekStart.add({ weeks: weekIndex });
@@ -1241,10 +1237,10 @@ var RRuleTemporal = class {
1241
1237
  occs = this.applyBySetPos(occs);
1242
1238
  return occs;
1243
1239
  }
1244
- applyBySetPos(list2) {
1240
+ applyBySetPos(list) {
1245
1241
  const { bySetPos } = this.opts;
1246
- if (!bySetPos || !bySetPos.length) return list2;
1247
- const sorted = [...list2].sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1242
+ if (!bySetPos || !bySetPos.length) return list;
1243
+ const sorted = [...list].sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1248
1244
  const out = [];
1249
1245
  const len = sorted.length;
1250
1246
  for (const pos of bySetPos) {
@@ -1253,17 +1249,17 @@ var RRuleTemporal = class {
1253
1249
  }
1254
1250
  return out.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1255
1251
  }
1256
- mergeRDates(list2, startInst, endInst, inc) {
1257
- if (!this.opts.rDate) return this.excludeExDates(list2);
1252
+ mergeRDates(list, startInst, endInst, inc) {
1253
+ if (!this.opts.rDate) return this.excludeExDates(list);
1258
1254
  for (const z of this.opts.rDate) {
1259
1255
  const inst = z.toInstant();
1260
1256
  const startOk = inc ? import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : import_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
1261
1257
  const endOk = inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) <= 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) < 0;
1262
- if (startOk && endOk) list2.push(z);
1258
+ if (startOk && endOk) list.push(z);
1263
1259
  }
1264
- list2.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1260
+ list.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
1265
1261
  const dedup = [];
1266
- for (const d of list2) {
1262
+ for (const d of list) {
1267
1263
  if (dedup.length === 0 || import_polyfill.Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
1268
1264
  dedup.push(d);
1269
1265
  }