rrule-temporal 1.0.0 → 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 });
@@ -32,9 +46,7 @@ function parseDateValues(dateValues, tzid) {
32
46
  for (const dateValue of dateValues) {
33
47
  if (/Z$/.test(dateValue)) {
34
48
  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
- ));
49
+ dates.push(import_polyfill.Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC"));
38
50
  } else {
39
51
  const isoDate = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9)}`;
40
52
  dates.push(import_polyfill.Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid));
@@ -42,24 +54,24 @@ function parseDateValues(dateValues, tzid) {
42
54
  }
43
55
  return dates;
44
56
  }
45
- function parseRRuleString(input, fallbackDtstart) {
46
- const unfoldedInput = unfoldLine(input);
57
+ function parseRRuleString(input, targetTimezone) {
58
+ var _a, _b;
59
+ const unfoldedInput = unfoldLine(input).trim();
47
60
  let dtstart;
48
61
  let tzid = "UTC";
49
62
  let rruleLine;
50
63
  let exDate = [];
51
64
  let rDate = [];
52
65
  if (/^DTSTART/mi.test(unfoldedInput)) {
53
- const lines = unfoldedInput.split(/\r?\n/);
66
+ const lines = unfoldedInput.split(/\s+/);
54
67
  const dtLine = lines[0];
55
68
  const rrLine = lines.find((line) => line.match(/^RRULE:/i));
56
69
  const exLines = lines.filter((line) => line.match(/^EXDATE/i));
57
70
  const rLines = lines.filter((line) => line.match(/^RDATE/i));
58
- 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);
59
72
  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);
73
+ tzid = (_b = (_a = m[1]) != null ? _a : targetTimezone) != null ? _b : tzid;
74
+ dtstart = parseDateValues([m[2]], tzid)[0];
63
75
  rruleLine = rrLine;
64
76
  for (const exLine of exLines) {
65
77
  const exMatch = exLine.match(/EXDATE(?:;TZID=([^:]+))?:(.+)/i);
@@ -78,11 +90,7 @@ function parseRRuleString(input, fallbackDtstart) {
78
90
  }
79
91
  }
80
92
  } 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:");
93
+ throw new Error("dtstart required when parsing RRULE alone");
86
94
  }
87
95
  const parts = rruleLine ? rruleLine.replace(/^RRULE:/i, "").split(";") : [];
88
96
  const opts = {
@@ -153,19 +161,16 @@ function parseRRuleString(input, fallbackDtstart) {
153
161
  return opts;
154
162
  }
155
163
  var RRuleTemporal = class {
156
- tzid;
157
- originalDtstart;
158
- opts;
159
- maxIterations;
160
- includeDtstart;
161
164
  constructor(params) {
165
+ var _a, _b, _c, _d, _e;
162
166
  let manual;
163
167
  if ("rruleString" in params) {
164
- manual = { ...params, ...parseRRuleString(params.rruleString) };
165
- this.tzid = manual.tzid || "UTC";
166
- 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);
167
172
  } else {
168
- manual = { ...params };
173
+ manual = __spreadValues({}, params);
169
174
  if (typeof manual.dtstart === "string") {
170
175
  throw new Error("Manual dtstart must be a ZonedDateTime");
171
176
  }
@@ -174,7 +179,7 @@ var RRuleTemporal = class {
174
179
  this.originalDtstart = manual.dtstart;
175
180
  }
176
181
  if (!manual.freq) throw new Error("RRULE must include FREQ");
177
- manual.interval = manual.interval ?? 1;
182
+ manual.interval = (_c = manual.interval) != null ? _c : 1;
178
183
  if (manual.interval <= 0) {
179
184
  throw new Error("Cannot create RRule: interval must be greater than 0");
180
185
  }
@@ -182,8 +187,8 @@ var RRuleTemporal = class {
182
187
  throw new Error("Manual until must be a ZonedDateTime");
183
188
  }
184
189
  this.opts = this.sanitizeOpts(manual);
185
- this.maxIterations = manual.maxIterations ?? 1e4;
186
- this.includeDtstart = manual.includeDtstart ?? false;
190
+ this.maxIterations = (_d = manual.maxIterations) != null ? _d : 1e4;
191
+ this.includeDtstart = (_e = manual.includeDtstart) != null ? _e : false;
187
192
  }
188
193
  sanitizeOpts(opts) {
189
194
  const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
@@ -251,9 +256,10 @@ var RRuleTemporal = class {
251
256
  * present the original date is returned unchanged.
252
257
  */
253
258
  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];
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];
257
263
  const out = [];
258
264
  for (const h of hours) {
259
265
  for (const m of minutes) {
@@ -327,8 +333,9 @@ var RRuleTemporal = class {
327
333
  return dt;
328
334
  }
329
335
  computeFirst() {
336
+ var _a;
330
337
  let zdt = this.originalDtstart;
331
- if (this.opts.byDay?.length) {
338
+ if ((_a = this.opts.byDay) == null ? void 0 : _a.length) {
332
339
  const dayMap = {
333
340
  MO: 1,
334
341
  TU: 2,
@@ -345,7 +352,8 @@ var RRuleTemporal = class {
345
352
  );
346
353
  } else {
347
354
  deltas = this.opts.byDay.map((tok) => {
348
- 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];
349
357
  return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
350
358
  }).filter((d) => d !== null);
351
359
  }
@@ -367,6 +375,7 @@ var RRuleTemporal = class {
367
375
  }
368
376
  // inside class RRuleTemporal:
369
377
  generateWeeklyOccurrences(sample) {
378
+ var _a;
370
379
  const dayMap = {
371
380
  MO: 1,
372
381
  TU: 2,
@@ -376,7 +385,7 @@ var RRuleTemporal = class {
376
385
  SA: 6,
377
386
  SU: 7
378
387
  };
379
- 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]];
380
389
  const occs = tokens.flatMap((tok) => {
381
390
  const targetDow = dayMap[tok];
382
391
  const delta = (targetDow - sample.dayOfWeek + 7) % 7;
@@ -449,7 +458,7 @@ var RRuleTemporal = class {
449
458
  const { byWeekNo, wkst } = this.opts;
450
459
  if (!byWeekNo) return true;
451
460
  const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
452
- const startDow = dayMap[wkst ?? "MO"];
461
+ const startDow = dayMap[wkst != null ? wkst : "MO"];
453
462
  const jan1 = zdt.with({ month: 1, day: 1 });
454
463
  const delta = (jan1.dayOfWeek - startDow + 7) % 7;
455
464
  const firstWeekStart = jan1.subtract({ days: delta });
@@ -468,6 +477,7 @@ var RRuleTemporal = class {
468
477
  * @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
469
478
  */
470
479
  all(iterator) {
480
+ var _a;
471
481
  if (!this.opts.count && !this.opts.until && !iterator) {
472
482
  throw new Error("all() requires iterator when no COUNT/UNTIL");
473
483
  }
@@ -492,7 +502,7 @@ var RRuleTemporal = class {
492
502
  throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
493
503
  }
494
504
  const occs = this.generateMonthlyOccurrences(monthCursor);
495
- 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)) {
496
506
  monthCursor = monthCursor.add({ months: this.opts.interval });
497
507
  continue outer_month;
498
508
  }
@@ -543,7 +553,7 @@ var RRuleTemporal = class {
543
553
  let firstOccurrence = firstWeekDates.reduce(
544
554
  (a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b
545
555
  );
546
- const wkstDay = dayMap[this.opts.wkst || "MO"] ?? 1;
556
+ const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
547
557
  const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
548
558
  let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
549
559
  let matchCount2 = 0;
@@ -1020,7 +1030,7 @@ var RRuleTemporal = class {
1020
1030
  toString() {
1021
1031
  const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
1022
1032
  const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
1023
- const parts = [];
1033
+ const rule = [];
1024
1034
  const {
1025
1035
  freq,
1026
1036
  interval,
@@ -1028,29 +1038,52 @@ var RRuleTemporal = class {
1028
1038
  until,
1029
1039
  byHour,
1030
1040
  byMinute,
1041
+ bySecond,
1031
1042
  byDay,
1032
1043
  byMonth,
1033
- byMonthDay
1044
+ byMonthDay,
1045
+ bySetPos,
1046
+ byWeekNo,
1047
+ byYearDay,
1048
+ wkst,
1049
+ rDate,
1050
+ exDate
1034
1051
  } = this.opts;
1035
- parts.push(`FREQ=${freq}`);
1036
- if (interval !== 1) parts.push(`INTERVAL=${interval}`);
1037
- 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}`);
1038
1055
  if (until) {
1039
1056
  const u = until.toInstant().toString().replace(/[-:]/g, "");
1040
- parts.push(`UNTIL=${u.slice(0, 15)}Z`);
1057
+ rule.push(`UNTIL=${u.slice(0, 15)}Z`);
1041
1058
  }
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");
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");
1077
+ }
1078
+ joinDates(dates) {
1079
+ return dates.map((d) => d.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z");
1048
1080
  }
1049
1081
  /**
1050
1082
  * Given any date in a month, return all the ZonedDateTimes in that month
1051
1083
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
1052
1084
  */
1053
1085
  generateMonthlyOccurrences(sample) {
1086
+ var _a;
1054
1087
  const { byDay, byMonth, byMonthDay } = this.opts;
1055
1088
  if (byMonth && !byMonth.includes(sample.month)) return [];
1056
1089
  const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
@@ -1086,12 +1119,12 @@ var RRuleTemporal = class {
1086
1119
  let cursor = sample.with({ day: 1 });
1087
1120
  while (cursor.month === sample.month) {
1088
1121
  const dow = cursor.dayOfWeek;
1089
- (buckets[dow] ||= []).push(cursor.day);
1122
+ (buckets[dow] || (buckets[dow] = [])).push(cursor.day);
1090
1123
  cursor = cursor.add({ days: 1 });
1091
1124
  }
1092
1125
  const byDayHits = [];
1093
1126
  for (const { ord, wd } of tokens) {
1094
- const list = buckets[wd] ?? [];
1127
+ const list = (_a = buckets[wd]) != null ? _a : [];
1095
1128
  if (!list.length) continue;
1096
1129
  if (ord === 0) {
1097
1130
  for (const d of list) byDayHits.push(d);
@@ -1119,6 +1152,7 @@ var RRuleTemporal = class {
1119
1152
  * if BYMONTH is not specified.
1120
1153
  */
1121
1154
  generateYearlyOccurrences(sample) {
1155
+ var _a;
1122
1156
  const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
1123
1157
  let occs = [];
1124
1158
  if (this.opts.byDay && this.opts.byDay.some((t) => /\d{2}/.test(t))) {
@@ -1182,7 +1216,10 @@ var RRuleTemporal = class {
1182
1216
  const firstWeekStart = jan1.subtract({ days: delta });
1183
1217
  const lastWeekDiff = sample.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
1184
1218
  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"];
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"];
1186
1223
  for (const weekNo of this.opts.byWeekNo) {
1187
1224
  const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
1188
1225
  const weekStart = firstWeekStart.add({ weeks: weekIndex });
package/dist/index.d.cts CHANGED
@@ -3,14 +3,13 @@ import { Temporal } from '@js-temporal/polyfill';
3
3
  type Freq = "YEARLY" | "MONTHLY" | "WEEKLY" | "DAILY" | "HOURLY" | "MINUTELY" | "SECONDLY";
4
4
  /**
5
5
  * Shared options for all rule constructors.
6
- *
7
- * @property tzid - Time zone identifier as defined in RFC&nbsp;5545 §3.2.19.
8
- * @property maxIterations - Safety cap when generating occurrences.
9
- * @property includeDtstart - Include DTSTART even if it does not match the rule.
10
6
  */
11
7
  interface BaseOpts {
8
+ /** Time zone identifier as defined in RFC&nbsp;5545 §3.2.19. */
12
9
  tzid?: string;
10
+ /** Safety cap when generating occurrences. */
13
11
  maxIterations?: number;
12
+ /** Include DTSTART as an occurrence even if it does not match the rule pattern. */
14
13
  includeDtstart?: boolean;
15
14
  }
16
15
  /**
@@ -63,9 +62,9 @@ declare class RRuleTemporal {
63
62
  private opts;
64
63
  private maxIterations;
65
64
  private includeDtstart;
66
- constructor(params: {
65
+ constructor(params: ({
67
66
  rruleString: string;
68
- } | ManualOpts);
67
+ } & BaseOpts) | ManualOpts);
69
68
  private sanitizeOpts;
70
69
  private rawAdvance;
71
70
  /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE × BYSECOND
@@ -138,6 +137,7 @@ declare class RRuleTemporal {
138
137
  */
139
138
  previous(before?: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime | null;
140
139
  toString(): string;
140
+ private joinDates;
141
141
  /**
142
142
  * Given any date in a month, return all the ZonedDateTimes in that month
143
143
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
package/dist/index.d.ts CHANGED
@@ -3,14 +3,13 @@ import { Temporal } from '@js-temporal/polyfill';
3
3
  type Freq = "YEARLY" | "MONTHLY" | "WEEKLY" | "DAILY" | "HOURLY" | "MINUTELY" | "SECONDLY";
4
4
  /**
5
5
  * Shared options for all rule constructors.
6
- *
7
- * @property tzid - Time zone identifier as defined in RFC&nbsp;5545 §3.2.19.
8
- * @property maxIterations - Safety cap when generating occurrences.
9
- * @property includeDtstart - Include DTSTART even if it does not match the rule.
10
6
  */
11
7
  interface BaseOpts {
8
+ /** Time zone identifier as defined in RFC&nbsp;5545 §3.2.19. */
12
9
  tzid?: string;
10
+ /** Safety cap when generating occurrences. */
13
11
  maxIterations?: number;
12
+ /** Include DTSTART as an occurrence even if it does not match the rule pattern. */
14
13
  includeDtstart?: boolean;
15
14
  }
16
15
  /**
@@ -63,9 +62,9 @@ declare class RRuleTemporal {
63
62
  private opts;
64
63
  private maxIterations;
65
64
  private includeDtstart;
66
- constructor(params: {
65
+ constructor(params: ({
67
66
  rruleString: string;
68
- } | ManualOpts);
67
+ } & BaseOpts) | ManualOpts);
69
68
  private sanitizeOpts;
70
69
  private rawAdvance;
71
70
  /** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE × BYSECOND
@@ -138,6 +137,7 @@ declare class RRuleTemporal {
138
137
  */
139
138
  previous(before?: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime | null;
140
139
  toString(): string;
140
+ private joinDates;
141
141
  /**
142
142
  * Given any date in a month, return all the ZonedDateTimes in that month
143
143
  * matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  RRuleTemporal
3
- } from "./chunk-U2SCUIWP.js";
3
+ } from "./chunk-6VZV7PDA.js";
4
4
  export {
5
5
  RRuleTemporal
6
6
  };