rrule-temporal 1.5.3 → 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/dist/index.cjs +107 -49
- package/dist/index.d.cts +42 -23
- package/dist/index.d.ts +42 -23
- package/dist/index.js +107 -49
- package/package.json +7 -3
package/dist/index.cjs
CHANGED
|
@@ -88,11 +88,25 @@ 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
|
}
|
|
94
108
|
function mergeDateLists(parsedDates, suppliedDates) {
|
|
95
|
-
const merged = [...parsedDates ?? [], ...suppliedDates ?? []];
|
|
109
|
+
const merged = [...parsedDates ?? [], ...normalizeZonedDateTimeList(suppliedDates, "Manual date") ?? []];
|
|
96
110
|
return merged.length > 0 ? merged : void 0;
|
|
97
111
|
}
|
|
98
112
|
/**
|
|
@@ -123,20 +137,45 @@ function parseIcsDateTime(dateStr, tzid, valueType) {
|
|
|
123
137
|
*/
|
|
124
138
|
function parseDateLines(lines, linePrefix, defaultTzid) {
|
|
125
139
|
const dates = [];
|
|
126
|
-
const regex = new RegExp(`^${linePrefix}(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)`, "i");
|
|
127
140
|
for (const line of lines) {
|
|
128
|
-
const
|
|
129
|
-
if (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
dates.push(...dateValues.map((dateValue) => parseIcsDateTime(dateValue, timezone, valueType)));
|
|
134
|
-
}
|
|
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)));
|
|
135
146
|
}
|
|
136
147
|
return dates;
|
|
137
148
|
}
|
|
138
|
-
function
|
|
139
|
-
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));
|
|
140
179
|
if (sort) return arr.sort((a, b) => a - b);
|
|
141
180
|
return arr;
|
|
142
181
|
}
|
|
@@ -144,11 +183,11 @@ function parseNumberArray(val, sort = false) {
|
|
|
144
183
|
* Parse BYMONTH values, supporting RFC 7529 leap-month tokens with an "L" suffix (e.g., "5L").
|
|
145
184
|
* Returns a heterogeneous array keeping original tokens for serialization.
|
|
146
185
|
*/
|
|
147
|
-
function parseByMonthArray(val) {
|
|
186
|
+
function parseByMonthArray(val, strict = false) {
|
|
148
187
|
return val.split(",").map((tok) => {
|
|
149
188
|
const t = tok.trim();
|
|
150
189
|
if (/^\d+L$/i.test(t)) return t.toUpperCase();
|
|
151
|
-
const n =
|
|
190
|
+
const n = parseIntegerToken(t, "BYMONTH", strict);
|
|
152
191
|
return Number.isFinite(n) ? n : t;
|
|
153
192
|
});
|
|
154
193
|
}
|
|
@@ -201,9 +240,9 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
201
240
|
const rrLine = lines.find((line) => line.match(/^RRULE:/i));
|
|
202
241
|
const exLines = lines.filter((line) => line.match(/^EXDATE/i));
|
|
203
242
|
const rLines = lines.filter((line) => line.match(/^RDATE/i));
|
|
204
|
-
const
|
|
205
|
-
if (!
|
|
206
|
-
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;
|
|
207
246
|
dtstartValueType = (valueType || (dtValue?.includes("T") ? "DATE-TIME" : "DATE")).toUpperCase() === "DATE" ? "DATE" : "DATE-TIME";
|
|
208
247
|
dtstartHasTzid = Boolean(dtTzid);
|
|
209
248
|
dtstartIsUtc = Boolean(dtValue?.endsWith("Z"));
|
|
@@ -213,7 +252,7 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
213
252
|
exDate = parseDateLines(exLines, "EXDATE", tzid ?? "UTC");
|
|
214
253
|
rDate = parseDateLines(rLines, "RDATE", tzid ?? "UTC");
|
|
215
254
|
} else {
|
|
216
|
-
parsedDtstart = dtstart;
|
|
255
|
+
parsedDtstart = dtstart ? normalizeZonedDateTime(dtstart, "dtstart") : void 0;
|
|
217
256
|
rruleLine = unfoldedInput;
|
|
218
257
|
if (parsedDtstart) {
|
|
219
258
|
tzid = parsedDtstart.timeZoneId;
|
|
@@ -258,10 +297,10 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
258
297
|
opts.freq = val.toUpperCase();
|
|
259
298
|
break;
|
|
260
299
|
case "INTERVAL":
|
|
261
|
-
opts.interval =
|
|
300
|
+
opts.interval = parseIntegerToken(val, "INTERVAL", strict);
|
|
262
301
|
break;
|
|
263
302
|
case "COUNT":
|
|
264
|
-
opts.count =
|
|
303
|
+
opts.count = parseIntegerToken(val, "COUNT", strict);
|
|
265
304
|
break;
|
|
266
305
|
case "UNTIL": {
|
|
267
306
|
const untilHasTime = val.includes("T");
|
|
@@ -288,31 +327,31 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
288
327
|
break;
|
|
289
328
|
}
|
|
290
329
|
case "BYHOUR":
|
|
291
|
-
opts.byHour = parseNumberArray(val, true);
|
|
330
|
+
opts.byHour = parseNumberArray(val, true, strict, "BYHOUR");
|
|
292
331
|
break;
|
|
293
332
|
case "BYMINUTE":
|
|
294
|
-
opts.byMinute = parseNumberArray(val, true);
|
|
333
|
+
opts.byMinute = parseNumberArray(val, true, strict, "BYMINUTE");
|
|
295
334
|
break;
|
|
296
335
|
case "BYSECOND":
|
|
297
|
-
opts.bySecond = parseNumberArray(val, true);
|
|
336
|
+
opts.bySecond = parseNumberArray(val, true, strict, "BYSECOND");
|
|
298
337
|
break;
|
|
299
338
|
case "BYDAY":
|
|
300
339
|
opts.byDay = val.split(",").map((token) => token.toUpperCase());
|
|
301
340
|
break;
|
|
302
341
|
case "BYMONTH":
|
|
303
|
-
opts.byMonth = parseByMonthArray(val);
|
|
342
|
+
opts.byMonth = parseByMonthArray(val, strict);
|
|
304
343
|
break;
|
|
305
344
|
case "BYMONTHDAY":
|
|
306
|
-
opts.byMonthDay = parseNumberArray(val);
|
|
345
|
+
opts.byMonthDay = parseNumberArray(val, false, strict, "BYMONTHDAY");
|
|
307
346
|
break;
|
|
308
347
|
case "BYYEARDAY":
|
|
309
|
-
opts.byYearDay = parseNumberArray(val);
|
|
348
|
+
opts.byYearDay = parseNumberArray(val, false, strict, "BYYEARDAY");
|
|
310
349
|
break;
|
|
311
350
|
case "BYWEEKNO":
|
|
312
|
-
opts.byWeekNo = parseNumberArray(val);
|
|
351
|
+
opts.byWeekNo = parseNumberArray(val, false, strict, "BYWEEKNO");
|
|
313
352
|
break;
|
|
314
353
|
case "BYSETPOS":
|
|
315
|
-
opts.bySetPos = parseNumberArray(val);
|
|
354
|
+
opts.bySetPos = parseNumberArray(val, false, strict, "BYSETPOS");
|
|
316
355
|
break;
|
|
317
356
|
case "WKST":
|
|
318
357
|
opts.wkst = val?.toUpperCase();
|
|
@@ -332,38 +371,53 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
332
371
|
* This prevents type mismatches when mixing native and polyfill Temporal objects.
|
|
333
372
|
*/
|
|
334
373
|
static normalizeToPolyfill(zdt) {
|
|
335
|
-
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;
|
|
336
384
|
}
|
|
337
385
|
constructor(params) {
|
|
338
386
|
let manual;
|
|
339
387
|
if (isIcsOpts(params)) {
|
|
340
388
|
const parsed = parseRRuleString(params.rruleString, params.tzid, params.dtstart, params.strict ?? false);
|
|
341
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);
|
|
342
391
|
this.tzid = parsed.tzid ?? params.tzid ?? "UTC";
|
|
343
|
-
this.originalDtstart =
|
|
392
|
+
this.originalDtstart = dtstart;
|
|
344
393
|
manual = {
|
|
345
394
|
...parsed,
|
|
395
|
+
dtstart,
|
|
346
396
|
rDate: mergeDateLists(parsed.rDate, params.rDate),
|
|
347
397
|
exDate: mergeDateLists(parsed.exDate, params.exDate),
|
|
348
398
|
count: params.count ?? parsed.count,
|
|
349
|
-
until: params.until
|
|
399
|
+
until: params.until ? RRuleTemporal.normalizeToPolyfill(params.until) : parsed.until,
|
|
350
400
|
strict: params.strict,
|
|
351
401
|
maxIterations: params.maxIterations,
|
|
352
402
|
includeDtstart: params.includeDtstart,
|
|
353
403
|
tzid: this.tzid
|
|
354
404
|
};
|
|
355
405
|
} else {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
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;
|
|
359
415
|
this.tzid = manual.tzid;
|
|
360
|
-
this.originalDtstart =
|
|
416
|
+
this.originalDtstart = dtstart;
|
|
361
417
|
}
|
|
362
418
|
if (!manual.freq) throw new Error("RRULE must include FREQ");
|
|
363
419
|
manual.interval = manual.interval ?? 1;
|
|
364
420
|
if (manual.interval <= 0) throw new Error("Cannot create RRule: interval must be greater than 0");
|
|
365
|
-
if (manual.until && !(manual.until instanceof _js_temporal_polyfill.Temporal.ZonedDateTime)) throw new Error("Manual until must be a ZonedDateTime");
|
|
366
|
-
if (manual.until) manual.until = RRuleTemporal.normalizeToPolyfill(manual.until);
|
|
367
421
|
this.opts = this.sanitizeOpts(manual);
|
|
368
422
|
this.maxIterations = manual.maxIterations ?? 1e4;
|
|
369
423
|
this.includeDtstart = manual.includeDtstart ?? false;
|
|
@@ -845,7 +899,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
845
899
|
});
|
|
846
900
|
}
|
|
847
901
|
options() {
|
|
848
|
-
return this.
|
|
902
|
+
return this.cloneOptions();
|
|
849
903
|
}
|
|
850
904
|
cloneOptions() {
|
|
851
905
|
const { byHour, byMinute, bySecond, byDay, byMonth, byMonthDay, byYearDay, byWeekNo, bySetPos, rDate, exDate, ...rest } = this.opts;
|
|
@@ -1583,6 +1637,9 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1583
1637
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
1584
1638
|
*/
|
|
1585
1639
|
all(iterator) {
|
|
1640
|
+
return RRuleTemporal.toPublicDates(this.allInternal(RRuleTemporal.toInternalIterator(iterator)));
|
|
1641
|
+
}
|
|
1642
|
+
allInternal(iterator) {
|
|
1586
1643
|
if (this.opts.rscale && [
|
|
1587
1644
|
"CHINESE",
|
|
1588
1645
|
"HEBREW",
|
|
@@ -1766,12 +1823,12 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1766
1823
|
* Returns all occurrences of the rule within a specified time window.
|
|
1767
1824
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
1768
1825
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
1769
|
-
* @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.
|
|
1770
1827
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
1771
1828
|
*/
|
|
1772
1829
|
between(after, before, inc = false) {
|
|
1773
|
-
const startInst = after instanceof Date ? _js_temporal_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
1774
|
-
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();
|
|
1775
1832
|
const startZdt = _js_temporal_polyfill.Temporal.Instant.from(startInst).toZonedDateTimeISO(this.tzid);
|
|
1776
1833
|
const beforeZdt = _js_temporal_polyfill.Temporal.Instant.from(endInst).toZonedDateTimeISO(this.tzid);
|
|
1777
1834
|
const tempOpts = { ...this.opts };
|
|
@@ -1840,12 +1897,13 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1840
1897
|
if (_js_temporal_polyfill.Temporal.ZonedDateTime.compare(candidate, dtstartForCompare) < 0) candidate = dtstartForCompare;
|
|
1841
1898
|
tempOpts.dtstart = candidate;
|
|
1842
1899
|
}
|
|
1843
|
-
|
|
1900
|
+
const allDates = new RRuleTemporal(tempOpts).allInternal();
|
|
1901
|
+
return RRuleTemporal.toPublicDates(allDates.filter((date) => {
|
|
1844
1902
|
const inst = date.toInstant();
|
|
1845
1903
|
const afterStart = inc ? _js_temporal_polyfill.Temporal.Instant.compare(inst, startInst) >= 0 : _js_temporal_polyfill.Temporal.Instant.compare(inst, startInst) > 0;
|
|
1846
1904
|
const beforeEnd = inc ? _js_temporal_polyfill.Temporal.Instant.compare(inst, endInst) <= 0 : _js_temporal_polyfill.Temporal.Instant.compare(inst, endInst) < 0;
|
|
1847
1905
|
return afterStart && beforeEnd;
|
|
1848
|
-
});
|
|
1906
|
+
}));
|
|
1849
1907
|
}
|
|
1850
1908
|
/**
|
|
1851
1909
|
* Convenience helper: true if the exact instant is an occurrence of the rule.
|
|
@@ -1859,7 +1917,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1859
1917
|
* in the rule's time zone. This ignores time-of-day granularity.
|
|
1860
1918
|
*/
|
|
1861
1919
|
occursOn(date) {
|
|
1862
|
-
const startOfDay = date.toZonedDateTime({
|
|
1920
|
+
const startOfDay = _js_temporal_polyfill.Temporal.PlainDate.from(date.toString()).toZonedDateTime({
|
|
1863
1921
|
timeZone: this.tzid,
|
|
1864
1922
|
plainTime: _js_temporal_polyfill.Temporal.PlainTime.from("00:00")
|
|
1865
1923
|
});
|
|
@@ -1873,9 +1931,9 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1873
1931
|
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
1874
1932
|
*/
|
|
1875
1933
|
next(after = /* @__PURE__ */ new Date(), inc = false) {
|
|
1876
|
-
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();
|
|
1877
1935
|
let result = null;
|
|
1878
|
-
this.
|
|
1936
|
+
this.allInternal((occ) => {
|
|
1879
1937
|
const inst = occ.toInstant();
|
|
1880
1938
|
if (inc ? _js_temporal_polyfill.Temporal.Instant.compare(inst, afterInst) >= 0 : _js_temporal_polyfill.Temporal.Instant.compare(inst, afterInst) > 0) {
|
|
1881
1939
|
if (!result || _js_temporal_polyfill.Temporal.ZonedDateTime.compare(occ, result) < 0) result = occ;
|
|
@@ -1883,7 +1941,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1883
1941
|
}
|
|
1884
1942
|
return true;
|
|
1885
1943
|
});
|
|
1886
|
-
return result;
|
|
1944
|
+
return RRuleTemporal.toPublicDate(result);
|
|
1887
1945
|
}
|
|
1888
1946
|
/**
|
|
1889
1947
|
* Returns the previous occurrence of the rule before a specified date.
|
|
@@ -1892,15 +1950,15 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1892
1950
|
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
1893
1951
|
*/
|
|
1894
1952
|
previous(before = /* @__PURE__ */ new Date(), inc = false) {
|
|
1895
|
-
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();
|
|
1896
1954
|
let prev = null;
|
|
1897
|
-
this.
|
|
1955
|
+
this.allInternal((occ) => {
|
|
1898
1956
|
const inst = occ.toInstant();
|
|
1899
1957
|
if (inc ? _js_temporal_polyfill.Temporal.Instant.compare(inst, beforeInst) > 0 : _js_temporal_polyfill.Temporal.Instant.compare(inst, beforeInst) >= 0) return false;
|
|
1900
1958
|
prev = occ;
|
|
1901
1959
|
return true;
|
|
1902
1960
|
});
|
|
1903
|
-
return prev;
|
|
1961
|
+
return RRuleTemporal.toPublicDate(prev);
|
|
1904
1962
|
}
|
|
1905
1963
|
toString() {
|
|
1906
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,27 +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;
|
|
73
84
|
/** RDATE: additional dates to include */
|
|
74
|
-
rDate?:
|
|
85
|
+
rDate?: TemporalZonedDateTimeInput[];
|
|
75
86
|
/** EXDATE: exception dates to exclude */
|
|
76
|
-
exDate?:
|
|
87
|
+
exDate?: TemporalZonedDateTimeInput[];
|
|
77
88
|
}
|
|
78
|
-
type RRuleOptions =
|
|
79
|
-
type
|
|
80
|
-
|
|
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
|
+
};
|
|
81
96
|
declare class RRuleTemporal {
|
|
82
97
|
private readonly tzid;
|
|
83
98
|
private readonly originalDtstart;
|
|
@@ -97,6 +112,9 @@ declare class RRuleTemporal {
|
|
|
97
112
|
* This prevents type mismatches when mixing native and polyfill Temporal objects.
|
|
98
113
|
*/
|
|
99
114
|
private static normalizeToPolyfill;
|
|
115
|
+
private static toInternalIterator;
|
|
116
|
+
private static toPublicDate;
|
|
117
|
+
private static toPublicDates;
|
|
100
118
|
constructor(params: RRuleOptions);
|
|
101
119
|
private buildParsedByDayTokens;
|
|
102
120
|
private buildByDayIsoDays;
|
|
@@ -124,7 +142,7 @@ declare class RRuleTemporal {
|
|
|
124
142
|
private matchesByYearDay;
|
|
125
143
|
private getIsoWeekInfo;
|
|
126
144
|
private matchesByWeekNo;
|
|
127
|
-
options():
|
|
145
|
+
options(): RRuleResolvedOptions;
|
|
128
146
|
private cloneOptions;
|
|
129
147
|
private cloneUpdateOptions;
|
|
130
148
|
/**
|
|
@@ -135,7 +153,7 @@ declare class RRuleTemporal {
|
|
|
135
153
|
* const updated = rule.with({byMonthDay: [3]});
|
|
136
154
|
* ```
|
|
137
155
|
*/
|
|
138
|
-
with(updates: Partial<
|
|
156
|
+
with(updates: Partial<ManualOptions>): RRuleTemporal;
|
|
139
157
|
private addDtstartIfNeeded;
|
|
140
158
|
private canUseUtcLinearFastPath;
|
|
141
159
|
private canUseUtcWeeklyFastPath;
|
|
@@ -182,7 +200,8 @@ declare class RRuleTemporal {
|
|
|
182
200
|
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
183
201
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
184
202
|
*/
|
|
185
|
-
all(iterator?: RRuleTemporalIterator):
|
|
203
|
+
all(iterator?: RRuleTemporalIterator): TemporalZonedDateTime[];
|
|
204
|
+
private allInternal;
|
|
186
205
|
/**
|
|
187
206
|
* RFC 7529: RSCALE present, simple monthly iteration with SKIP behavior.
|
|
188
207
|
* Handles month-to-month stepping from DTSTART's year/month aiming for DTSTART's day-of-month.
|
|
@@ -225,10 +244,10 @@ declare class RRuleTemporal {
|
|
|
225
244
|
* Returns all occurrences of the rule within a specified time window.
|
|
226
245
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
227
246
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
228
|
-
* @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.
|
|
229
248
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
230
249
|
*/
|
|
231
|
-
between(after: DateFilter, before: DateFilter, inc?: boolean):
|
|
250
|
+
between(after: DateFilter, before: DateFilter, inc?: boolean): TemporalZonedDateTime[];
|
|
232
251
|
/**
|
|
233
252
|
* Convenience helper: true if the exact instant is an occurrence of the rule.
|
|
234
253
|
* This checks full date-time equality (including time and time zone).
|
|
@@ -238,21 +257,21 @@ declare class RRuleTemporal {
|
|
|
238
257
|
* Convenience helper: true if any occurrence falls on the given calendar day
|
|
239
258
|
* in the rule's time zone. This ignores time-of-day granularity.
|
|
240
259
|
*/
|
|
241
|
-
occursOn(date:
|
|
260
|
+
occursOn(date: TemporalPlainDateInput): boolean;
|
|
242
261
|
/**
|
|
243
262
|
* Returns the next occurrence of the rule after a specified date.
|
|
244
263
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
245
264
|
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
246
265
|
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
247
266
|
*/
|
|
248
|
-
next(after?: DateFilter, inc?: boolean):
|
|
267
|
+
next(after?: DateFilter, inc?: boolean): TemporalZonedDateTime | null;
|
|
249
268
|
/**
|
|
250
269
|
* Returns the previous occurrence of the rule before a specified date.
|
|
251
270
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
252
271
|
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
253
272
|
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
254
273
|
*/
|
|
255
|
-
previous(before?: DateFilter, inc?: boolean):
|
|
274
|
+
previous(before?: DateFilter, inc?: boolean): TemporalZonedDateTime | null;
|
|
256
275
|
toString(): string;
|
|
257
276
|
private formatIcsDateTime;
|
|
258
277
|
private joinDates;
|
|
@@ -305,4 +324,4 @@ declare class RRuleTemporal {
|
|
|
305
324
|
private _allRscaleNonGregorian;
|
|
306
325
|
}
|
|
307
326
|
//#endregion
|
|
308
|
-
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,27 +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;
|
|
73
84
|
/** RDATE: additional dates to include */
|
|
74
|
-
rDate?:
|
|
85
|
+
rDate?: TemporalZonedDateTimeInput[];
|
|
75
86
|
/** EXDATE: exception dates to exclude */
|
|
76
|
-
exDate?:
|
|
87
|
+
exDate?: TemporalZonedDateTimeInput[];
|
|
77
88
|
}
|
|
78
|
-
type RRuleOptions =
|
|
79
|
-
type
|
|
80
|
-
|
|
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
|
+
};
|
|
81
96
|
declare class RRuleTemporal {
|
|
82
97
|
private readonly tzid;
|
|
83
98
|
private readonly originalDtstart;
|
|
@@ -97,6 +112,9 @@ declare class RRuleTemporal {
|
|
|
97
112
|
* This prevents type mismatches when mixing native and polyfill Temporal objects.
|
|
98
113
|
*/
|
|
99
114
|
private static normalizeToPolyfill;
|
|
115
|
+
private static toInternalIterator;
|
|
116
|
+
private static toPublicDate;
|
|
117
|
+
private static toPublicDates;
|
|
100
118
|
constructor(params: RRuleOptions);
|
|
101
119
|
private buildParsedByDayTokens;
|
|
102
120
|
private buildByDayIsoDays;
|
|
@@ -124,7 +142,7 @@ declare class RRuleTemporal {
|
|
|
124
142
|
private matchesByYearDay;
|
|
125
143
|
private getIsoWeekInfo;
|
|
126
144
|
private matchesByWeekNo;
|
|
127
|
-
options():
|
|
145
|
+
options(): RRuleResolvedOptions;
|
|
128
146
|
private cloneOptions;
|
|
129
147
|
private cloneUpdateOptions;
|
|
130
148
|
/**
|
|
@@ -135,7 +153,7 @@ declare class RRuleTemporal {
|
|
|
135
153
|
* const updated = rule.with({byMonthDay: [3]});
|
|
136
154
|
* ```
|
|
137
155
|
*/
|
|
138
|
-
with(updates: Partial<
|
|
156
|
+
with(updates: Partial<ManualOptions>): RRuleTemporal;
|
|
139
157
|
private addDtstartIfNeeded;
|
|
140
158
|
private canUseUtcLinearFastPath;
|
|
141
159
|
private canUseUtcWeeklyFastPath;
|
|
@@ -182,7 +200,8 @@ declare class RRuleTemporal {
|
|
|
182
200
|
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
183
201
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
184
202
|
*/
|
|
185
|
-
all(iterator?: RRuleTemporalIterator):
|
|
203
|
+
all(iterator?: RRuleTemporalIterator): TemporalZonedDateTime[];
|
|
204
|
+
private allInternal;
|
|
186
205
|
/**
|
|
187
206
|
* RFC 7529: RSCALE present, simple monthly iteration with SKIP behavior.
|
|
188
207
|
* Handles month-to-month stepping from DTSTART's year/month aiming for DTSTART's day-of-month.
|
|
@@ -225,10 +244,10 @@ declare class RRuleTemporal {
|
|
|
225
244
|
* Returns all occurrences of the rule within a specified time window.
|
|
226
245
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
227
246
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
228
|
-
* @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.
|
|
229
248
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
230
249
|
*/
|
|
231
|
-
between(after: DateFilter, before: DateFilter, inc?: boolean):
|
|
250
|
+
between(after: DateFilter, before: DateFilter, inc?: boolean): TemporalZonedDateTime[];
|
|
232
251
|
/**
|
|
233
252
|
* Convenience helper: true if the exact instant is an occurrence of the rule.
|
|
234
253
|
* This checks full date-time equality (including time and time zone).
|
|
@@ -238,21 +257,21 @@ declare class RRuleTemporal {
|
|
|
238
257
|
* Convenience helper: true if any occurrence falls on the given calendar day
|
|
239
258
|
* in the rule's time zone. This ignores time-of-day granularity.
|
|
240
259
|
*/
|
|
241
|
-
occursOn(date:
|
|
260
|
+
occursOn(date: TemporalPlainDateInput): boolean;
|
|
242
261
|
/**
|
|
243
262
|
* Returns the next occurrence of the rule after a specified date.
|
|
244
263
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
245
264
|
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
246
265
|
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
247
266
|
*/
|
|
248
|
-
next(after?: DateFilter, inc?: boolean):
|
|
267
|
+
next(after?: DateFilter, inc?: boolean): TemporalZonedDateTime | null;
|
|
249
268
|
/**
|
|
250
269
|
* Returns the previous occurrence of the rule before a specified date.
|
|
251
270
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
252
271
|
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
253
272
|
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
254
273
|
*/
|
|
255
|
-
previous(before?: DateFilter, inc?: boolean):
|
|
274
|
+
previous(before?: DateFilter, inc?: boolean): TemporalZonedDateTime | null;
|
|
256
275
|
toString(): string;
|
|
257
276
|
private formatIcsDateTime;
|
|
258
277
|
private joinDates;
|
|
@@ -305,4 +324,4 @@ declare class RRuleTemporal {
|
|
|
305
324
|
private _allRscaleNonGregorian;
|
|
306
325
|
}
|
|
307
326
|
//#endregion
|
|
308
|
-
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,11 +87,25 @@ 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
|
}
|
|
93
107
|
function mergeDateLists(parsedDates, suppliedDates) {
|
|
94
|
-
const merged = [...parsedDates ?? [], ...suppliedDates ?? []];
|
|
108
|
+
const merged = [...parsedDates ?? [], ...normalizeZonedDateTimeList(suppliedDates, "Manual date") ?? []];
|
|
95
109
|
return merged.length > 0 ? merged : void 0;
|
|
96
110
|
}
|
|
97
111
|
/**
|
|
@@ -122,20 +136,45 @@ function parseIcsDateTime(dateStr, tzid, valueType) {
|
|
|
122
136
|
*/
|
|
123
137
|
function parseDateLines(lines, linePrefix, defaultTzid) {
|
|
124
138
|
const dates = [];
|
|
125
|
-
const regex = new RegExp(`^${linePrefix}(?:;VALUE=([^;]+))?(?:;TZID=([^:]+))?:(.+)`, "i");
|
|
126
139
|
for (const line of lines) {
|
|
127
|
-
const
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
dates.push(...dateValues.map((dateValue) => parseIcsDateTime(dateValue, timezone, valueType)));
|
|
133
|
-
}
|
|
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)));
|
|
134
145
|
}
|
|
135
146
|
return dates;
|
|
136
147
|
}
|
|
137
|
-
function
|
|
138
|
-
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));
|
|
139
178
|
if (sort) return arr.sort((a, b) => a - b);
|
|
140
179
|
return arr;
|
|
141
180
|
}
|
|
@@ -143,11 +182,11 @@ function parseNumberArray(val, sort = false) {
|
|
|
143
182
|
* Parse BYMONTH values, supporting RFC 7529 leap-month tokens with an "L" suffix (e.g., "5L").
|
|
144
183
|
* Returns a heterogeneous array keeping original tokens for serialization.
|
|
145
184
|
*/
|
|
146
|
-
function parseByMonthArray(val) {
|
|
185
|
+
function parseByMonthArray(val, strict = false) {
|
|
147
186
|
return val.split(",").map((tok) => {
|
|
148
187
|
const t = tok.trim();
|
|
149
188
|
if (/^\d+L$/i.test(t)) return t.toUpperCase();
|
|
150
|
-
const n =
|
|
189
|
+
const n = parseIntegerToken(t, "BYMONTH", strict);
|
|
151
190
|
return Number.isFinite(n) ? n : t;
|
|
152
191
|
});
|
|
153
192
|
}
|
|
@@ -200,9 +239,9 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
200
239
|
const rrLine = lines.find((line) => line.match(/^RRULE:/i));
|
|
201
240
|
const exLines = lines.filter((line) => line.match(/^EXDATE/i));
|
|
202
241
|
const rLines = lines.filter((line) => line.match(/^RDATE/i));
|
|
203
|
-
const
|
|
204
|
-
if (!
|
|
205
|
-
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;
|
|
206
245
|
dtstartValueType = (valueType || (dtValue?.includes("T") ? "DATE-TIME" : "DATE")).toUpperCase() === "DATE" ? "DATE" : "DATE-TIME";
|
|
207
246
|
dtstartHasTzid = Boolean(dtTzid);
|
|
208
247
|
dtstartIsUtc = Boolean(dtValue?.endsWith("Z"));
|
|
@@ -212,7 +251,7 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
212
251
|
exDate = parseDateLines(exLines, "EXDATE", tzid ?? "UTC");
|
|
213
252
|
rDate = parseDateLines(rLines, "RDATE", tzid ?? "UTC");
|
|
214
253
|
} else {
|
|
215
|
-
parsedDtstart = dtstart;
|
|
254
|
+
parsedDtstart = dtstart ? normalizeZonedDateTime(dtstart, "dtstart") : void 0;
|
|
216
255
|
rruleLine = unfoldedInput;
|
|
217
256
|
if (parsedDtstart) {
|
|
218
257
|
tzid = parsedDtstart.timeZoneId;
|
|
@@ -257,10 +296,10 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
257
296
|
opts.freq = val.toUpperCase();
|
|
258
297
|
break;
|
|
259
298
|
case "INTERVAL":
|
|
260
|
-
opts.interval =
|
|
299
|
+
opts.interval = parseIntegerToken(val, "INTERVAL", strict);
|
|
261
300
|
break;
|
|
262
301
|
case "COUNT":
|
|
263
|
-
opts.count =
|
|
302
|
+
opts.count = parseIntegerToken(val, "COUNT", strict);
|
|
264
303
|
break;
|
|
265
304
|
case "UNTIL": {
|
|
266
305
|
const untilHasTime = val.includes("T");
|
|
@@ -287,31 +326,31 @@ function parseRRuleString(input, targetTimezone, dtstart, strict = false) {
|
|
|
287
326
|
break;
|
|
288
327
|
}
|
|
289
328
|
case "BYHOUR":
|
|
290
|
-
opts.byHour = parseNumberArray(val, true);
|
|
329
|
+
opts.byHour = parseNumberArray(val, true, strict, "BYHOUR");
|
|
291
330
|
break;
|
|
292
331
|
case "BYMINUTE":
|
|
293
|
-
opts.byMinute = parseNumberArray(val, true);
|
|
332
|
+
opts.byMinute = parseNumberArray(val, true, strict, "BYMINUTE");
|
|
294
333
|
break;
|
|
295
334
|
case "BYSECOND":
|
|
296
|
-
opts.bySecond = parseNumberArray(val, true);
|
|
335
|
+
opts.bySecond = parseNumberArray(val, true, strict, "BYSECOND");
|
|
297
336
|
break;
|
|
298
337
|
case "BYDAY":
|
|
299
338
|
opts.byDay = val.split(",").map((token) => token.toUpperCase());
|
|
300
339
|
break;
|
|
301
340
|
case "BYMONTH":
|
|
302
|
-
opts.byMonth = parseByMonthArray(val);
|
|
341
|
+
opts.byMonth = parseByMonthArray(val, strict);
|
|
303
342
|
break;
|
|
304
343
|
case "BYMONTHDAY":
|
|
305
|
-
opts.byMonthDay = parseNumberArray(val);
|
|
344
|
+
opts.byMonthDay = parseNumberArray(val, false, strict, "BYMONTHDAY");
|
|
306
345
|
break;
|
|
307
346
|
case "BYYEARDAY":
|
|
308
|
-
opts.byYearDay = parseNumberArray(val);
|
|
347
|
+
opts.byYearDay = parseNumberArray(val, false, strict, "BYYEARDAY");
|
|
309
348
|
break;
|
|
310
349
|
case "BYWEEKNO":
|
|
311
|
-
opts.byWeekNo = parseNumberArray(val);
|
|
350
|
+
opts.byWeekNo = parseNumberArray(val, false, strict, "BYWEEKNO");
|
|
312
351
|
break;
|
|
313
352
|
case "BYSETPOS":
|
|
314
|
-
opts.bySetPos = parseNumberArray(val);
|
|
353
|
+
opts.bySetPos = parseNumberArray(val, false, strict, "BYSETPOS");
|
|
315
354
|
break;
|
|
316
355
|
case "WKST":
|
|
317
356
|
opts.wkst = val?.toUpperCase();
|
|
@@ -331,38 +370,53 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
331
370
|
* This prevents type mismatches when mixing native and polyfill Temporal objects.
|
|
332
371
|
*/
|
|
333
372
|
static normalizeToPolyfill(zdt) {
|
|
334
|
-
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;
|
|
335
383
|
}
|
|
336
384
|
constructor(params) {
|
|
337
385
|
let manual;
|
|
338
386
|
if (isIcsOpts(params)) {
|
|
339
387
|
const parsed = parseRRuleString(params.rruleString, params.tzid, params.dtstart, params.strict ?? false);
|
|
340
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);
|
|
341
390
|
this.tzid = parsed.tzid ?? params.tzid ?? "UTC";
|
|
342
|
-
this.originalDtstart =
|
|
391
|
+
this.originalDtstart = dtstart;
|
|
343
392
|
manual = {
|
|
344
393
|
...parsed,
|
|
394
|
+
dtstart,
|
|
345
395
|
rDate: mergeDateLists(parsed.rDate, params.rDate),
|
|
346
396
|
exDate: mergeDateLists(parsed.exDate, params.exDate),
|
|
347
397
|
count: params.count ?? parsed.count,
|
|
348
|
-
until: params.until
|
|
398
|
+
until: params.until ? RRuleTemporal.normalizeToPolyfill(params.until) : parsed.until,
|
|
349
399
|
strict: params.strict,
|
|
350
400
|
maxIterations: params.maxIterations,
|
|
351
401
|
includeDtstart: params.includeDtstart,
|
|
352
402
|
tzid: this.tzid
|
|
353
403
|
};
|
|
354
404
|
} else {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
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;
|
|
358
414
|
this.tzid = manual.tzid;
|
|
359
|
-
this.originalDtstart =
|
|
415
|
+
this.originalDtstart = dtstart;
|
|
360
416
|
}
|
|
361
417
|
if (!manual.freq) throw new Error("RRULE must include FREQ");
|
|
362
418
|
manual.interval = manual.interval ?? 1;
|
|
363
419
|
if (manual.interval <= 0) throw new Error("Cannot create RRule: interval must be greater than 0");
|
|
364
|
-
if (manual.until && !(manual.until instanceof Temporal.ZonedDateTime)) throw new Error("Manual until must be a ZonedDateTime");
|
|
365
|
-
if (manual.until) manual.until = RRuleTemporal.normalizeToPolyfill(manual.until);
|
|
366
420
|
this.opts = this.sanitizeOpts(manual);
|
|
367
421
|
this.maxIterations = manual.maxIterations ?? 1e4;
|
|
368
422
|
this.includeDtstart = manual.includeDtstart ?? false;
|
|
@@ -844,7 +898,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
844
898
|
});
|
|
845
899
|
}
|
|
846
900
|
options() {
|
|
847
|
-
return this.
|
|
901
|
+
return this.cloneOptions();
|
|
848
902
|
}
|
|
849
903
|
cloneOptions() {
|
|
850
904
|
const { byHour, byMinute, bySecond, byDay, byMonth, byMonthDay, byYearDay, byWeekNo, bySetPos, rDate, exDate, ...rest } = this.opts;
|
|
@@ -1582,6 +1636,9 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1582
1636
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
1583
1637
|
*/
|
|
1584
1638
|
all(iterator) {
|
|
1639
|
+
return RRuleTemporal.toPublicDates(this.allInternal(RRuleTemporal.toInternalIterator(iterator)));
|
|
1640
|
+
}
|
|
1641
|
+
allInternal(iterator) {
|
|
1585
1642
|
if (this.opts.rscale && [
|
|
1586
1643
|
"CHINESE",
|
|
1587
1644
|
"HEBREW",
|
|
@@ -1765,12 +1822,12 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1765
1822
|
* Returns all occurrences of the rule within a specified time window.
|
|
1766
1823
|
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
1767
1824
|
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
1768
|
-
* @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.
|
|
1769
1826
|
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
1770
1827
|
*/
|
|
1771
1828
|
between(after, before, inc = false) {
|
|
1772
|
-
const startInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
1773
|
-
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();
|
|
1774
1831
|
const startZdt = Temporal.Instant.from(startInst).toZonedDateTimeISO(this.tzid);
|
|
1775
1832
|
const beforeZdt = Temporal.Instant.from(endInst).toZonedDateTimeISO(this.tzid);
|
|
1776
1833
|
const tempOpts = { ...this.opts };
|
|
@@ -1839,12 +1896,13 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1839
1896
|
if (Temporal.ZonedDateTime.compare(candidate, dtstartForCompare) < 0) candidate = dtstartForCompare;
|
|
1840
1897
|
tempOpts.dtstart = candidate;
|
|
1841
1898
|
}
|
|
1842
|
-
|
|
1899
|
+
const allDates = new RRuleTemporal(tempOpts).allInternal();
|
|
1900
|
+
return RRuleTemporal.toPublicDates(allDates.filter((date) => {
|
|
1843
1901
|
const inst = date.toInstant();
|
|
1844
1902
|
const afterStart = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
1845
1903
|
const beforeEnd = inc ? Temporal.Instant.compare(inst, endInst) <= 0 : Temporal.Instant.compare(inst, endInst) < 0;
|
|
1846
1904
|
return afterStart && beforeEnd;
|
|
1847
|
-
});
|
|
1905
|
+
}));
|
|
1848
1906
|
}
|
|
1849
1907
|
/**
|
|
1850
1908
|
* Convenience helper: true if the exact instant is an occurrence of the rule.
|
|
@@ -1858,7 +1916,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1858
1916
|
* in the rule's time zone. This ignores time-of-day granularity.
|
|
1859
1917
|
*/
|
|
1860
1918
|
occursOn(date) {
|
|
1861
|
-
const startOfDay = date.toZonedDateTime({
|
|
1919
|
+
const startOfDay = Temporal.PlainDate.from(date.toString()).toZonedDateTime({
|
|
1862
1920
|
timeZone: this.tzid,
|
|
1863
1921
|
plainTime: Temporal.PlainTime.from("00:00")
|
|
1864
1922
|
});
|
|
@@ -1872,9 +1930,9 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1872
1930
|
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
1873
1931
|
*/
|
|
1874
1932
|
next(after = /* @__PURE__ */ new Date(), inc = false) {
|
|
1875
|
-
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();
|
|
1876
1934
|
let result = null;
|
|
1877
|
-
this.
|
|
1935
|
+
this.allInternal((occ) => {
|
|
1878
1936
|
const inst = occ.toInstant();
|
|
1879
1937
|
if (inc ? Temporal.Instant.compare(inst, afterInst) >= 0 : Temporal.Instant.compare(inst, afterInst) > 0) {
|
|
1880
1938
|
if (!result || Temporal.ZonedDateTime.compare(occ, result) < 0) result = occ;
|
|
@@ -1882,7 +1940,7 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1882
1940
|
}
|
|
1883
1941
|
return true;
|
|
1884
1942
|
});
|
|
1885
|
-
return result;
|
|
1943
|
+
return RRuleTemporal.toPublicDate(result);
|
|
1886
1944
|
}
|
|
1887
1945
|
/**
|
|
1888
1946
|
* Returns the previous occurrence of the rule before a specified date.
|
|
@@ -1891,15 +1949,15 @@ var RRuleTemporal = class RRuleTemporal {
|
|
|
1891
1949
|
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
1892
1950
|
*/
|
|
1893
1951
|
previous(before = /* @__PURE__ */ new Date(), inc = false) {
|
|
1894
|
-
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();
|
|
1895
1953
|
let prev = null;
|
|
1896
|
-
this.
|
|
1954
|
+
this.allInternal((occ) => {
|
|
1897
1955
|
const inst = occ.toInstant();
|
|
1898
1956
|
if (inc ? Temporal.Instant.compare(inst, beforeInst) > 0 : Temporal.Instant.compare(inst, beforeInst) >= 0) return false;
|
|
1899
1957
|
prev = occ;
|
|
1900
1958
|
return true;
|
|
1901
1959
|
});
|
|
1902
|
-
return prev;
|
|
1960
|
+
return RRuleTemporal.toPublicDate(prev);
|
|
1903
1961
|
}
|
|
1904
1962
|
toString() {
|
|
1905
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,6 +20,7 @@
|
|
|
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
25
|
"format": "prettier --write 'src/**/*.ts'",
|
|
25
26
|
"prepare": "npm run build"
|
|
@@ -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
|
}
|