rrule-temporal 0.2.6 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +424 -83
- package/dist/index.d.cts +34 -4
- package/dist/index.d.ts +34 -4
- package/dist/index.js +424 -83
- package/package.json +3 -1
package/dist/index.js
CHANGED
|
@@ -116,6 +116,21 @@ function parseRRuleString(input, fallbackDtstart) {
|
|
|
116
116
|
case "BYMONTHDAY":
|
|
117
117
|
opts.byMonthDay = val.split(",").map((n) => parseInt(n, 10));
|
|
118
118
|
break;
|
|
119
|
+
case "BYSECOND":
|
|
120
|
+
opts.bySecond = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
|
|
121
|
+
break;
|
|
122
|
+
case "BYYEARDAY":
|
|
123
|
+
opts.byYearDay = val.split(",").map((n) => parseInt(n, 10));
|
|
124
|
+
break;
|
|
125
|
+
case "BYWEEKNO":
|
|
126
|
+
opts.byWeekNo = val.split(",").map((n) => parseInt(n, 10));
|
|
127
|
+
break;
|
|
128
|
+
case "BYSETPOS":
|
|
129
|
+
opts.bySetPos = val.split(",").map((n) => parseInt(n, 10));
|
|
130
|
+
break;
|
|
131
|
+
case "WKST":
|
|
132
|
+
opts.wkst = val;
|
|
133
|
+
break;
|
|
119
134
|
}
|
|
120
135
|
}
|
|
121
136
|
return opts;
|
|
@@ -158,7 +173,53 @@ var RRuleTemporal = class {
|
|
|
158
173
|
}
|
|
159
174
|
if (!manual.freq) throw new Error("RRULE must include FREQ");
|
|
160
175
|
manual.interval = manual.interval ?? 1;
|
|
161
|
-
|
|
176
|
+
if (manual.interval <= 0) {
|
|
177
|
+
throw new Error("Cannot create RRule: interval must be greater than 0");
|
|
178
|
+
}
|
|
179
|
+
if (manual.until && !(manual.until instanceof Temporal.ZonedDateTime)) {
|
|
180
|
+
throw new Error("Manual until must be a ZonedDateTime");
|
|
181
|
+
}
|
|
182
|
+
this.opts = this.sanitizeOpts(manual);
|
|
183
|
+
}
|
|
184
|
+
sanitizeOpts(opts) {
|
|
185
|
+
const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
|
|
186
|
+
if (opts.byDay) {
|
|
187
|
+
opts.byDay = opts.byDay.filter((d) => validDay.test(d));
|
|
188
|
+
if (opts.byDay.length === 0) delete opts.byDay;
|
|
189
|
+
}
|
|
190
|
+
if (opts.byMonth) {
|
|
191
|
+
opts.byMonth = opts.byMonth.filter((n) => Number.isInteger(n) && n >= 1 && n <= 12);
|
|
192
|
+
if (opts.byMonth.length === 0) delete opts.byMonth;
|
|
193
|
+
}
|
|
194
|
+
if (opts.byMonthDay) {
|
|
195
|
+
opts.byMonthDay = opts.byMonthDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -31 && n <= 31);
|
|
196
|
+
if (opts.byMonthDay.length === 0) delete opts.byMonthDay;
|
|
197
|
+
}
|
|
198
|
+
if (opts.byYearDay) {
|
|
199
|
+
opts.byYearDay = opts.byYearDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -366 && n <= 366);
|
|
200
|
+
if (opts.byYearDay.length === 0) delete opts.byYearDay;
|
|
201
|
+
}
|
|
202
|
+
if (opts.byWeekNo) {
|
|
203
|
+
opts.byWeekNo = opts.byWeekNo.filter((n) => Number.isInteger(n) && n !== 0 && n >= -53 && n <= 53);
|
|
204
|
+
if (opts.byWeekNo.length === 0) delete opts.byWeekNo;
|
|
205
|
+
}
|
|
206
|
+
if (opts.byHour) {
|
|
207
|
+
opts.byHour = opts.byHour.filter((n) => Number.isInteger(n) && n >= 0 && n <= 23).sort((a, b) => a - b);
|
|
208
|
+
if (opts.byHour.length === 0) delete opts.byHour;
|
|
209
|
+
}
|
|
210
|
+
if (opts.byMinute) {
|
|
211
|
+
opts.byMinute = opts.byMinute.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
|
|
212
|
+
if (opts.byMinute.length === 0) delete opts.byMinute;
|
|
213
|
+
}
|
|
214
|
+
if (opts.bySecond) {
|
|
215
|
+
opts.bySecond = opts.bySecond.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
|
|
216
|
+
if (opts.bySecond.length === 0) delete opts.bySecond;
|
|
217
|
+
}
|
|
218
|
+
if (opts.bySetPos) {
|
|
219
|
+
opts.bySetPos = opts.bySetPos.filter((n) => Number.isInteger(n) && n !== 0);
|
|
220
|
+
if (opts.bySetPos.length === 0) delete opts.bySetPos;
|
|
221
|
+
}
|
|
222
|
+
return opts;
|
|
162
223
|
}
|
|
163
224
|
rawAdvance(zdt) {
|
|
164
225
|
const { freq, interval } = this.opts;
|
|
@@ -181,39 +242,84 @@ var RRuleTemporal = class {
|
|
|
181
242
|
throw new Error(`Unsupported FREQ: ${freq}`);
|
|
182
243
|
}
|
|
183
244
|
}
|
|
184
|
-
/** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE
|
|
185
|
-
* keeping chronological order.
|
|
186
|
-
* original date is returned unchanged.
|
|
245
|
+
/** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE × BYSECOND
|
|
246
|
+
* combinations, keeping chronological order. If the options are not
|
|
247
|
+
* present the original date is returned unchanged.
|
|
187
248
|
*/
|
|
188
|
-
|
|
249
|
+
expandByTime(base) {
|
|
189
250
|
const hours = this.opts.byHour ?? [base.hour];
|
|
190
251
|
const minutes = this.opts.byMinute ?? [base.minute];
|
|
252
|
+
const seconds = this.opts.bySecond ?? [base.second];
|
|
191
253
|
const out = [];
|
|
192
254
|
for (const h of hours) {
|
|
193
255
|
for (const m of minutes) {
|
|
194
|
-
|
|
256
|
+
for (const s of seconds) {
|
|
257
|
+
out.push(base.with({ hour: h, minute: m, second: s }));
|
|
258
|
+
}
|
|
195
259
|
}
|
|
196
260
|
}
|
|
197
261
|
return out.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
198
262
|
}
|
|
199
263
|
nextCandidateSameDate(zdt) {
|
|
200
|
-
const { byHour, byMinute } = this.opts;
|
|
264
|
+
const { freq, interval = 1, byHour, byMinute, bySecond } = this.opts;
|
|
265
|
+
if (freq === "HOURLY" && byHour && byHour.length === 1) {
|
|
266
|
+
return this.applyTimeOverride(zdt.add({ days: interval }));
|
|
267
|
+
}
|
|
268
|
+
if (freq === "MINUTELY" && byMinute && byMinute.length === 1) {
|
|
269
|
+
return this.applyTimeOverride(zdt.add({ hours: interval }));
|
|
270
|
+
}
|
|
271
|
+
if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute && interval > 1) {
|
|
272
|
+
const next = zdt.add({ minutes: interval });
|
|
273
|
+
if (byHour.includes(next.hour)) {
|
|
274
|
+
return next;
|
|
275
|
+
}
|
|
276
|
+
const nextHour = byHour.find((h) => h > zdt.hour) || byHour[0];
|
|
277
|
+
if (nextHour && nextHour > zdt.hour) {
|
|
278
|
+
return zdt.with({ hour: nextHour, minute: 0 });
|
|
279
|
+
}
|
|
280
|
+
return this.applyTimeOverride(zdt.add({ days: 1 }));
|
|
281
|
+
}
|
|
282
|
+
if (freq === "SECONDLY" && bySecond && bySecond.length === 1) {
|
|
283
|
+
return this.applyTimeOverride(zdt.add({ minutes: interval })).with({ second: bySecond[0] });
|
|
284
|
+
}
|
|
285
|
+
if (freq === "SECONDLY" && byMinute && byMinute.length === 1) {
|
|
286
|
+
const next = zdt.add({ seconds: interval });
|
|
287
|
+
if (next.minute === byMinute[0]) return next;
|
|
288
|
+
return this.applyTimeOverride(zdt.add({ hours: interval })).with({ second: 0 });
|
|
289
|
+
}
|
|
290
|
+
if (bySecond && bySecond.length > 1) {
|
|
291
|
+
const idx = bySecond.indexOf(zdt.second);
|
|
292
|
+
if (idx !== -1 && idx < bySecond.length - 1) {
|
|
293
|
+
return zdt.with({ second: bySecond[idx + 1] });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (byMinute && byMinute.length > 1) {
|
|
297
|
+
const idx = byMinute.indexOf(zdt.minute);
|
|
298
|
+
if (idx !== -1 && idx < byMinute.length - 1) {
|
|
299
|
+
return zdt.with({
|
|
300
|
+
minute: byMinute[idx + 1],
|
|
301
|
+
second: bySecond ? bySecond[0] : zdt.second
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
201
305
|
if (byHour && byHour.length > 1) {
|
|
202
306
|
const idx = byHour.indexOf(zdt.hour);
|
|
203
307
|
if (idx !== -1 && idx < byHour.length - 1) {
|
|
204
308
|
return zdt.with({
|
|
205
309
|
hour: byHour[idx + 1],
|
|
206
|
-
minute: byMinute ? byMinute[0] : zdt.minute
|
|
310
|
+
minute: byMinute ? byMinute[0] : zdt.minute,
|
|
311
|
+
second: bySecond ? bySecond[0] : zdt.second
|
|
207
312
|
});
|
|
208
313
|
}
|
|
209
314
|
}
|
|
210
315
|
return this.applyTimeOverride(this.rawAdvance(zdt));
|
|
211
316
|
}
|
|
212
317
|
applyTimeOverride(zdt) {
|
|
213
|
-
const { byHour, byMinute } = this.opts;
|
|
318
|
+
const { byHour, byMinute, bySecond } = this.opts;
|
|
214
319
|
let dt = zdt;
|
|
215
320
|
if (byHour) dt = dt.with({ hour: byHour[0] });
|
|
216
321
|
if (byMinute) dt = dt.with({ minute: byMinute[0] });
|
|
322
|
+
if (bySecond) dt = dt.with({ second: bySecond[0] });
|
|
217
323
|
return dt;
|
|
218
324
|
}
|
|
219
325
|
computeFirst() {
|
|
@@ -243,8 +349,8 @@ var RRuleTemporal = class {
|
|
|
243
349
|
zdt = zdt.add({ days: Math.min(...deltas) });
|
|
244
350
|
}
|
|
245
351
|
}
|
|
246
|
-
const { byHour, byMinute } = this.opts;
|
|
247
|
-
if (byHour || byMinute) {
|
|
352
|
+
const { byHour, byMinute, bySecond } = this.opts;
|
|
353
|
+
if (byHour || byMinute || bySecond) {
|
|
248
354
|
zdt = this.applyTimeOverride(zdt);
|
|
249
355
|
if (Temporal.Instant.compare(
|
|
250
356
|
zdt.toInstant(),
|
|
@@ -271,7 +377,7 @@ var RRuleTemporal = class {
|
|
|
271
377
|
const targetDow = dayMap[tok];
|
|
272
378
|
const delta = (targetDow - sample.dayOfWeek + 7) % 7;
|
|
273
379
|
const sameDate = sample.add({ days: delta });
|
|
274
|
-
return this.
|
|
380
|
+
return this.expandByTime(sameDate);
|
|
275
381
|
});
|
|
276
382
|
return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
277
383
|
}
|
|
@@ -290,7 +396,7 @@ var RRuleTemporal = class {
|
|
|
290
396
|
SU: 7
|
|
291
397
|
};
|
|
292
398
|
for (const token of byDay) {
|
|
293
|
-
const m = token.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
399
|
+
const m = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
294
400
|
if (!m) continue;
|
|
295
401
|
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
296
402
|
const weekday = m[2];
|
|
@@ -326,7 +432,28 @@ var RRuleTemporal = class {
|
|
|
326
432
|
);
|
|
327
433
|
}
|
|
328
434
|
matchesAll(zdt) {
|
|
329
|
-
return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt);
|
|
435
|
+
return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt) && this.matchesByYearDay(zdt) && this.matchesByWeekNo(zdt);
|
|
436
|
+
}
|
|
437
|
+
matchesByYearDay(zdt) {
|
|
438
|
+
const { byYearDay } = this.opts;
|
|
439
|
+
if (!byYearDay) return true;
|
|
440
|
+
const dayOfYear = zdt.dayOfYear;
|
|
441
|
+
const last = zdt.with({ month: 12, day: 31 }).dayOfYear;
|
|
442
|
+
return byYearDay.some((d) => d > 0 ? dayOfYear === d : dayOfYear === last + d + 1);
|
|
443
|
+
}
|
|
444
|
+
matchesByWeekNo(zdt) {
|
|
445
|
+
const { byWeekNo, wkst } = this.opts;
|
|
446
|
+
if (!byWeekNo) return true;
|
|
447
|
+
const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
|
|
448
|
+
const startDow = dayMap[wkst ?? "MO"];
|
|
449
|
+
const jan1 = zdt.with({ month: 1, day: 1 });
|
|
450
|
+
const delta = (jan1.dayOfWeek - startDow + 7) % 7;
|
|
451
|
+
const firstWeekStart = jan1.subtract({ days: delta });
|
|
452
|
+
const diffDays = zdt.toPlainDate().since(firstWeekStart.toPlainDate()).days;
|
|
453
|
+
const week = Math.floor(diffDays / 7) + 1;
|
|
454
|
+
const lastWeekDiff = zdt.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
|
|
455
|
+
const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
|
|
456
|
+
return byWeekNo.some((n) => n > 0 ? week === n : week === lastWeek + n + 1);
|
|
330
457
|
}
|
|
331
458
|
options() {
|
|
332
459
|
return this.opts;
|
|
@@ -356,18 +483,18 @@ var RRuleTemporal = class {
|
|
|
356
483
|
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
357
484
|
break outer_month;
|
|
358
485
|
}
|
|
359
|
-
if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
|
|
360
|
-
break outer_month;
|
|
361
|
-
}
|
|
362
486
|
if (iterator && !iterator(occ, matchCount2)) {
|
|
363
487
|
break outer_month;
|
|
364
488
|
}
|
|
365
489
|
dates.push(occ);
|
|
366
490
|
matchCount2++;
|
|
491
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
492
|
+
break outer_month;
|
|
493
|
+
}
|
|
367
494
|
}
|
|
368
495
|
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
369
496
|
}
|
|
370
|
-
return dates;
|
|
497
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
371
498
|
}
|
|
372
499
|
if (this.opts.freq === "WEEKLY") {
|
|
373
500
|
const start = this.originalDtstart;
|
|
@@ -386,61 +513,65 @@ var RRuleTemporal = class {
|
|
|
386
513
|
const delta = (dw - start.dayOfWeek + 7) % 7;
|
|
387
514
|
return start.add({ days: delta });
|
|
388
515
|
});
|
|
389
|
-
let
|
|
516
|
+
let firstOccurrence = firstWeekDates.reduce(
|
|
390
517
|
(a, b) => Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b
|
|
391
518
|
);
|
|
519
|
+
const wkstDay = dayMap[this.opts.wkst || "MO"] ?? 1;
|
|
520
|
+
const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
|
|
521
|
+
let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
|
|
392
522
|
let matchCount2 = 0;
|
|
393
523
|
outer_week: while (true) {
|
|
394
|
-
const baseDow = weekCursor.dayOfWeek;
|
|
395
524
|
const occs = dows.flatMap((dw) => {
|
|
396
|
-
const delta = dw -
|
|
525
|
+
const delta = (dw - wkstDay + 7) % 7;
|
|
397
526
|
const sameDate = weekCursor.add({ days: delta });
|
|
398
|
-
return this.
|
|
527
|
+
return this.expandByTime(sameDate);
|
|
399
528
|
}).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
400
529
|
for (const occ of occs) {
|
|
401
530
|
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
402
531
|
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
403
532
|
break outer_week;
|
|
404
533
|
}
|
|
405
|
-
if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
|
|
406
|
-
break outer_week;
|
|
407
|
-
}
|
|
408
534
|
if (iterator && !iterator(occ, matchCount2)) {
|
|
409
535
|
break outer_week;
|
|
410
536
|
}
|
|
411
537
|
dates.push(occ);
|
|
412
538
|
matchCount2++;
|
|
539
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
540
|
+
break outer_week;
|
|
541
|
+
}
|
|
413
542
|
}
|
|
414
543
|
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
415
544
|
}
|
|
416
|
-
return dates;
|
|
545
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
417
546
|
}
|
|
418
547
|
if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
|
|
419
548
|
const start = this.originalDtstart;
|
|
420
549
|
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
421
|
-
let
|
|
550
|
+
let yearOffset = 0;
|
|
551
|
+
let matchCount2 = 0;
|
|
422
552
|
while (true) {
|
|
423
|
-
const year = start.year +
|
|
424
|
-
const month
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
553
|
+
const year = start.year + yearOffset * this.opts.interval;
|
|
554
|
+
for (const month of months) {
|
|
555
|
+
let occ = start.with({ year, month });
|
|
556
|
+
occ = this.applyTimeOverride(occ);
|
|
557
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) {
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
561
|
+
return dates;
|
|
562
|
+
}
|
|
563
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
564
|
+
return dates;
|
|
565
|
+
}
|
|
566
|
+
dates.push(occ);
|
|
567
|
+
matchCount2++;
|
|
568
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
569
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
570
|
+
}
|
|
439
571
|
}
|
|
440
|
-
|
|
441
|
-
i++;
|
|
572
|
+
yearOffset++;
|
|
442
573
|
}
|
|
443
|
-
return dates;
|
|
574
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
444
575
|
}
|
|
445
576
|
if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
446
577
|
const start = this.originalDtstart;
|
|
@@ -453,18 +584,42 @@ var RRuleTemporal = class {
|
|
|
453
584
|
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
454
585
|
break outer_year;
|
|
455
586
|
}
|
|
456
|
-
if (
|
|
587
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
457
588
|
break outer_year;
|
|
458
589
|
}
|
|
459
|
-
|
|
590
|
+
dates.push(occ);
|
|
591
|
+
matchCount2++;
|
|
592
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
460
593
|
break outer_year;
|
|
461
594
|
}
|
|
595
|
+
}
|
|
596
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
597
|
+
}
|
|
598
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
599
|
+
}
|
|
600
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
|
|
601
|
+
const start = this.originalDtstart;
|
|
602
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
603
|
+
let matchCount2 = 0;
|
|
604
|
+
outer_year2: while (true) {
|
|
605
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
606
|
+
for (const occ of occs) {
|
|
607
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
608
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
609
|
+
break outer_year2;
|
|
610
|
+
}
|
|
611
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
612
|
+
break outer_year2;
|
|
613
|
+
}
|
|
462
614
|
dates.push(occ);
|
|
463
615
|
matchCount2++;
|
|
616
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
617
|
+
break outer_year2;
|
|
618
|
+
}
|
|
464
619
|
}
|
|
465
620
|
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
466
621
|
}
|
|
467
|
-
return dates;
|
|
622
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
468
623
|
}
|
|
469
624
|
let current = this.computeFirst();
|
|
470
625
|
let matchCount = 0;
|
|
@@ -473,18 +628,71 @@ var RRuleTemporal = class {
|
|
|
473
628
|
break;
|
|
474
629
|
}
|
|
475
630
|
if (this.matchesAll(current)) {
|
|
476
|
-
if (this.opts.count !== void 0 && matchCount >= this.opts.count) {
|
|
477
|
-
break;
|
|
478
|
-
}
|
|
479
631
|
if (iterator && !iterator(current, matchCount)) {
|
|
480
632
|
break;
|
|
481
633
|
}
|
|
482
634
|
dates.push(current);
|
|
483
635
|
matchCount++;
|
|
636
|
+
if (this.shouldBreakForCountLimit(matchCount)) {
|
|
637
|
+
break;
|
|
638
|
+
}
|
|
484
639
|
}
|
|
485
640
|
current = this.nextCandidateSameDate(current);
|
|
486
641
|
}
|
|
487
|
-
return dates;
|
|
642
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Converts rDate entries to ZonedDateTime and merges with existing dates.
|
|
646
|
+
* @param dates - Array of dates to merge with
|
|
647
|
+
* @returns Merged and deduplicated array of dates
|
|
648
|
+
*/
|
|
649
|
+
mergeAndDeduplicateRDates(dates) {
|
|
650
|
+
if (!this.opts.rDate) return dates;
|
|
651
|
+
const extras = this.opts.rDate.map(
|
|
652
|
+
(d) => d instanceof Temporal.ZonedDateTime ? d : Temporal.Instant.from(d.toISOString()).toZonedDateTimeISO(this.tzid)
|
|
653
|
+
);
|
|
654
|
+
dates.push(...extras);
|
|
655
|
+
dates.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
656
|
+
const dedup = [];
|
|
657
|
+
for (const d of dates) {
|
|
658
|
+
if (dedup.length === 0 || Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
|
|
659
|
+
dedup.push(d);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return dedup;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Applies count limit and merges rDates with the rule-generated dates.
|
|
666
|
+
* @param dates - Array of dates generated by the rule
|
|
667
|
+
* @param iterator - Optional iterator function
|
|
668
|
+
* @returns Final array of dates after merging and applying count limit
|
|
669
|
+
*/
|
|
670
|
+
applyCountLimitAndMergeRDates(dates, iterator) {
|
|
671
|
+
const merged = this.mergeAndDeduplicateRDates(dates);
|
|
672
|
+
if (this.opts.count !== void 0) {
|
|
673
|
+
let finalCount = 0;
|
|
674
|
+
const finalDates = [];
|
|
675
|
+
for (const d of merged) {
|
|
676
|
+
if (finalCount >= this.opts.count) break;
|
|
677
|
+
if (iterator && !iterator(d, finalCount)) break;
|
|
678
|
+
finalDates.push(d);
|
|
679
|
+
finalCount++;
|
|
680
|
+
}
|
|
681
|
+
return finalDates;
|
|
682
|
+
}
|
|
683
|
+
return merged;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Checks if the count limit should break the loop based on rDate presence.
|
|
687
|
+
* @param matchCount - Current number of matches
|
|
688
|
+
* @returns true if the loop should break
|
|
689
|
+
*/
|
|
690
|
+
shouldBreakForCountLimit(matchCount) {
|
|
691
|
+
if (this.opts.count === void 0) return false;
|
|
692
|
+
if (!this.opts.rDate) {
|
|
693
|
+
return matchCount >= this.opts.count;
|
|
694
|
+
}
|
|
695
|
+
return matchCount >= this.opts.count * 2;
|
|
488
696
|
}
|
|
489
697
|
/**
|
|
490
698
|
* Returns all occurrences of the rule within a specified time window.
|
|
@@ -506,33 +714,36 @@ var RRuleTemporal = class {
|
|
|
506
714
|
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
507
715
|
break outer;
|
|
508
716
|
}
|
|
509
|
-
|
|
717
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
718
|
+
if (startOk) {
|
|
510
719
|
results.push(occ);
|
|
511
720
|
}
|
|
512
721
|
}
|
|
513
722
|
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
514
723
|
}
|
|
515
|
-
return results;
|
|
724
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
516
725
|
}
|
|
517
726
|
if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
|
|
518
727
|
const start = this.originalDtstart;
|
|
519
728
|
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
520
|
-
let
|
|
729
|
+
let yearOffset = 0;
|
|
521
730
|
outer: while (true) {
|
|
522
|
-
const year = start.year +
|
|
523
|
-
const month
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
731
|
+
const year = start.year + yearOffset * this.opts.interval;
|
|
732
|
+
for (const month of months) {
|
|
733
|
+
let occ = start.with({ year, month });
|
|
734
|
+
occ = this.applyTimeOverride(occ);
|
|
735
|
+
const inst = occ.toInstant();
|
|
736
|
+
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
737
|
+
break outer;
|
|
738
|
+
}
|
|
739
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
740
|
+
if (startOk) {
|
|
741
|
+
results.push(occ);
|
|
742
|
+
}
|
|
532
743
|
}
|
|
533
|
-
|
|
744
|
+
yearOffset++;
|
|
534
745
|
}
|
|
535
|
-
return results;
|
|
746
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
536
747
|
}
|
|
537
748
|
if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
538
749
|
const start = this.originalDtstart;
|
|
@@ -544,13 +755,33 @@ var RRuleTemporal = class {
|
|
|
544
755
|
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
545
756
|
break outer_year;
|
|
546
757
|
}
|
|
547
|
-
|
|
758
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
759
|
+
if (startOk) {
|
|
548
760
|
results.push(occ);
|
|
549
761
|
}
|
|
550
762
|
}
|
|
551
763
|
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
552
764
|
}
|
|
553
|
-
return results;
|
|
765
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
766
|
+
}
|
|
767
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
|
|
768
|
+
const start = this.originalDtstart;
|
|
769
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
770
|
+
outer_year2: while (true) {
|
|
771
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
772
|
+
for (const occ of occs) {
|
|
773
|
+
const inst = occ.toInstant();
|
|
774
|
+
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
775
|
+
break outer_year2;
|
|
776
|
+
}
|
|
777
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
778
|
+
if (startOk) {
|
|
779
|
+
results.push(occ);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
783
|
+
}
|
|
784
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
554
785
|
}
|
|
555
786
|
if (this.opts.freq === "WEEKLY") {
|
|
556
787
|
const startInst2 = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
@@ -564,13 +795,14 @@ var RRuleTemporal = class {
|
|
|
564
795
|
if (inc ? Temporal.Instant.compare(inst, endInst2) > 0 : Temporal.Instant.compare(inst, endInst2) >= 0) {
|
|
565
796
|
break outer;
|
|
566
797
|
}
|
|
567
|
-
|
|
798
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst2) >= 0 : Temporal.Instant.compare(inst, startInst2) > 0;
|
|
799
|
+
if (startOk) {
|
|
568
800
|
results2.push(occ);
|
|
569
801
|
}
|
|
570
802
|
}
|
|
571
803
|
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
572
804
|
}
|
|
573
|
-
return results2;
|
|
805
|
+
return this.mergeRDates(results2, startInst2, endInst2, inc);
|
|
574
806
|
}
|
|
575
807
|
let current = this.computeFirst();
|
|
576
808
|
while (true) {
|
|
@@ -580,12 +812,13 @@ var RRuleTemporal = class {
|
|
|
580
812
|
} else {
|
|
581
813
|
if (Temporal.Instant.compare(inst, endInst) >= 0) break;
|
|
582
814
|
}
|
|
583
|
-
|
|
815
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
816
|
+
if (startOk && this.matchesAll(current)) {
|
|
584
817
|
results.push(current);
|
|
585
818
|
}
|
|
586
819
|
current = this.nextCandidateSameDate(current);
|
|
587
820
|
}
|
|
588
|
-
return results;
|
|
821
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
589
822
|
}
|
|
590
823
|
/**
|
|
591
824
|
* Returns the next occurrence of the rule after a specified date.
|
|
@@ -743,10 +976,10 @@ var RRuleTemporal = class {
|
|
|
743
976
|
}
|
|
744
977
|
if (!byDay && byMonthDayHits.length) {
|
|
745
978
|
const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
|
|
746
|
-
return dates.flatMap((z) => this.
|
|
979
|
+
return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
747
980
|
}
|
|
748
981
|
if (!byDay) {
|
|
749
|
-
return this.
|
|
982
|
+
return this.expandByTime(sample);
|
|
750
983
|
}
|
|
751
984
|
const dayMap = {
|
|
752
985
|
MO: 1,
|
|
@@ -758,7 +991,7 @@ var RRuleTemporal = class {
|
|
|
758
991
|
SU: 7
|
|
759
992
|
};
|
|
760
993
|
const tokens = byDay.map((tok) => {
|
|
761
|
-
const m = tok.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
994
|
+
const m = tok.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
762
995
|
if (!m) return null;
|
|
763
996
|
return { ord: m[1] ? parseInt(m[1], 10) : 0, wd: dayMap[m[2]] };
|
|
764
997
|
}).filter((x) => x !== null);
|
|
@@ -786,7 +1019,9 @@ var RRuleTemporal = class {
|
|
|
786
1019
|
finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
|
|
787
1020
|
}
|
|
788
1021
|
const hits = finalDays.map((d) => sample.with({ day: d }));
|
|
789
|
-
|
|
1022
|
+
let expanded = hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1023
|
+
expanded = this.applyBySetPos(expanded);
|
|
1024
|
+
return expanded;
|
|
790
1025
|
}
|
|
791
1026
|
/**
|
|
792
1027
|
* Given any date in a year, return all ZonedDateTimes in that year matching
|
|
@@ -795,11 +1030,117 @@ var RRuleTemporal = class {
|
|
|
795
1030
|
*/
|
|
796
1031
|
generateYearlyOccurrences(sample) {
|
|
797
1032
|
const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
1033
|
+
let occs = [];
|
|
1034
|
+
if (this.opts.byDay && this.opts.byDay.some((t) => /\d{2}/.test(t))) {
|
|
1035
|
+
const dayMap = {
|
|
1036
|
+
MO: 1,
|
|
1037
|
+
TU: 2,
|
|
1038
|
+
WE: 3,
|
|
1039
|
+
TH: 4,
|
|
1040
|
+
FR: 5,
|
|
1041
|
+
SA: 6,
|
|
1042
|
+
SU: 7
|
|
1043
|
+
};
|
|
1044
|
+
for (const tok of this.opts.byDay) {
|
|
1045
|
+
const m = tok.match(/^([+-]?\d{1,2})(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
1046
|
+
if (!m) continue;
|
|
1047
|
+
const ord = parseInt(m[1], 10);
|
|
1048
|
+
const wd = dayMap[m[2]];
|
|
1049
|
+
let dt;
|
|
1050
|
+
if (ord > 0) {
|
|
1051
|
+
const jan1 = sample.with({ month: 1, day: 1 });
|
|
1052
|
+
const delta = (wd - jan1.dayOfWeek + 7) % 7;
|
|
1053
|
+
dt = jan1.add({ days: delta + 7 * (ord - 1) });
|
|
1054
|
+
} else {
|
|
1055
|
+
const dec31 = sample.with({ month: 12, day: 31 });
|
|
1056
|
+
const delta = (dec31.dayOfWeek - wd + 7) % 7;
|
|
1057
|
+
dt = dec31.subtract({ days: delta + 7 * (-ord - 1) });
|
|
1058
|
+
}
|
|
1059
|
+
if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
|
|
1060
|
+
occs.push(...this.expandByTime(dt));
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
} else if (!this.opts.byYearDay && !this.opts.byWeekNo) {
|
|
1064
|
+
occs = months.flatMap((m) => {
|
|
1065
|
+
const monthSample = sample.with({ month: m, day: 1 });
|
|
1066
|
+
return this.generateMonthlyOccurrences(monthSample);
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
if (this.opts.byYearDay) {
|
|
1070
|
+
const last = sample.with({ month: 12, day: 31 }).dayOfYear;
|
|
1071
|
+
for (const d of this.opts.byYearDay) {
|
|
1072
|
+
const dayNum = d > 0 ? d : last + d + 1;
|
|
1073
|
+
const dt = sample.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
|
|
1074
|
+
if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
|
|
1075
|
+
occs.push(...this.expandByTime(dt));
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
if (this.opts.byWeekNo) {
|
|
1080
|
+
const dayMap = {
|
|
1081
|
+
MO: 1,
|
|
1082
|
+
TU: 2,
|
|
1083
|
+
WE: 3,
|
|
1084
|
+
TH: 4,
|
|
1085
|
+
FR: 5,
|
|
1086
|
+
SA: 6,
|
|
1087
|
+
SU: 7
|
|
1088
|
+
};
|
|
1089
|
+
const wkst = dayMap[this.opts.wkst || "MO"];
|
|
1090
|
+
const jan1 = sample.with({ month: 1, day: 1 });
|
|
1091
|
+
const delta = (jan1.dayOfWeek - wkst + 7) % 7;
|
|
1092
|
+
const firstWeekStart = jan1.subtract({ days: delta });
|
|
1093
|
+
const lastWeekDiff = sample.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
|
|
1094
|
+
const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
|
|
1095
|
+
const tokens = this.opts.byDay?.length ? this.opts.byDay.map((tok) => tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)?.[1]) : [this.opts.wkst || "MO"];
|
|
1096
|
+
for (const weekNo of this.opts.byWeekNo) {
|
|
1097
|
+
const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
|
|
1098
|
+
const weekStart = firstWeekStart.add({ weeks: weekIndex });
|
|
1099
|
+
for (const tok of tokens) {
|
|
1100
|
+
if (!tok) continue;
|
|
1101
|
+
const targetDow = dayMap[tok];
|
|
1102
|
+
const inst = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
|
|
1103
|
+
if (!this.opts.byMonth || this.opts.byMonth.includes(inst.month)) {
|
|
1104
|
+
occs.push(...this.expandByTime(inst));
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
occs = occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1110
|
+
occs = this.applyBySetPos(occs);
|
|
1111
|
+
return occs;
|
|
1112
|
+
}
|
|
1113
|
+
applyBySetPos(list2) {
|
|
1114
|
+
const { bySetPos } = this.opts;
|
|
1115
|
+
if (!bySetPos || !bySetPos.length) return list2;
|
|
1116
|
+
const sorted = [...list2].sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1117
|
+
const out = [];
|
|
1118
|
+
const len = sorted.length;
|
|
1119
|
+
for (const pos of bySetPos) {
|
|
1120
|
+
const idx = pos > 0 ? pos - 1 : len + pos;
|
|
1121
|
+
if (idx >= 0 && idx < len) out.push(sorted[idx]);
|
|
1122
|
+
}
|
|
1123
|
+
return out.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1124
|
+
}
|
|
1125
|
+
mergeRDates(list2, startInst, endInst, inc) {
|
|
1126
|
+
if (!this.opts.rDate) return list2;
|
|
1127
|
+
const extras = this.opts.rDate.map(
|
|
1128
|
+
(d) => d instanceof Temporal.ZonedDateTime ? d : Temporal.Instant.from(d.toISOString()).toZonedDateTimeISO(this.tzid)
|
|
1129
|
+
);
|
|
1130
|
+
for (const z of extras) {
|
|
1131
|
+
const inst = z.toInstant();
|
|
1132
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
1133
|
+
const endOk = inc ? Temporal.Instant.compare(inst, endInst) <= 0 : Temporal.Instant.compare(inst, endInst) < 0;
|
|
1134
|
+
if (startOk && endOk) list2.push(z);
|
|
1135
|
+
}
|
|
1136
|
+
list2.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1137
|
+
const dedup = [];
|
|
1138
|
+
for (const d of list2) {
|
|
1139
|
+
if (dedup.length === 0 || Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
|
|
1140
|
+
dedup.push(d);
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
return dedup;
|
|
803
1144
|
}
|
|
804
1145
|
};
|
|
805
1146
|
export {
|