rrule-temporal 1.5.2 β 1.6.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/README.md +1 -3
- package/dist/index.cjs +112 -48
- package/dist/index.d.cts +44 -21
- package/dist/index.d.ts +44 -21
- package/dist/index.js +112 -48
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# RRule Temporal
|
|
2
2
|
|
|
3
|
-
> This library is sponsored by [PostalForm π](https://postalform.com/?utm_source=github&utm_medium=readme&utm_campaign=rrule-temporal) β upload a PDF and we print + mail it via USPS (no printer or stamps needed). The only mailing platform for AI Agents via MCP, and the easiest one for humans!
|
|
4
|
-
|
|
5
3
|
The first and only fully compliant Recurrence rule ([RFC-5545](https://www.rfc-editor.org/rfc/rfc5545.html)) processing JS/TS library built on the Temporal API, now with support for [RFC-7529](https://www.rfc-editor.org/rfc/rfc7529.html) (RSCALE / SKIP) for non-Gregorian calendars.
|
|
6
4
|
The library accepts the familiar `RRULE` format and returns
|
|
7
5
|
`Temporal.ZonedDateTime` instances for easy timeβzone aware scheduling.
|
|
@@ -433,4 +431,4 @@ Both approaches preserve the original calendar, time-zone and nanosecond accurac
|
|
|
433
431
|
|
|
434
432
|
If this library saves you time, sponsorship helps keep it maintained.
|
|
435
433
|
|
|
436
|
-
|
|
434
|
+
> This library is sponsored by [PostalForm π](https://postalform.com/?utm_source=github&utm_medium=readme&utm_campaign=rrule-temporal) β upload a PDF and we print + mail it via USPS (no printer or stamps needed). The only mailing platform for AI Agents via MCP, and the easiest one for humans!
|
package/dist/index.cjs
CHANGED
|
@@ -88,9 +88,27 @@ function parseByDayToken(token) {
|
|
|
88
88
|
weekday
|
|
89
89
|
};
|
|
90
90
|
}
|
|
91
|
+
function isTemporalZonedDateTimeInput(value) {
|
|
92
|
+
return typeof value === "object" && value !== null && typeof value.timeZoneId === "string" && typeof value.toString === "function";
|
|
93
|
+
}
|
|
94
|
+
function normalizeZonedDateTime(value, label) {
|
|
95
|
+
if (!isTemporalZonedDateTimeInput(value)) throw new Error(`${label} must be a ZonedDateTime`);
|
|
96
|
+
try {
|
|
97
|
+
return _js_temporal_polyfill.Temporal.ZonedDateTime.from(value.toString());
|
|
98
|
+
} catch {
|
|
99
|
+
throw new Error(`${label} must be a ZonedDateTime`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function normalizeZonedDateTimeList(values, label) {
|
|
103
|
+
return values?.map((value) => normalizeZonedDateTime(value, label));
|
|
104
|
+
}
|
|
91
105
|
function isIcsOpts(opts) {
|
|
92
106
|
return typeof opts.rruleString === "string";
|
|
93
107
|
}
|
|
108
|
+
function mergeDateLists(parsedDates, suppliedDates) {
|
|
109
|
+
const merged = [...parsedDates ?? [], ...normalizeZonedDateTimeList(suppliedDates, "Manual date") ?? []];
|
|
110
|
+
return merged.length > 0 ? merged : void 0;
|
|
111
|
+
}
|
|
94
112
|
/**
|
|
95
113
|
* Unfold lines according to RFC 5545 specification.
|
|
96
114
|
* Lines can be folded by inserting CRLF followed by a single linear white-space character.
|
|
@@ -119,20 +137,45 @@ function parseIcsDateTime(dateStr, tzid, valueType) {
|
|
|
119
137
|
*/
|
|
120
138
|
function parseDateLines(lines, linePrefix, defaultTzid) {
|
|
121
139
|
const dates = [];
|
|
122
|
-
const regex = new RegExp(`^${linePrefix}(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)`, "i");
|
|
123
140
|
for (const line of lines) {
|
|
124
|
-
const
|
|
125
|
-
if (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
dates.push(...dateValues.map((dateValue) => parseIcsDateTime(dateValue, timezone, valueType)));
|
|
130
|
-
}
|
|
141
|
+
const parsed = parseIcsDatePropertyLine(line, linePrefix);
|
|
142
|
+
if (!parsed) continue;
|
|
143
|
+
const timezone = parsed.tzid || defaultTzid;
|
|
144
|
+
const dateValues = parsed.value.split(",");
|
|
145
|
+
dates.push(...dateValues.map((dateValue) => parseIcsDateTime(dateValue, timezone, parsed.valueType)));
|
|
131
146
|
}
|
|
132
147
|
return dates;
|
|
133
148
|
}
|
|
134
|
-
function
|
|
135
|
-
const
|
|
149
|
+
function parseIcsDatePropertyLine(line, propertyName) {
|
|
150
|
+
const colonIndex = line.indexOf(":");
|
|
151
|
+
if (colonIndex === -1) return null;
|
|
152
|
+
const head = line.slice(0, colonIndex);
|
|
153
|
+
const value = line.slice(colonIndex + 1);
|
|
154
|
+
const [name, ...params] = head.split(";");
|
|
155
|
+
if (name?.toUpperCase() !== propertyName) return null;
|
|
156
|
+
let valueType;
|
|
157
|
+
let tzid;
|
|
158
|
+
for (const param of params) {
|
|
159
|
+
const equalsIndex = param.indexOf("=");
|
|
160
|
+
if (equalsIndex === -1) continue;
|
|
161
|
+
const paramName = param.slice(0, equalsIndex).toUpperCase();
|
|
162
|
+
const paramValue = param.slice(equalsIndex + 1);
|
|
163
|
+
if (paramName === "VALUE") valueType = paramValue.toUpperCase();
|
|
164
|
+
else if (paramName === "TZID") tzid = paramValue;
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
valueType,
|
|
168
|
+
tzid,
|
|
169
|
+
value
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function parseIntegerToken(token, label, strict) {
|
|
173
|
+
const trimmed = token.trim();
|
|
174
|
+
if (strict && !/^[+-]?\d+$/.test(trimmed)) throw new Error(`Invalid ${label} value: ${token}`);
|
|
175
|
+
return parseInt(trimmed, 10);
|
|
176
|
+
}
|
|
177
|
+
function parseNumberArray(val, sort = false, strict = false, label = "number") {
|
|
178
|
+
const arr = val.split(",").map((n) => parseIntegerToken(n, label, strict));
|
|
136
179
|
if (sort) return arr.sort((a, b) => a - b);
|
|
137
180
|
return arr;
|
|
138
181
|
}
|
|
@@ -140,11 +183,11 @@ function parseNumberArray(val, sort = false) {
|
|
|
140
183
|
* Parse BYMONTH values, supporting RFC 7529 leap-month tokens with an "L" suffix (e.g., "5L").
|
|
141
184
|
* Returns a heterogeneous array keeping original tokens for serialization.
|
|
142
185
|
*/
|
|
143
|
-
function parseByMonthArray(val) {
|
|
186
|
+
function parseByMonthArray(val, strict = false) {
|
|
144
187
|
return val.split(",").map((tok) => {
|
|
145
188
|
const t = tok.trim();
|
|
146
189
|
if (/^\d+L$/i.test(t)) return t.toUpperCase();
|
|
147
|
-
const n =
|
|
190
|
+
const n = parseIntegerToken(t, "BYMONTH", strict);
|
|
148
191
|
return Number.isFinite(n) ? n : t;
|
|
149
192
|
});
|
|
150
193
|
}
|
|
@@ -197,9 +240,9 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
197
240
|
const rrLine = lines.find((line) => line.match(/^RRULE:/i));
|
|
198
241
|
const exLines = lines.filter((line) => line.match(/^EXDATE/i));
|
|
199
242
|
const rLines = lines.filter((line) => line.match(/^RDATE/i));
|
|
200
|
-
const
|
|
201
|
-
if (!
|
|
202
|
-
const
|
|
243
|
+
const parsedDtLine = parseIcsDatePropertyLine(dtLine, "DTSTART");
|
|
244
|
+
if (!parsedDtLine) throw new Error("Invalid DTSTART in ICS snippet");
|
|
245
|
+
const { valueType, tzid: dtTzid, value: dtValue } = parsedDtLine;
|
|
203
246
|
dtstartValueType = (valueType || (dtValue?.includes("T") ? "DATE-TIME" : "DATE")).toUpperCase() === "DATE" ? "DATE" : "DATE-TIME";
|
|
204
247
|
dtstartHasTzid = Boolean(dtTzid);
|
|
205
248
|
dtstartIsUtc = Boolean(dtValue?.endsWith("Z"));
|
|
@@ -209,7 +252,7 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
209
252
|
exDate = parseDateLines(exLines, "EXDATE", tzid ?? "UTC");
|
|
210
253
|
rDate = parseDateLines(rLines, "RDATE", tzid ?? "UTC");
|
|
211
254
|
} else {
|
|
212
|
-
parsedDtstart = dtstart;
|
|
255
|
+
parsedDtstart = dtstart ? normalizeZonedDateTime(dtstart, "dtstart") : void 0;
|
|
213
256
|
rruleLine = unfoldedInput;
|
|
214
257
|
if (parsedDtstart) {
|
|
215
258
|
tzid = parsedDtstart.timeZoneId;
|
|
@@ -254,10 +297,10 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
254
297
|
opts.freq = val.toUpperCase();
|
|
255
298
|
break;
|
|
256
299
|
case "INTERVAL":
|
|
257
|
-
opts.interval =
|
|
300
|
+
opts.interval = parseIntegerToken(val, "INTERVAL", strict);
|
|
258
301
|
break;
|
|
259
302
|
case "COUNT":
|
|
260
|
-
opts.count =
|
|
303
|
+
opts.count = parseIntegerToken(val, "COUNT", strict);
|
|
261
304
|
break;
|
|
262
305
|
case "UNTIL": {
|
|
263
306
|
const untilHasTime = val.includes("T");
|
|
@@ -284,31 +327,31 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
284
327
|
break;
|
|
285
328
|
}
|
|
286
329
|
case "BYHOUR":
|
|
287
|
-
opts.byHour = parseNumberArray(val, true);
|
|
330
|
+
opts.byHour = parseNumberArray(val, true, strict, "BYHOUR");
|
|
288
331
|
break;
|
|
289
332
|
case "BYMINUTE":
|
|
290
|
-
opts.byMinute = parseNumberArray(val, true);
|
|
333
|
+
opts.byMinute = parseNumberArray(val, true, strict, "BYMINUTE");
|
|
291
334
|
break;
|
|
292
335
|
case "BYSECOND":
|
|
293
|
-
opts.bySecond = parseNumberArray(val, true);
|
|
336
|
+
opts.bySecond = parseNumberArray(val, true, strict, "BYSECOND");
|
|
294
337
|
break;
|
|
295
338
|
case "BYDAY":
|
|
296
339
|
opts.byDay = val.split(",").map((token) => token.toUpperCase());
|
|
297
340
|
break;
|
|
298
341
|
case "BYMONTH":
|
|
299
|
-
opts.byMonth = parseByMonthArray(val);
|
|
342
|
+
opts.byMonth = parseByMonthArray(val, strict);
|
|
300
343
|
break;
|
|
301
344
|
case "BYMONTHDAY":
|
|
302
|
-
opts.byMonthDay = parseNumberArray(val);
|
|
345
|
+
opts.byMonthDay = parseNumberArray(val, false, strict, "BYMONTHDAY");
|
|
303
346
|
break;
|
|
304
347
|
case "BYYEARDAY":
|
|
305
|
-
opts.byYearDay = parseNumberArray(val);
|
|
348
|
+
opts.byYearDay = parseNumberArray(val, false, strict, "BYYEARDAY");
|
|
306
349
|
break;
|
|
307
350
|
case "BYWEEKNO":
|
|
308
|
-
opts.byWeekNo = parseNumberArray(val);
|
|
351
|
+
opts.byWeekNo = parseNumberArray(val, false, strict, "BYWEEKNO");
|
|
309
352
|
break;
|
|
310
353
|
case "BYSETPOS":
|
|
311
|
-
opts.bySetPos = parseNumberArray(val);
|
|
354
|
+
opts.bySetPos = parseNumberArray(val, false, strict, "BYSETPOS");
|
|
312
355
|
break;
|
|
313
356
|
case "WKST":
|
|
314
357
|
opts.wkst = val?.toUpperCase();
|
|
@@ -328,36 +371,53 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
328
371
|
* This prevents type mismatches when mixing native and polyfill Temporal objects.
|
|
329
372
|
*/
|
|
330
373
|
static normalizeToPolyfill(zdt) {
|
|
331
|
-
return
|
|
374
|
+
return normalizeZonedDateTime(zdt, "Date");
|
|
375
|
+
}
|
|
376
|
+
static toInternalIterator(iterator) {
|
|
377
|
+
return iterator;
|
|
378
|
+
}
|
|
379
|
+
static toPublicDate(date) {
|
|
380
|
+
return date;
|
|
381
|
+
}
|
|
382
|
+
static toPublicDates(dates) {
|
|
383
|
+
return dates;
|
|
332
384
|
}
|
|
333
385
|
constructor(params) {
|
|
334
386
|
let manual;
|
|
335
387
|
if (isIcsOpts(params)) {
|
|
336
388
|
const parsed = parseRRuleString(params.rruleString, params.tzid, params.dtstart, params.strict ?? false);
|
|
337
389
|
if (!parsed.dtstart) throw new Error("dtstart is required - provide it either in rruleString or as a separate parameter");
|
|
390
|
+
const dtstart = RRuleTemporal.normalizeToPolyfill(parsed.dtstart);
|
|
338
391
|
this.tzid = parsed.tzid ?? params.tzid ?? "UTC";
|
|
339
|
-
this.originalDtstart =
|
|
392
|
+
this.originalDtstart = dtstart;
|
|
340
393
|
manual = {
|
|
341
394
|
...parsed,
|
|
395
|
+
dtstart,
|
|
396
|
+
rDate: mergeDateLists(parsed.rDate, params.rDate),
|
|
397
|
+
exDate: mergeDateLists(parsed.exDate, params.exDate),
|
|
342
398
|
count: params.count ?? parsed.count,
|
|
343
|
-
until: params.until
|
|
399
|
+
until: params.until ? RRuleTemporal.normalizeToPolyfill(params.until) : parsed.until,
|
|
344
400
|
strict: params.strict,
|
|
345
401
|
maxIterations: params.maxIterations,
|
|
346
402
|
includeDtstart: params.includeDtstart,
|
|
347
403
|
tzid: this.tzid
|
|
348
404
|
};
|
|
349
405
|
} else {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
406
|
+
const dtstart = normalizeZonedDateTime(params.dtstart, "Manual dtstart");
|
|
407
|
+
manual = {
|
|
408
|
+
...params,
|
|
409
|
+
dtstart,
|
|
410
|
+
until: params.until ? normalizeZonedDateTime(params.until, "Manual until") : void 0,
|
|
411
|
+
rDate: normalizeZonedDateTimeList(params.rDate, "Manual rDate"),
|
|
412
|
+
exDate: normalizeZonedDateTimeList(params.exDate, "Manual exDate")
|
|
413
|
+
};
|
|
414
|
+
manual.tzid = manual.tzid || dtstart.timeZoneId;
|
|
353
415
|
this.tzid = manual.tzid;
|
|
354
|
-
this.originalDtstart =
|
|
416
|
+
this.originalDtstart = dtstart;
|
|
355
417
|
}
|
|
356
418
|
if (!manual.freq) throw new Error("RRULE must include FREQ");
|
|
357
419
|
manual.interval = manual.interval ?? 1;
|
|
358
420
|
if (manual.interval <= 0) throw new Error("Cannot create RRule: interval must be greater than 0");
|
|
359
|
-
if (manual.until && !(manual.until instanceof _js_temporal_polyfill.Temporal.ZonedDateTime)) throw new Error("Manual until must be a ZonedDateTime");
|
|
360
|
-
if (manual.until) manual.until = RRuleTemporal.normalizeToPolyfill(manual.until);
|
|
361
421
|
this.opts = this.sanitizeOpts(manual);
|
|
362
422
|
this.maxIterations = manual.maxIterations ?? 1e4;
|
|
363
423
|
this.includeDtstart = manual.includeDtstart ?? false;
|
|
@@ -839,7 +899,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
839
899
|
});
|
|
840
900
|
}
|
|
841
901
|
options() {
|
|
842
|
-
return this.
|
|
902
|
+
return this.cloneOptions();
|
|
843
903
|
}
|
|
844
904
|
cloneOptions() {
|
|
845
905
|
const { byHour, byMinute, bySecond, byDay, byMonth, byMonthDay, byYearDay, byWeekNo, bySetPos, rDate, exDate, ...rest } = this.opts;
|
|
@@ -1577,6 +1637,9 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1577
1637
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
1578
1638
|
*/
|
|
1579
1639
|
all(iterator) {
|
|
1640
|
+
return RRuleTemporal.toPublicDates(this.allInternal(RRuleTemporal.toInternalIterator(iterator)));
|
|
1641
|
+
}
|
|
1642
|
+
allInternal(iterator) {
|
|
1580
1643
|
if (this.opts.rscale && [
|
|
1581
1644
|
"CHINESE",
|
|
1582
1645
|
"HEBREW",
|
|
@@ -1760,12 +1823,12 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1760
1823
|
* Returns all occurrences of the rule within a specified time window.
|
|
1761
1824
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
1762
1825
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
1763
|
-
* @param inc - Optional boolean flag to include the end
|
|
1826
|
+
* @param inc - Optional boolean flag to include the start and end dates in the results.
|
|
1764
1827
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
1765
1828
|
*/
|
|
1766
1829
|
between(after, before, inc = false) {
|
|
1767
|
-
const startInst = after instanceof Date ? _js_temporal_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
1768
|
-
const endInst = before instanceof Date ? _js_temporal_polyfill.Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
1830
|
+
const startInst = after instanceof Date ? _js_temporal_polyfill.Temporal.Instant.from(after.toISOString()) : normalizeZonedDateTime(after, "after").toInstant();
|
|
1831
|
+
const endInst = before instanceof Date ? _js_temporal_polyfill.Temporal.Instant.from(before.toISOString()) : normalizeZonedDateTime(before, "before").toInstant();
|
|
1769
1832
|
const startZdt = _js_temporal_polyfill.Temporal.Instant.from(startInst).toZonedDateTimeISO(this.tzid);
|
|
1770
1833
|
const beforeZdt = _js_temporal_polyfill.Temporal.Instant.from(endInst).toZonedDateTimeISO(this.tzid);
|
|
1771
1834
|
const tempOpts = { ...this.opts };
|
|
@@ -1834,12 +1897,13 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1834
1897
|
if (_js_temporal_polyfill.Temporal.ZonedDateTime.compare(candidate, dtstartForCompare) < 0) candidate = dtstartForCompare;
|
|
1835
1898
|
tempOpts.dtstart = candidate;
|
|
1836
1899
|
}
|
|
1837
|
-
|
|
1900
|
+
const allDates = new RRuleTemporal(tempOpts).allInternal();
|
|
1901
|
+
return RRuleTemporal.toPublicDates(allDates.filter((date) => {
|
|
1838
1902
|
const inst = date.toInstant();
|
|
1839
1903
|
const afterStart = inc ? _js_temporal_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : _js_temporal_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
|
|
1840
1904
|
const beforeEnd = inc ? _js_temporal_polyfill.Temporal.Instant.compare(inst, endInst) <= 0 : _js_temporal_polyfill.Temporal.Instant.compare(inst, endInst) < 0;
|
|
1841
1905
|
return afterStart && beforeEnd;
|
|
1842
|
-
});
|
|
1906
|
+
}));
|
|
1843
1907
|
}
|
|
1844
1908
|
/**
|
|
1845
1909
|
* Convenience helper: true if the exact instant is an occurrence of the rule.
|
|
@@ -1853,7 +1917,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1853
1917
|
* in the rule's time zone. This ignores time-of-day granularity.
|
|
1854
1918
|
*/
|
|
1855
1919
|
occursOn(date) {
|
|
1856
|
-
const startOfDay = date.toZonedDateTime({
|
|
1920
|
+
const startOfDay = _js_temporal_polyfill.Temporal.PlainDate.from(date.toString()).toZonedDateTime({
|
|
1857
1921
|
timeZone: this.tzid,
|
|
1858
1922
|
plainTime: _js_temporal_polyfill.Temporal.PlainTime.from("00:00")
|
|
1859
1923
|
});
|
|
@@ -1867,9 +1931,9 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1867
1931
|
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
1868
1932
|
*/
|
|
1869
1933
|
next(after = /* @__PURE__ */ new Date(), inc = false) {
|
|
1870
|
-
const afterInst = after instanceof Date ? _js_temporal_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
1934
|
+
const afterInst = after instanceof Date ? _js_temporal_polyfill.Temporal.Instant.from(after.toISOString()) : normalizeZonedDateTime(after, "after").toInstant();
|
|
1871
1935
|
let result = null;
|
|
1872
|
-
this.
|
|
1936
|
+
this.allInternal((occ) => {
|
|
1873
1937
|
const inst = occ.toInstant();
|
|
1874
1938
|
if (inc ? _js_temporal_polyfill.Temporal.Instant.compare(inst, afterInst) >= 0 : _js_temporal_polyfill.Temporal.Instant.compare(inst, afterInst) > 0) {
|
|
1875
1939
|
if (!result || _js_temporal_polyfill.Temporal.ZonedDateTime.compare(occ, result) < 0) result = occ;
|
|
@@ -1877,7 +1941,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1877
1941
|
}
|
|
1878
1942
|
return true;
|
|
1879
1943
|
});
|
|
1880
|
-
return result;
|
|
1944
|
+
return RRuleTemporal.toPublicDate(result);
|
|
1881
1945
|
}
|
|
1882
1946
|
/**
|
|
1883
1947
|
* Returns the previous occurrence of the rule before a specified date.
|
|
@@ -1886,15 +1950,15 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1886
1950
|
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
1887
1951
|
*/
|
|
1888
1952
|
previous(before = /* @__PURE__ */ new Date(), inc = false) {
|
|
1889
|
-
const beforeInst = before instanceof Date ? _js_temporal_polyfill.Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
1953
|
+
const beforeInst = before instanceof Date ? _js_temporal_polyfill.Temporal.Instant.from(before.toISOString()) : normalizeZonedDateTime(before, "before").toInstant();
|
|
1890
1954
|
let prev = null;
|
|
1891
|
-
this.
|
|
1955
|
+
this.allInternal((occ) => {
|
|
1892
1956
|
const inst = occ.toInstant();
|
|
1893
1957
|
if (inc ? _js_temporal_polyfill.Temporal.Instant.compare(inst, beforeInst) > 0 : _js_temporal_polyfill.Temporal.Instant.compare(inst, beforeInst) >= 0) return false;
|
|
1894
1958
|
prev = occ;
|
|
1895
1959
|
return true;
|
|
1896
1960
|
});
|
|
1897
|
-
return prev;
|
|
1961
|
+
return RRuleTemporal.toPublicDate(prev);
|
|
1898
1962
|
}
|
|
1899
1963
|
toString() {
|
|
1900
1964
|
const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Temporal } from "
|
|
1
|
+
import { Temporal } from "temporal-spec";
|
|
2
2
|
|
|
3
3
|
//#region src/RRuleTemporal.d.ts
|
|
4
4
|
declare const allowedFreq: readonly ["YEARLY", "MONTHLY", "WEEKLY", "DAILY", "HOURLY", "MINUTELY", "SECONDLY"];
|
|
@@ -23,11 +23,22 @@ interface BaseOpts {
|
|
|
23
23
|
/** SKIP behavior per RFC 7529: OMIT (default), BACKWARD, FORWARD (requires RSCALE). */
|
|
24
24
|
skip?: 'OMIT' | 'BACKWARD' | 'FORWARD';
|
|
25
25
|
}
|
|
26
|
+
type TemporalZonedDateTime = Temporal.ZonedDateTime;
|
|
27
|
+
type TemporalPlainDate = Temporal.PlainDate;
|
|
28
|
+
interface TemporalZonedDateTimeInput {
|
|
29
|
+
readonly timeZoneId: string;
|
|
30
|
+
toString(): string;
|
|
31
|
+
}
|
|
32
|
+
interface TemporalPlainDateInput {
|
|
33
|
+
toString(): string;
|
|
34
|
+
}
|
|
35
|
+
type RRuleTemporalIterator = (date: TemporalZonedDateTime, i: number) => boolean;
|
|
36
|
+
type DateFilter = Date | TemporalZonedDateTimeInput;
|
|
26
37
|
/**
|
|
27
38
|
* Manual rule definition following the recurrence rule parts defined in
|
|
28
39
|
* RFC 5545 Β§3.3.10.
|
|
29
40
|
*/
|
|
30
|
-
interface
|
|
41
|
+
interface ManualOptions extends BaseOpts {
|
|
31
42
|
/** FREQ: recurrence frequency */
|
|
32
43
|
freq: Freq;
|
|
33
44
|
/** INTERVAL between each occurrence of {@link freq} */
|
|
@@ -35,7 +46,7 @@ interface ManualOpts extends BaseOpts {
|
|
|
35
46
|
/** COUNT: total number of occurrences */
|
|
36
47
|
count?: number;
|
|
37
48
|
/** UNTIL: last possible occurrence */
|
|
38
|
-
until?:
|
|
49
|
+
until?: TemporalZonedDateTimeInput;
|
|
39
50
|
/** BYHOUR: hours to include (0-23) */
|
|
40
51
|
byHour?: number[];
|
|
41
52
|
/** BYMINUTE: minutes to include (0-59) */
|
|
@@ -57,23 +68,31 @@ interface ManualOpts extends BaseOpts {
|
|
|
57
68
|
/** WKST: weekday on which the week starts ("MO".."SU") */
|
|
58
69
|
wkst?: string;
|
|
59
70
|
/** RDATE: additional dates to include */
|
|
60
|
-
rDate?:
|
|
71
|
+
rDate?: TemporalZonedDateTimeInput[];
|
|
61
72
|
/** EXDATE: exception dates to exclude */
|
|
62
|
-
exDate?:
|
|
73
|
+
exDate?: TemporalZonedDateTimeInput[];
|
|
63
74
|
/** DTSTART: first occurrence */
|
|
64
|
-
dtstart:
|
|
75
|
+
dtstart: TemporalZonedDateTimeInput;
|
|
65
76
|
}
|
|
66
|
-
interface
|
|
77
|
+
interface IcsOptions extends BaseOpts {
|
|
67
78
|
rruleString: string;
|
|
68
|
-
dtstart?:
|
|
79
|
+
dtstart?: TemporalZonedDateTimeInput;
|
|
69
80
|
/** COUNT: total number of occurrences, used when missing from rruleString */
|
|
70
81
|
count?: number;
|
|
71
82
|
/** UNTIL: last possible occurrence, used when missing from rruleString */
|
|
72
|
-
until?:
|
|
83
|
+
until?: TemporalZonedDateTimeInput;
|
|
84
|
+
/** RDATE: additional dates to include */
|
|
85
|
+
rDate?: TemporalZonedDateTimeInput[];
|
|
86
|
+
/** EXDATE: exception dates to exclude */
|
|
87
|
+
exDate?: TemporalZonedDateTimeInput[];
|
|
73
88
|
}
|
|
74
|
-
type RRuleOptions =
|
|
75
|
-
type
|
|
76
|
-
|
|
89
|
+
type RRuleOptions = ManualOptions | IcsOptions;
|
|
90
|
+
type RRuleResolvedOptions = Omit<ManualOptions, 'dtstart' | 'until' | 'rDate' | 'exDate'> & {
|
|
91
|
+
dtstart: TemporalZonedDateTime;
|
|
92
|
+
until?: TemporalZonedDateTime;
|
|
93
|
+
rDate?: TemporalZonedDateTime[];
|
|
94
|
+
exDate?: TemporalZonedDateTime[];
|
|
95
|
+
};
|
|
77
96
|
declare class RRuleTemporal {
|
|
78
97
|
private readonly tzid;
|
|
79
98
|
private readonly originalDtstart;
|
|
@@ -93,6 +112,9 @@ declare class RRuleTemporal {
|
|
|
93
112
|
* This prevents type mismatches when mixing native and polyfill Temporal objects.
|
|
94
113
|
*/
|
|
95
114
|
private static normalizeToPolyfill;
|
|
115
|
+
private static toInternalIterator;
|
|
116
|
+
private static toPublicDate;
|
|
117
|
+
private static toPublicDates;
|
|
96
118
|
constructor(params: RRuleOptions);
|
|
97
119
|
private buildParsedByDayTokens;
|
|
98
120
|
private buildByDayIsoDays;
|
|
@@ -120,7 +142,7 @@ declare class RRuleTemporal {
|
|
|
120
142
|
private matchesByYearDay;
|
|
121
143
|
private getIsoWeekInfo;
|
|
122
144
|
private matchesByWeekNo;
|
|
123
|
-
options():
|
|
145
|
+
options(): RRuleResolvedOptions;
|
|
124
146
|
private cloneOptions;
|
|
125
147
|
private cloneUpdateOptions;
|
|
126
148
|
/**
|
|
@@ -131,7 +153,7 @@ declare class RRuleTemporal {
|
|
|
131
153
|
* const updated = rule.with({byMonthDay: [3]});
|
|
132
154
|
* ```
|
|
133
155
|
*/
|
|
134
|
-
with(updates: Partial<
|
|
156
|
+
with(updates: Partial<ManualOptions>): RRuleTemporal;
|
|
135
157
|
private addDtstartIfNeeded;
|
|
136
158
|
private canUseUtcLinearFastPath;
|
|
137
159
|
private canUseUtcWeeklyFastPath;
|
|
@@ -178,7 +200,8 @@ declare class RRuleTemporal {
|
|
|
178
200
|
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
179
201
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
180
202
|
*/
|
|
181
|
-
all(iterator?: RRuleTemporalIterator):
|
|
203
|
+
all(iterator?: RRuleTemporalIterator): TemporalZonedDateTime[];
|
|
204
|
+
private allInternal;
|
|
182
205
|
/**
|
|
183
206
|
* RFC 7529: RSCALE present, simple monthly iteration with SKIP behavior.
|
|
184
207
|
* Handles month-to-month stepping from DTSTART's year/month aiming for DTSTART's day-of-month.
|
|
@@ -221,10 +244,10 @@ declare class RRuleTemporal {
|
|
|
221
244
|
* Returns all occurrences of the rule within a specified time window.
|
|
222
245
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
223
246
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
224
|
-
* @param inc - Optional boolean flag to include the end
|
|
247
|
+
* @param inc - Optional boolean flag to include the start and end dates in the results.
|
|
225
248
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
226
249
|
*/
|
|
227
|
-
between(after: DateFilter, before: DateFilter, inc?: boolean):
|
|
250
|
+
between(after: DateFilter, before: DateFilter, inc?: boolean): TemporalZonedDateTime[];
|
|
228
251
|
/**
|
|
229
252
|
* Convenience helper: true if the exact instant is an occurrence of the rule.
|
|
230
253
|
* This checks full date-time equality (including time and time zone).
|
|
@@ -234,21 +257,21 @@ declare class RRuleTemporal {
|
|
|
234
257
|
* Convenience helper: true if any occurrence falls on the given calendar day
|
|
235
258
|
* in the rule's time zone. This ignores time-of-day granularity.
|
|
236
259
|
*/
|
|
237
|
-
occursOn(date:
|
|
260
|
+
occursOn(date: TemporalPlainDateInput): boolean;
|
|
238
261
|
/**
|
|
239
262
|
* Returns the next occurrence of the rule after a specified date.
|
|
240
263
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
241
264
|
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
242
265
|
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
243
266
|
*/
|
|
244
|
-
next(after?: DateFilter, inc?: boolean):
|
|
267
|
+
next(after?: DateFilter, inc?: boolean): TemporalZonedDateTime | null;
|
|
245
268
|
/**
|
|
246
269
|
* Returns the previous occurrence of the rule before a specified date.
|
|
247
270
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
248
271
|
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
249
272
|
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
250
273
|
*/
|
|
251
|
-
previous(before?: DateFilter, inc?: boolean):
|
|
274
|
+
previous(before?: DateFilter, inc?: boolean): TemporalZonedDateTime | null;
|
|
252
275
|
toString(): string;
|
|
253
276
|
private formatIcsDateTime;
|
|
254
277
|
private joinDates;
|
|
@@ -301,4 +324,4 @@ declare class RRuleTemporal {
|
|
|
301
324
|
private _allRscaleNonGregorian;
|
|
302
325
|
}
|
|
303
326
|
//#endregion
|
|
304
|
-
export { Freq, RRuleOptions, RRuleTemporal, Weekday, allowedFreq, allowedWeekdays, weekdayToIsoDay };
|
|
327
|
+
export { DateFilter, Freq, RRuleOptions, RRuleResolvedOptions, RRuleTemporal, RRuleTemporalIterator, TemporalPlainDate, TemporalPlainDateInput, TemporalZonedDateTime, TemporalZonedDateTimeInput, Weekday, allowedFreq, allowedWeekdays, weekdayToIsoDay };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Temporal } from "
|
|
1
|
+
import { Temporal } from "temporal-spec";
|
|
2
2
|
|
|
3
3
|
//#region src/RRuleTemporal.d.ts
|
|
4
4
|
declare const allowedFreq: readonly ["YEARLY", "MONTHLY", "WEEKLY", "DAILY", "HOURLY", "MINUTELY", "SECONDLY"];
|
|
@@ -23,11 +23,22 @@ interface BaseOpts {
|
|
|
23
23
|
/** SKIP behavior per RFC 7529: OMIT (default), BACKWARD, FORWARD (requires RSCALE). */
|
|
24
24
|
skip?: 'OMIT' | 'BACKWARD' | 'FORWARD';
|
|
25
25
|
}
|
|
26
|
+
type TemporalZonedDateTime = Temporal.ZonedDateTime;
|
|
27
|
+
type TemporalPlainDate = Temporal.PlainDate;
|
|
28
|
+
interface TemporalZonedDateTimeInput {
|
|
29
|
+
readonly timeZoneId: string;
|
|
30
|
+
toString(): string;
|
|
31
|
+
}
|
|
32
|
+
interface TemporalPlainDateInput {
|
|
33
|
+
toString(): string;
|
|
34
|
+
}
|
|
35
|
+
type RRuleTemporalIterator = (date: TemporalZonedDateTime, i: number) => boolean;
|
|
36
|
+
type DateFilter = Date | TemporalZonedDateTimeInput;
|
|
26
37
|
/**
|
|
27
38
|
* Manual rule definition following the recurrence rule parts defined in
|
|
28
39
|
* RFC 5545 Β§3.3.10.
|
|
29
40
|
*/
|
|
30
|
-
interface
|
|
41
|
+
interface ManualOptions extends BaseOpts {
|
|
31
42
|
/** FREQ: recurrence frequency */
|
|
32
43
|
freq: Freq;
|
|
33
44
|
/** INTERVAL between each occurrence of {@link freq} */
|
|
@@ -35,7 +46,7 @@ interface ManualOpts extends BaseOpts {
|
|
|
35
46
|
/** COUNT: total number of occurrences */
|
|
36
47
|
count?: number;
|
|
37
48
|
/** UNTIL: last possible occurrence */
|
|
38
|
-
until?:
|
|
49
|
+
until?: TemporalZonedDateTimeInput;
|
|
39
50
|
/** BYHOUR: hours to include (0-23) */
|
|
40
51
|
byHour?: number[];
|
|
41
52
|
/** BYMINUTE: minutes to include (0-59) */
|
|
@@ -57,23 +68,31 @@ interface ManualOpts extends BaseOpts {
|
|
|
57
68
|
/** WKST: weekday on which the week starts ("MO".."SU") */
|
|
58
69
|
wkst?: string;
|
|
59
70
|
/** RDATE: additional dates to include */
|
|
60
|
-
rDate?:
|
|
71
|
+
rDate?: TemporalZonedDateTimeInput[];
|
|
61
72
|
/** EXDATE: exception dates to exclude */
|
|
62
|
-
exDate?:
|
|
73
|
+
exDate?: TemporalZonedDateTimeInput[];
|
|
63
74
|
/** DTSTART: first occurrence */
|
|
64
|
-
dtstart:
|
|
75
|
+
dtstart: TemporalZonedDateTimeInput;
|
|
65
76
|
}
|
|
66
|
-
interface
|
|
77
|
+
interface IcsOptions extends BaseOpts {
|
|
67
78
|
rruleString: string;
|
|
68
|
-
dtstart?:
|
|
79
|
+
dtstart?: TemporalZonedDateTimeInput;
|
|
69
80
|
/** COUNT: total number of occurrences, used when missing from rruleString */
|
|
70
81
|
count?: number;
|
|
71
82
|
/** UNTIL: last possible occurrence, used when missing from rruleString */
|
|
72
|
-
until?:
|
|
83
|
+
until?: TemporalZonedDateTimeInput;
|
|
84
|
+
/** RDATE: additional dates to include */
|
|
85
|
+
rDate?: TemporalZonedDateTimeInput[];
|
|
86
|
+
/** EXDATE: exception dates to exclude */
|
|
87
|
+
exDate?: TemporalZonedDateTimeInput[];
|
|
73
88
|
}
|
|
74
|
-
type RRuleOptions =
|
|
75
|
-
type
|
|
76
|
-
|
|
89
|
+
type RRuleOptions = ManualOptions | IcsOptions;
|
|
90
|
+
type RRuleResolvedOptions = Omit<ManualOptions, 'dtstart' | 'until' | 'rDate' | 'exDate'> & {
|
|
91
|
+
dtstart: TemporalZonedDateTime;
|
|
92
|
+
until?: TemporalZonedDateTime;
|
|
93
|
+
rDate?: TemporalZonedDateTime[];
|
|
94
|
+
exDate?: TemporalZonedDateTime[];
|
|
95
|
+
};
|
|
77
96
|
declare class RRuleTemporal {
|
|
78
97
|
private readonly tzid;
|
|
79
98
|
private readonly originalDtstart;
|
|
@@ -93,6 +112,9 @@ declare class RRuleTemporal {
|
|
|
93
112
|
* This prevents type mismatches when mixing native and polyfill Temporal objects.
|
|
94
113
|
*/
|
|
95
114
|
private static normalizeToPolyfill;
|
|
115
|
+
private static toInternalIterator;
|
|
116
|
+
private static toPublicDate;
|
|
117
|
+
private static toPublicDates;
|
|
96
118
|
constructor(params: RRuleOptions);
|
|
97
119
|
private buildParsedByDayTokens;
|
|
98
120
|
private buildByDayIsoDays;
|
|
@@ -120,7 +142,7 @@ declare class RRuleTemporal {
|
|
|
120
142
|
private matchesByYearDay;
|
|
121
143
|
private getIsoWeekInfo;
|
|
122
144
|
private matchesByWeekNo;
|
|
123
|
-
options():
|
|
145
|
+
options(): RRuleResolvedOptions;
|
|
124
146
|
private cloneOptions;
|
|
125
147
|
private cloneUpdateOptions;
|
|
126
148
|
/**
|
|
@@ -131,7 +153,7 @@ declare class RRuleTemporal {
|
|
|
131
153
|
* const updated = rule.with({byMonthDay: [3]});
|
|
132
154
|
* ```
|
|
133
155
|
*/
|
|
134
|
-
with(updates: Partial<
|
|
156
|
+
with(updates: Partial<ManualOptions>): RRuleTemporal;
|
|
135
157
|
private addDtstartIfNeeded;
|
|
136
158
|
private canUseUtcLinearFastPath;
|
|
137
159
|
private canUseUtcWeeklyFastPath;
|
|
@@ -178,7 +200,8 @@ declare class RRuleTemporal {
|
|
|
178
200
|
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
179
201
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
180
202
|
*/
|
|
181
|
-
all(iterator?: RRuleTemporalIterator):
|
|
203
|
+
all(iterator?: RRuleTemporalIterator): TemporalZonedDateTime[];
|
|
204
|
+
private allInternal;
|
|
182
205
|
/**
|
|
183
206
|
* RFC 7529: RSCALE present, simple monthly iteration with SKIP behavior.
|
|
184
207
|
* Handles month-to-month stepping from DTSTART's year/month aiming for DTSTART's day-of-month.
|
|
@@ -221,10 +244,10 @@ declare class RRuleTemporal {
|
|
|
221
244
|
* Returns all occurrences of the rule within a specified time window.
|
|
222
245
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
223
246
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
224
|
-
* @param inc - Optional boolean flag to include the end
|
|
247
|
+
* @param inc - Optional boolean flag to include the start and end dates in the results.
|
|
225
248
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
226
249
|
*/
|
|
227
|
-
between(after: DateFilter, before: DateFilter, inc?: boolean):
|
|
250
|
+
between(after: DateFilter, before: DateFilter, inc?: boolean): TemporalZonedDateTime[];
|
|
228
251
|
/**
|
|
229
252
|
* Convenience helper: true if the exact instant is an occurrence of the rule.
|
|
230
253
|
* This checks full date-time equality (including time and time zone).
|
|
@@ -234,21 +257,21 @@ declare class RRuleTemporal {
|
|
|
234
257
|
* Convenience helper: true if any occurrence falls on the given calendar day
|
|
235
258
|
* in the rule's time zone. This ignores time-of-day granularity.
|
|
236
259
|
*/
|
|
237
|
-
occursOn(date:
|
|
260
|
+
occursOn(date: TemporalPlainDateInput): boolean;
|
|
238
261
|
/**
|
|
239
262
|
* Returns the next occurrence of the rule after a specified date.
|
|
240
263
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
241
264
|
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
242
265
|
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
243
266
|
*/
|
|
244
|
-
next(after?: DateFilter, inc?: boolean):
|
|
267
|
+
next(after?: DateFilter, inc?: boolean): TemporalZonedDateTime | null;
|
|
245
268
|
/**
|
|
246
269
|
* Returns the previous occurrence of the rule before a specified date.
|
|
247
270
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
248
271
|
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
249
272
|
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
250
273
|
*/
|
|
251
|
-
previous(before?: DateFilter, inc?: boolean):
|
|
274
|
+
previous(before?: DateFilter, inc?: boolean): TemporalZonedDateTime | null;
|
|
252
275
|
toString(): string;
|
|
253
276
|
private formatIcsDateTime;
|
|
254
277
|
private joinDates;
|
|
@@ -301,4 +324,4 @@ declare class RRuleTemporal {
|
|
|
301
324
|
private _allRscaleNonGregorian;
|
|
302
325
|
}
|
|
303
326
|
//#endregion
|
|
304
|
-
export { Freq, RRuleOptions, RRuleTemporal, Weekday, allowedFreq, allowedWeekdays, weekdayToIsoDay };
|
|
327
|
+
export { DateFilter, Freq, RRuleOptions, RRuleResolvedOptions, RRuleTemporal, RRuleTemporalIterator, TemporalPlainDate, TemporalPlainDateInput, TemporalZonedDateTime, TemporalZonedDateTimeInput, Weekday, allowedFreq, allowedWeekdays, weekdayToIsoDay };
|
package/dist/index.js
CHANGED
|
@@ -87,9 +87,27 @@ function parseByDayToken(token) {
|
|
|
87
87
|
weekday
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
|
+
function isTemporalZonedDateTimeInput(value) {
|
|
91
|
+
return typeof value === "object" && value !== null && typeof value.timeZoneId === "string" && typeof value.toString === "function";
|
|
92
|
+
}
|
|
93
|
+
function normalizeZonedDateTime(value, label) {
|
|
94
|
+
if (!isTemporalZonedDateTimeInput(value)) throw new Error(`${label} must be a ZonedDateTime`);
|
|
95
|
+
try {
|
|
96
|
+
return Temporal.ZonedDateTime.from(value.toString());
|
|
97
|
+
} catch {
|
|
98
|
+
throw new Error(`${label} must be a ZonedDateTime`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function normalizeZonedDateTimeList(values, label) {
|
|
102
|
+
return values?.map((value) => normalizeZonedDateTime(value, label));
|
|
103
|
+
}
|
|
90
104
|
function isIcsOpts(opts) {
|
|
91
105
|
return typeof opts.rruleString === "string";
|
|
92
106
|
}
|
|
107
|
+
function mergeDateLists(parsedDates, suppliedDates) {
|
|
108
|
+
const merged = [...parsedDates ?? [], ...normalizeZonedDateTimeList(suppliedDates, "Manual date") ?? []];
|
|
109
|
+
return merged.length > 0 ? merged : void 0;
|
|
110
|
+
}
|
|
93
111
|
/**
|
|
94
112
|
* Unfold lines according to RFC 5545 specification.
|
|
95
113
|
* Lines can be folded by inserting CRLF followed by a single linear white-space character.
|
|
@@ -118,20 +136,45 @@ function parseIcsDateTime(dateStr, tzid, valueType) {
|
|
|
118
136
|
*/
|
|
119
137
|
function parseDateLines(lines, linePrefix, defaultTzid) {
|
|
120
138
|
const dates = [];
|
|
121
|
-
const regex = new RegExp(`^${linePrefix}(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)`, "i");
|
|
122
139
|
for (const line of lines) {
|
|
123
|
-
const
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
dates.push(...dateValues.map((dateValue) => parseIcsDateTime(dateValue, timezone, valueType)));
|
|
129
|
-
}
|
|
140
|
+
const parsed = parseIcsDatePropertyLine(line, linePrefix);
|
|
141
|
+
if (!parsed) continue;
|
|
142
|
+
const timezone = parsed.tzid || defaultTzid;
|
|
143
|
+
const dateValues = parsed.value.split(",");
|
|
144
|
+
dates.push(...dateValues.map((dateValue) => parseIcsDateTime(dateValue, timezone, parsed.valueType)));
|
|
130
145
|
}
|
|
131
146
|
return dates;
|
|
132
147
|
}
|
|
133
|
-
function
|
|
134
|
-
const
|
|
148
|
+
function parseIcsDatePropertyLine(line, propertyName) {
|
|
149
|
+
const colonIndex = line.indexOf(":");
|
|
150
|
+
if (colonIndex === -1) return null;
|
|
151
|
+
const head = line.slice(0, colonIndex);
|
|
152
|
+
const value = line.slice(colonIndex + 1);
|
|
153
|
+
const [name, ...params] = head.split(";");
|
|
154
|
+
if (name?.toUpperCase() !== propertyName) return null;
|
|
155
|
+
let valueType;
|
|
156
|
+
let tzid;
|
|
157
|
+
for (const param of params) {
|
|
158
|
+
const equalsIndex = param.indexOf("=");
|
|
159
|
+
if (equalsIndex === -1) continue;
|
|
160
|
+
const paramName = param.slice(0, equalsIndex).toUpperCase();
|
|
161
|
+
const paramValue = param.slice(equalsIndex + 1);
|
|
162
|
+
if (paramName === "VALUE") valueType = paramValue.toUpperCase();
|
|
163
|
+
else if (paramName === "TZID") tzid = paramValue;
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
valueType,
|
|
167
|
+
tzid,
|
|
168
|
+
value
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function parseIntegerToken(token, label, strict) {
|
|
172
|
+
const trimmed = token.trim();
|
|
173
|
+
if (strict && !/^[+-]?\d+$/.test(trimmed)) throw new Error(`Invalid ${label} value: ${token}`);
|
|
174
|
+
return parseInt(trimmed, 10);
|
|
175
|
+
}
|
|
176
|
+
function parseNumberArray(val, sort = false, strict = false, label = "number") {
|
|
177
|
+
const arr = val.split(",").map((n) => parseIntegerToken(n, label, strict));
|
|
135
178
|
if (sort) return arr.sort((a, b) => a - b);
|
|
136
179
|
return arr;
|
|
137
180
|
}
|
|
@@ -139,11 +182,11 @@ function parseNumberArray(val, sort = false) {
|
|
|
139
182
|
* Parse BYMONTH values, supporting RFC 7529 leap-month tokens with an "L" suffix (e.g., "5L").
|
|
140
183
|
* Returns a heterogeneous array keeping original tokens for serialization.
|
|
141
184
|
*/
|
|
142
|
-
function parseByMonthArray(val) {
|
|
185
|
+
function parseByMonthArray(val, strict = false) {
|
|
143
186
|
return val.split(",").map((tok) => {
|
|
144
187
|
const t = tok.trim();
|
|
145
188
|
if (/^\d+L$/i.test(t)) return t.toUpperCase();
|
|
146
|
-
const n =
|
|
189
|
+
const n = parseIntegerToken(t, "BYMONTH", strict);
|
|
147
190
|
return Number.isFinite(n) ? n : t;
|
|
148
191
|
});
|
|
149
192
|
}
|
|
@@ -196,9 +239,9 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
196
239
|
const rrLine = lines.find((line) => line.match(/^RRULE:/i));
|
|
197
240
|
const exLines = lines.filter((line) => line.match(/^EXDATE/i));
|
|
198
241
|
const rLines = lines.filter((line) => line.match(/^RDATE/i));
|
|
199
|
-
const
|
|
200
|
-
if (!
|
|
201
|
-
const
|
|
242
|
+
const parsedDtLine = parseIcsDatePropertyLine(dtLine, "DTSTART");
|
|
243
|
+
if (!parsedDtLine) throw new Error("Invalid DTSTART in ICS snippet");
|
|
244
|
+
const { valueType, tzid: dtTzid, value: dtValue } = parsedDtLine;
|
|
202
245
|
dtstartValueType = (valueType || (dtValue?.includes("T") ? "DATE-TIME" : "DATE")).toUpperCase() === "DATE" ? "DATE" : "DATE-TIME";
|
|
203
246
|
dtstartHasTzid = Boolean(dtTzid);
|
|
204
247
|
dtstartIsUtc = Boolean(dtValue?.endsWith("Z"));
|
|
@@ -208,7 +251,7 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
208
251
|
exDate = parseDateLines(exLines, "EXDATE", tzid ?? "UTC");
|
|
209
252
|
rDate = parseDateLines(rLines, "RDATE", tzid ?? "UTC");
|
|
210
253
|
} else {
|
|
211
|
-
parsedDtstart = dtstart;
|
|
254
|
+
parsedDtstart = dtstart ? normalizeZonedDateTime(dtstart, "dtstart") : void 0;
|
|
212
255
|
rruleLine = unfoldedInput;
|
|
213
256
|
if (parsedDtstart) {
|
|
214
257
|
tzid = parsedDtstart.timeZoneId;
|
|
@@ -253,10 +296,10 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
253
296
|
opts.freq = val.toUpperCase();
|
|
254
297
|
break;
|
|
255
298
|
case "INTERVAL":
|
|
256
|
-
opts.interval =
|
|
299
|
+
opts.interval = parseIntegerToken(val, "INTERVAL", strict);
|
|
257
300
|
break;
|
|
258
301
|
case "COUNT":
|
|
259
|
-
opts.count =
|
|
302
|
+
opts.count = parseIntegerToken(val, "COUNT", strict);
|
|
260
303
|
break;
|
|
261
304
|
case "UNTIL": {
|
|
262
305
|
const untilHasTime = val.includes("T");
|
|
@@ -283,31 +326,31 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
283
326
|
break;
|
|
284
327
|
}
|
|
285
328
|
case "BYHOUR":
|
|
286
|
-
opts.byHour = parseNumberArray(val, true);
|
|
329
|
+
opts.byHour = parseNumberArray(val, true, strict, "BYHOUR");
|
|
287
330
|
break;
|
|
288
331
|
case "BYMINUTE":
|
|
289
|
-
opts.byMinute = parseNumberArray(val, true);
|
|
332
|
+
opts.byMinute = parseNumberArray(val, true, strict, "BYMINUTE");
|
|
290
333
|
break;
|
|
291
334
|
case "BYSECOND":
|
|
292
|
-
opts.bySecond = parseNumberArray(val, true);
|
|
335
|
+
opts.bySecond = parseNumberArray(val, true, strict, "BYSECOND");
|
|
293
336
|
break;
|
|
294
337
|
case "BYDAY":
|
|
295
338
|
opts.byDay = val.split(",").map((token) => token.toUpperCase());
|
|
296
339
|
break;
|
|
297
340
|
case "BYMONTH":
|
|
298
|
-
opts.byMonth = parseByMonthArray(val);
|
|
341
|
+
opts.byMonth = parseByMonthArray(val, strict);
|
|
299
342
|
break;
|
|
300
343
|
case "BYMONTHDAY":
|
|
301
|
-
opts.byMonthDay = parseNumberArray(val);
|
|
344
|
+
opts.byMonthDay = parseNumberArray(val, false, strict, "BYMONTHDAY");
|
|
302
345
|
break;
|
|
303
346
|
case "BYYEARDAY":
|
|
304
|
-
opts.byYearDay = parseNumberArray(val);
|
|
347
|
+
opts.byYearDay = parseNumberArray(val, false, strict, "BYYEARDAY");
|
|
305
348
|
break;
|
|
306
349
|
case "BYWEEKNO":
|
|
307
|
-
opts.byWeekNo = parseNumberArray(val);
|
|
350
|
+
opts.byWeekNo = parseNumberArray(val, false, strict, "BYWEEKNO");
|
|
308
351
|
break;
|
|
309
352
|
case "BYSETPOS":
|
|
310
|
-
opts.bySetPos = parseNumberArray(val);
|
|
353
|
+
opts.bySetPos = parseNumberArray(val, false, strict, "BYSETPOS");
|
|
311
354
|
break;
|
|
312
355
|
case "WKST":
|
|
313
356
|
opts.wkst = val?.toUpperCase();
|
|
@@ -327,36 +370,53 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
327
370
|
* This prevents type mismatches when mixing native and polyfill Temporal objects.
|
|
328
371
|
*/
|
|
329
372
|
static normalizeToPolyfill(zdt) {
|
|
330
|
-
return
|
|
373
|
+
return normalizeZonedDateTime(zdt, "Date");
|
|
374
|
+
}
|
|
375
|
+
static toInternalIterator(iterator) {
|
|
376
|
+
return iterator;
|
|
377
|
+
}
|
|
378
|
+
static toPublicDate(date) {
|
|
379
|
+
return date;
|
|
380
|
+
}
|
|
381
|
+
static toPublicDates(dates) {
|
|
382
|
+
return dates;
|
|
331
383
|
}
|
|
332
384
|
constructor(params) {
|
|
333
385
|
let manual;
|
|
334
386
|
if (isIcsOpts(params)) {
|
|
335
387
|
const parsed = parseRRuleString(params.rruleString, params.tzid, params.dtstart, params.strict ?? false);
|
|
336
388
|
if (!parsed.dtstart) throw new Error("dtstart is required - provide it either in rruleString or as a separate parameter");
|
|
389
|
+
const dtstart = RRuleTemporal.normalizeToPolyfill(parsed.dtstart);
|
|
337
390
|
this.tzid = parsed.tzid ?? params.tzid ?? "UTC";
|
|
338
|
-
this.originalDtstart =
|
|
391
|
+
this.originalDtstart = dtstart;
|
|
339
392
|
manual = {
|
|
340
393
|
...parsed,
|
|
394
|
+
dtstart,
|
|
395
|
+
rDate: mergeDateLists(parsed.rDate, params.rDate),
|
|
396
|
+
exDate: mergeDateLists(parsed.exDate, params.exDate),
|
|
341
397
|
count: params.count ?? parsed.count,
|
|
342
|
-
until: params.until
|
|
398
|
+
until: params.until ? RRuleTemporal.normalizeToPolyfill(params.until) : parsed.until,
|
|
343
399
|
strict: params.strict,
|
|
344
400
|
maxIterations: params.maxIterations,
|
|
345
401
|
includeDtstart: params.includeDtstart,
|
|
346
402
|
tzid: this.tzid
|
|
347
403
|
};
|
|
348
404
|
} else {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
405
|
+
const dtstart = normalizeZonedDateTime(params.dtstart, "Manual dtstart");
|
|
406
|
+
manual = {
|
|
407
|
+
...params,
|
|
408
|
+
dtstart,
|
|
409
|
+
until: params.until ? normalizeZonedDateTime(params.until, "Manual until") : void 0,
|
|
410
|
+
rDate: normalizeZonedDateTimeList(params.rDate, "Manual rDate"),
|
|
411
|
+
exDate: normalizeZonedDateTimeList(params.exDate, "Manual exDate")
|
|
412
|
+
};
|
|
413
|
+
manual.tzid = manual.tzid || dtstart.timeZoneId;
|
|
352
414
|
this.tzid = manual.tzid;
|
|
353
|
-
this.originalDtstart =
|
|
415
|
+
this.originalDtstart = dtstart;
|
|
354
416
|
}
|
|
355
417
|
if (!manual.freq) throw new Error("RRULE must include FREQ");
|
|
356
418
|
manual.interval = manual.interval ?? 1;
|
|
357
419
|
if (manual.interval <= 0) throw new Error("Cannot create RRule: interval must be greater than 0");
|
|
358
|
-
if (manual.until && !(manual.until instanceof Temporal.ZonedDateTime)) throw new Error("Manual until must be a ZonedDateTime");
|
|
359
|
-
if (manual.until) manual.until = RRuleTemporal.normalizeToPolyfill(manual.until);
|
|
360
420
|
this.opts = this.sanitizeOpts(manual);
|
|
361
421
|
this.maxIterations = manual.maxIterations ?? 1e4;
|
|
362
422
|
this.includeDtstart = manual.includeDtstart ?? false;
|
|
@@ -838,7 +898,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
838
898
|
});
|
|
839
899
|
}
|
|
840
900
|
options() {
|
|
841
|
-
return this.
|
|
901
|
+
return this.cloneOptions();
|
|
842
902
|
}
|
|
843
903
|
cloneOptions() {
|
|
844
904
|
const { byHour, byMinute, bySecond, byDay, byMonth, byMonthDay, byYearDay, byWeekNo, bySetPos, rDate, exDate, ...rest } = this.opts;
|
|
@@ -1576,6 +1636,9 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1576
1636
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
1577
1637
|
*/
|
|
1578
1638
|
all(iterator) {
|
|
1639
|
+
return RRuleTemporal.toPublicDates(this.allInternal(RRuleTemporal.toInternalIterator(iterator)));
|
|
1640
|
+
}
|
|
1641
|
+
allInternal(iterator) {
|
|
1579
1642
|
if (this.opts.rscale && [
|
|
1580
1643
|
"CHINESE",
|
|
1581
1644
|
"HEBREW",
|
|
@@ -1759,12 +1822,12 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1759
1822
|
* Returns all occurrences of the rule within a specified time window.
|
|
1760
1823
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
1761
1824
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
1762
|
-
* @param inc - Optional boolean flag to include the end
|
|
1825
|
+
* @param inc - Optional boolean flag to include the start and end dates in the results.
|
|
1763
1826
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
1764
1827
|
*/
|
|
1765
1828
|
between(after, before, inc = false) {
|
|
1766
|
-
const startInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
1767
|
-
const endInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
1829
|
+
const startInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : normalizeZonedDateTime(after, "after").toInstant();
|
|
1830
|
+
const endInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : normalizeZonedDateTime(before, "before").toInstant();
|
|
1768
1831
|
const startZdt = Temporal.Instant.from(startInst).toZonedDateTimeISO(this.tzid);
|
|
1769
1832
|
const beforeZdt = Temporal.Instant.from(endInst).toZonedDateTimeISO(this.tzid);
|
|
1770
1833
|
const tempOpts = { ...this.opts };
|
|
@@ -1833,12 +1896,13 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1833
1896
|
if (Temporal.ZonedDateTime.compare(candidate, dtstartForCompare) < 0) candidate = dtstartForCompare;
|
|
1834
1897
|
tempOpts.dtstart = candidate;
|
|
1835
1898
|
}
|
|
1836
|
-
|
|
1899
|
+
const allDates = new RRuleTemporal(tempOpts).allInternal();
|
|
1900
|
+
return RRuleTemporal.toPublicDates(allDates.filter((date) => {
|
|
1837
1901
|
const inst = date.toInstant();
|
|
1838
1902
|
const afterStart = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
1839
1903
|
const beforeEnd = inc ? Temporal.Instant.compare(inst, endInst) <= 0 : Temporal.Instant.compare(inst, endInst) < 0;
|
|
1840
1904
|
return afterStart && beforeEnd;
|
|
1841
|
-
});
|
|
1905
|
+
}));
|
|
1842
1906
|
}
|
|
1843
1907
|
/**
|
|
1844
1908
|
* Convenience helper: true if the exact instant is an occurrence of the rule.
|
|
@@ -1852,7 +1916,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1852
1916
|
* in the rule's time zone. This ignores time-of-day granularity.
|
|
1853
1917
|
*/
|
|
1854
1918
|
occursOn(date) {
|
|
1855
|
-
const startOfDay = date.toZonedDateTime({
|
|
1919
|
+
const startOfDay = Temporal.PlainDate.from(date.toString()).toZonedDateTime({
|
|
1856
1920
|
timeZone: this.tzid,
|
|
1857
1921
|
plainTime: Temporal.PlainTime.from("00:00")
|
|
1858
1922
|
});
|
|
@@ -1866,9 +1930,9 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1866
1930
|
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
1867
1931
|
*/
|
|
1868
1932
|
next(after = /* @__PURE__ */ new Date(), inc = false) {
|
|
1869
|
-
const afterInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
1933
|
+
const afterInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : normalizeZonedDateTime(after, "after").toInstant();
|
|
1870
1934
|
let result = null;
|
|
1871
|
-
this.
|
|
1935
|
+
this.allInternal((occ) => {
|
|
1872
1936
|
const inst = occ.toInstant();
|
|
1873
1937
|
if (inc ? Temporal.Instant.compare(inst, afterInst) >= 0 : Temporal.Instant.compare(inst, afterInst) > 0) {
|
|
1874
1938
|
if (!result || Temporal.ZonedDateTime.compare(occ, result) < 0) result = occ;
|
|
@@ -1876,7 +1940,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1876
1940
|
}
|
|
1877
1941
|
return true;
|
|
1878
1942
|
});
|
|
1879
|
-
return result;
|
|
1943
|
+
return RRuleTemporal.toPublicDate(result);
|
|
1880
1944
|
}
|
|
1881
1945
|
/**
|
|
1882
1946
|
* Returns the previous occurrence of the rule before a specified date.
|
|
@@ -1885,15 +1949,15 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1885
1949
|
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
1886
1950
|
*/
|
|
1887
1951
|
previous(before = /* @__PURE__ */ new Date(), inc = false) {
|
|
1888
|
-
const beforeInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
1952
|
+
const beforeInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : normalizeZonedDateTime(before, "before").toInstant();
|
|
1889
1953
|
let prev = null;
|
|
1890
|
-
this.
|
|
1954
|
+
this.allInternal((occ) => {
|
|
1891
1955
|
const inst = occ.toInstant();
|
|
1892
1956
|
if (inc ? Temporal.Instant.compare(inst, beforeInst) > 0 : Temporal.Instant.compare(inst, beforeInst) >= 0) return false;
|
|
1893
1957
|
prev = occ;
|
|
1894
1958
|
return true;
|
|
1895
1959
|
});
|
|
1896
|
-
return prev;
|
|
1960
|
+
return RRuleTemporal.toPublicDate(prev);
|
|
1897
1961
|
}
|
|
1898
1962
|
toString() {
|
|
1899
1963
|
const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rrule-temporal",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "Recurrence rule (rrule) processing using Temporal PlainDate/PlainDateTime, with cross-timezone and cross-calendar rrule support",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,8 +20,9 @@
|
|
|
20
20
|
"scripts": {
|
|
21
21
|
"build": "tsdown",
|
|
22
22
|
"test": "vitest run",
|
|
23
|
+
"test:types": "npm run build && tsc -p tests/type-compat/tsconfig.json --noEmit",
|
|
23
24
|
"coverage": "vitest run --coverage",
|
|
24
|
-
"format": "
|
|
25
|
+
"format": "prettier --write 'src/**/*.ts'",
|
|
25
26
|
"prepare": "npm run build"
|
|
26
27
|
},
|
|
27
28
|
"repository": {
|
|
@@ -50,11 +51,14 @@
|
|
|
50
51
|
"@types/node": "^25.5.0",
|
|
51
52
|
"@vitest/coverage-v8": "^4.1.0",
|
|
52
53
|
"prettier": "^3.8.1",
|
|
54
|
+
"temporal-polyfill": "^1.0.1",
|
|
53
55
|
"tsdown": "^0.21.4",
|
|
54
56
|
"typescript": "^5.9.3",
|
|
55
57
|
"vitest": "^4.1.0"
|
|
56
58
|
},
|
|
57
59
|
"dependencies": {
|
|
58
|
-
"@js-temporal/polyfill": "^0.5.1"
|
|
59
|
-
|
|
60
|
+
"@js-temporal/polyfill": "^0.5.1",
|
|
61
|
+
"temporal-spec": "^1.0.0"
|
|
62
|
+
},
|
|
63
|
+
"packageManager": "pnpm@10.6.0+sha1.c53a73dd9336949b37f63d82c45beb5b161924b6"
|
|
60
64
|
}
|