rrule-temporal 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -6
- package/dist/chunk-6VZV7PDA.js +1252 -0
- package/dist/index.cjs +88 -51
- package/dist/index.d.cts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +1 -1
- package/dist/totext.cjs +544 -84
- package/dist/totext.js +456 -33
- package/package.json +3 -2
|
@@ -0,0 +1,1252 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/index.ts
|
|
19
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
20
|
+
function unfoldLine(foldedLine) {
|
|
21
|
+
return foldedLine.replace(/\r?\n[ \t]/g, "");
|
|
22
|
+
}
|
|
23
|
+
function parseDateValues(dateValues, tzid) {
|
|
24
|
+
const dates = [];
|
|
25
|
+
for (const dateValue of dateValues) {
|
|
26
|
+
if (/Z$/.test(dateValue)) {
|
|
27
|
+
const iso = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9, 15)}Z`;
|
|
28
|
+
dates.push(Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC"));
|
|
29
|
+
} else {
|
|
30
|
+
const isoDate = `${dateValue.slice(0, 4)}-${dateValue.slice(4, 6)}-${dateValue.slice(6, 8)}T${dateValue.slice(9)}`;
|
|
31
|
+
dates.push(Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return dates;
|
|
35
|
+
}
|
|
36
|
+
function parseRRuleString(input, targetTimezone) {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
const unfoldedInput = unfoldLine(input).trim();
|
|
39
|
+
let dtstart;
|
|
40
|
+
let tzid = "UTC";
|
|
41
|
+
let rruleLine;
|
|
42
|
+
let exDate = [];
|
|
43
|
+
let rDate = [];
|
|
44
|
+
if (/^DTSTART/mi.test(unfoldedInput)) {
|
|
45
|
+
const lines = unfoldedInput.split(/\s+/);
|
|
46
|
+
const dtLine = lines[0];
|
|
47
|
+
const rrLine = lines.find((line) => line.match(/^RRULE:/i));
|
|
48
|
+
const exLines = lines.filter((line) => line.match(/^EXDATE/i));
|
|
49
|
+
const rLines = lines.filter((line) => line.match(/^RDATE/i));
|
|
50
|
+
const m = dtLine == null ? void 0 : dtLine.match(/DTSTART(?:;TZID=([^:]+))?:(\d{8}T\d{6}Z?)/i);
|
|
51
|
+
if (!m) throw new Error("Invalid DTSTART in ICS snippet");
|
|
52
|
+
tzid = (_b = (_a = m[1]) != null ? _a : targetTimezone) != null ? _b : tzid;
|
|
53
|
+
dtstart = parseDateValues([m[2]], tzid)[0];
|
|
54
|
+
rruleLine = rrLine;
|
|
55
|
+
for (const exLine of exLines) {
|
|
56
|
+
const exMatch = exLine.match(/EXDATE(?:;TZID=([^:]+))?:(.+)/i);
|
|
57
|
+
if (exMatch) {
|
|
58
|
+
const exTzid = exMatch[1] || tzid;
|
|
59
|
+
const dateValues = exMatch[2].split(",");
|
|
60
|
+
exDate.push(...parseDateValues(dateValues, exTzid));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const rLine of rLines) {
|
|
64
|
+
const rMatch = rLine.match(/RDATE(?:;TZID=([^:]+))?:(.+)/i);
|
|
65
|
+
if (rMatch) {
|
|
66
|
+
const rTzid = rMatch[1] || tzid;
|
|
67
|
+
const dateValues = rMatch[2].split(",");
|
|
68
|
+
rDate.push(...parseDateValues(dateValues, rTzid));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
throw new Error("dtstart required when parsing RRULE alone");
|
|
73
|
+
}
|
|
74
|
+
const parts = rruleLine ? rruleLine.replace(/^RRULE:/i, "").split(";") : [];
|
|
75
|
+
const opts = {
|
|
76
|
+
dtstart,
|
|
77
|
+
tzid,
|
|
78
|
+
exDate: exDate.length > 0 ? exDate : void 0,
|
|
79
|
+
rDate: rDate.length > 0 ? rDate : void 0
|
|
80
|
+
};
|
|
81
|
+
for (const part of parts) {
|
|
82
|
+
const [key, val] = part.split("=");
|
|
83
|
+
if (!key) continue;
|
|
84
|
+
switch (key.toUpperCase()) {
|
|
85
|
+
case "FREQ":
|
|
86
|
+
opts.freq = val.toUpperCase();
|
|
87
|
+
break;
|
|
88
|
+
case "INTERVAL":
|
|
89
|
+
opts.interval = parseInt(val, 10);
|
|
90
|
+
break;
|
|
91
|
+
case "COUNT":
|
|
92
|
+
opts.count = parseInt(val, 10);
|
|
93
|
+
break;
|
|
94
|
+
case "UNTIL": {
|
|
95
|
+
if (/Z$/.test(val)) {
|
|
96
|
+
const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}Z`;
|
|
97
|
+
opts.until = Temporal.Instant.from(iso).toZonedDateTimeISO(
|
|
98
|
+
tzid || "UTC"
|
|
99
|
+
);
|
|
100
|
+
} else {
|
|
101
|
+
const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}`;
|
|
102
|
+
opts.until = Temporal.PlainDateTime.from(iso).toZonedDateTime(
|
|
103
|
+
tzid || "UTC"
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case "BYHOUR":
|
|
109
|
+
opts.byHour = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
|
|
110
|
+
break;
|
|
111
|
+
case "BYMINUTE":
|
|
112
|
+
opts.byMinute = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
|
|
113
|
+
break;
|
|
114
|
+
case "BYDAY":
|
|
115
|
+
opts.byDay = val.split(",");
|
|
116
|
+
break;
|
|
117
|
+
case "BYMONTH":
|
|
118
|
+
opts.byMonth = val.split(",").map((n) => parseInt(n, 10));
|
|
119
|
+
break;
|
|
120
|
+
case "BYMONTHDAY":
|
|
121
|
+
opts.byMonthDay = val.split(",").map((n) => parseInt(n, 10));
|
|
122
|
+
break;
|
|
123
|
+
case "BYSECOND":
|
|
124
|
+
opts.bySecond = val.split(",").map((n) => parseInt(n, 10)).sort((a, b) => a - b);
|
|
125
|
+
break;
|
|
126
|
+
case "BYYEARDAY":
|
|
127
|
+
opts.byYearDay = val.split(",").map((n) => parseInt(n, 10));
|
|
128
|
+
break;
|
|
129
|
+
case "BYWEEKNO":
|
|
130
|
+
opts.byWeekNo = val.split(",").map((n) => parseInt(n, 10));
|
|
131
|
+
break;
|
|
132
|
+
case "BYSETPOS":
|
|
133
|
+
opts.bySetPos = val.split(",").map((n) => parseInt(n, 10));
|
|
134
|
+
break;
|
|
135
|
+
case "WKST":
|
|
136
|
+
opts.wkst = val;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return opts;
|
|
141
|
+
}
|
|
142
|
+
var RRuleTemporal = class {
|
|
143
|
+
constructor(params) {
|
|
144
|
+
var _a, _b, _c, _d, _e;
|
|
145
|
+
let manual;
|
|
146
|
+
if ("rruleString" in params) {
|
|
147
|
+
const parsed = parseRRuleString(params.rruleString, params.tzid);
|
|
148
|
+
this.tzid = (_b = (_a = parsed.tzid) != null ? _a : params.tzid) != null ? _b : "UTC";
|
|
149
|
+
this.originalDtstart = parsed.dtstart;
|
|
150
|
+
manual = __spreadValues(__spreadValues({}, params), parsed);
|
|
151
|
+
} else {
|
|
152
|
+
manual = __spreadValues({}, params);
|
|
153
|
+
if (typeof manual.dtstart === "string") {
|
|
154
|
+
throw new Error("Manual dtstart must be a ZonedDateTime");
|
|
155
|
+
}
|
|
156
|
+
manual.tzid = manual.tzid || manual.dtstart.timeZoneId;
|
|
157
|
+
this.tzid = manual.tzid;
|
|
158
|
+
this.originalDtstart = manual.dtstart;
|
|
159
|
+
}
|
|
160
|
+
if (!manual.freq) throw new Error("RRULE must include FREQ");
|
|
161
|
+
manual.interval = (_c = manual.interval) != null ? _c : 1;
|
|
162
|
+
if (manual.interval <= 0) {
|
|
163
|
+
throw new Error("Cannot create RRule: interval must be greater than 0");
|
|
164
|
+
}
|
|
165
|
+
if (manual.until && !(manual.until instanceof Temporal.ZonedDateTime)) {
|
|
166
|
+
throw new Error("Manual until must be a ZonedDateTime");
|
|
167
|
+
}
|
|
168
|
+
this.opts = this.sanitizeOpts(manual);
|
|
169
|
+
this.maxIterations = (_d = manual.maxIterations) != null ? _d : 1e4;
|
|
170
|
+
this.includeDtstart = (_e = manual.includeDtstart) != null ? _e : false;
|
|
171
|
+
}
|
|
172
|
+
sanitizeOpts(opts) {
|
|
173
|
+
const validDay = /^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/;
|
|
174
|
+
if (opts.byDay) {
|
|
175
|
+
opts.byDay = opts.byDay.filter((d) => validDay.test(d));
|
|
176
|
+
if (opts.byDay.length === 0) delete opts.byDay;
|
|
177
|
+
}
|
|
178
|
+
if (opts.byMonth) {
|
|
179
|
+
opts.byMonth = opts.byMonth.filter((n) => Number.isInteger(n) && n >= 1 && n <= 12);
|
|
180
|
+
if (opts.byMonth.length === 0) delete opts.byMonth;
|
|
181
|
+
}
|
|
182
|
+
if (opts.byMonthDay) {
|
|
183
|
+
opts.byMonthDay = opts.byMonthDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -31 && n <= 31);
|
|
184
|
+
if (opts.byMonthDay.length === 0) delete opts.byMonthDay;
|
|
185
|
+
}
|
|
186
|
+
if (opts.byYearDay) {
|
|
187
|
+
opts.byYearDay = opts.byYearDay.filter((n) => Number.isInteger(n) && n !== 0 && n >= -366 && n <= 366);
|
|
188
|
+
if (opts.byYearDay.length === 0) delete opts.byYearDay;
|
|
189
|
+
}
|
|
190
|
+
if (opts.byWeekNo) {
|
|
191
|
+
opts.byWeekNo = opts.byWeekNo.filter((n) => Number.isInteger(n) && n !== 0 && n >= -53 && n <= 53);
|
|
192
|
+
if (opts.byWeekNo.length === 0) delete opts.byWeekNo;
|
|
193
|
+
}
|
|
194
|
+
if (opts.byHour) {
|
|
195
|
+
opts.byHour = opts.byHour.filter((n) => Number.isInteger(n) && n >= 0 && n <= 23).sort((a, b) => a - b);
|
|
196
|
+
if (opts.byHour.length === 0) delete opts.byHour;
|
|
197
|
+
}
|
|
198
|
+
if (opts.byMinute) {
|
|
199
|
+
opts.byMinute = opts.byMinute.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
|
|
200
|
+
if (opts.byMinute.length === 0) delete opts.byMinute;
|
|
201
|
+
}
|
|
202
|
+
if (opts.bySecond) {
|
|
203
|
+
opts.bySecond = opts.bySecond.filter((n) => Number.isInteger(n) && n >= 0 && n <= 59).sort((a, b) => a - b);
|
|
204
|
+
if (opts.bySecond.length === 0) delete opts.bySecond;
|
|
205
|
+
}
|
|
206
|
+
if (opts.bySetPos) {
|
|
207
|
+
opts.bySetPos = opts.bySetPos.filter((n) => Number.isInteger(n) && n !== 0);
|
|
208
|
+
if (opts.bySetPos.length === 0) delete opts.bySetPos;
|
|
209
|
+
}
|
|
210
|
+
return opts;
|
|
211
|
+
}
|
|
212
|
+
rawAdvance(zdt) {
|
|
213
|
+
const { freq, interval } = this.opts;
|
|
214
|
+
switch (freq) {
|
|
215
|
+
case "DAILY":
|
|
216
|
+
return zdt.add({ days: interval });
|
|
217
|
+
case "WEEKLY":
|
|
218
|
+
return zdt.add({ weeks: interval });
|
|
219
|
+
case "MONTHLY":
|
|
220
|
+
return zdt.add({ months: interval });
|
|
221
|
+
case "YEARLY":
|
|
222
|
+
return zdt.add({ years: interval });
|
|
223
|
+
case "HOURLY":
|
|
224
|
+
return zdt.add({ hours: interval });
|
|
225
|
+
case "MINUTELY":
|
|
226
|
+
return zdt.add({ minutes: interval });
|
|
227
|
+
case "SECONDLY":
|
|
228
|
+
return zdt.add({ seconds: interval });
|
|
229
|
+
default:
|
|
230
|
+
throw new Error(`Unsupported FREQ: ${freq}`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** Expand one base ZonedDateTime into all BYHOUR × BYMINUTE × BYSECOND
|
|
234
|
+
* combinations, keeping chronological order. If the options are not
|
|
235
|
+
* present the original date is returned unchanged.
|
|
236
|
+
*/
|
|
237
|
+
expandByTime(base) {
|
|
238
|
+
var _a, _b, _c;
|
|
239
|
+
const hours = (_a = this.opts.byHour) != null ? _a : [base.hour];
|
|
240
|
+
const minutes = (_b = this.opts.byMinute) != null ? _b : [base.minute];
|
|
241
|
+
const seconds = (_c = this.opts.bySecond) != null ? _c : [base.second];
|
|
242
|
+
const out = [];
|
|
243
|
+
for (const h of hours) {
|
|
244
|
+
for (const m of minutes) {
|
|
245
|
+
for (const s of seconds) {
|
|
246
|
+
out.push(base.with({ hour: h, minute: m, second: s }));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return out.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
251
|
+
}
|
|
252
|
+
nextCandidateSameDate(zdt) {
|
|
253
|
+
const { freq, interval = 1, byHour, byMinute, bySecond } = this.opts;
|
|
254
|
+
if (freq === "HOURLY" && byHour && byHour.length === 1) {
|
|
255
|
+
return this.applyTimeOverride(zdt.add({ days: interval }));
|
|
256
|
+
}
|
|
257
|
+
if (freq === "MINUTELY" && byMinute && byMinute.length === 1) {
|
|
258
|
+
return this.applyTimeOverride(zdt.add({ hours: interval }));
|
|
259
|
+
}
|
|
260
|
+
if (freq === "MINUTELY" && byHour && byHour.length > 1 && !byMinute && interval > 1) {
|
|
261
|
+
const next = zdt.add({ minutes: interval });
|
|
262
|
+
if (byHour.includes(next.hour)) {
|
|
263
|
+
return next;
|
|
264
|
+
}
|
|
265
|
+
const nextHour = byHour.find((h) => h > zdt.hour) || byHour[0];
|
|
266
|
+
if (nextHour && nextHour > zdt.hour) {
|
|
267
|
+
return zdt.with({ hour: nextHour, minute: 0 });
|
|
268
|
+
}
|
|
269
|
+
return this.applyTimeOverride(zdt.add({ days: 1 }));
|
|
270
|
+
}
|
|
271
|
+
if (freq === "SECONDLY" && bySecond && bySecond.length === 1) {
|
|
272
|
+
return this.applyTimeOverride(zdt.add({ minutes: interval })).with({ second: bySecond[0] });
|
|
273
|
+
}
|
|
274
|
+
if (freq === "SECONDLY" && byMinute && byMinute.length === 1) {
|
|
275
|
+
const next = zdt.add({ seconds: interval });
|
|
276
|
+
if (next.minute === byMinute[0]) return next;
|
|
277
|
+
return this.applyTimeOverride(zdt.add({ hours: interval })).with({ second: 0 });
|
|
278
|
+
}
|
|
279
|
+
if (bySecond && bySecond.length > 1) {
|
|
280
|
+
const idx = bySecond.indexOf(zdt.second);
|
|
281
|
+
if (idx !== -1 && idx < bySecond.length - 1) {
|
|
282
|
+
return zdt.with({ second: bySecond[idx + 1] });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (byMinute && byMinute.length > 1) {
|
|
286
|
+
const idx = byMinute.indexOf(zdt.minute);
|
|
287
|
+
if (idx !== -1 && idx < byMinute.length - 1) {
|
|
288
|
+
return zdt.with({
|
|
289
|
+
minute: byMinute[idx + 1],
|
|
290
|
+
second: bySecond ? bySecond[0] : zdt.second
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (byHour && byHour.length > 1) {
|
|
295
|
+
const idx = byHour.indexOf(zdt.hour);
|
|
296
|
+
if (idx !== -1 && idx < byHour.length - 1) {
|
|
297
|
+
return zdt.with({
|
|
298
|
+
hour: byHour[idx + 1],
|
|
299
|
+
minute: byMinute ? byMinute[0] : zdt.minute,
|
|
300
|
+
second: bySecond ? bySecond[0] : zdt.second
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return this.applyTimeOverride(this.rawAdvance(zdt));
|
|
305
|
+
}
|
|
306
|
+
applyTimeOverride(zdt) {
|
|
307
|
+
const { byHour, byMinute, bySecond } = this.opts;
|
|
308
|
+
let dt = zdt;
|
|
309
|
+
if (byHour) dt = dt.with({ hour: byHour[0] });
|
|
310
|
+
if (byMinute) dt = dt.with({ minute: byMinute[0] });
|
|
311
|
+
if (bySecond) dt = dt.with({ second: bySecond[0] });
|
|
312
|
+
return dt;
|
|
313
|
+
}
|
|
314
|
+
computeFirst() {
|
|
315
|
+
var _a;
|
|
316
|
+
let zdt = this.originalDtstart;
|
|
317
|
+
if ((_a = this.opts.byDay) == null ? void 0 : _a.length) {
|
|
318
|
+
const dayMap = {
|
|
319
|
+
MO: 1,
|
|
320
|
+
TU: 2,
|
|
321
|
+
WE: 3,
|
|
322
|
+
TH: 4,
|
|
323
|
+
FR: 5,
|
|
324
|
+
SA: 6,
|
|
325
|
+
SU: 7
|
|
326
|
+
};
|
|
327
|
+
let deltas = [];
|
|
328
|
+
if (["DAILY", "HOURLY", "MINUTELY", "SECONDLY"].includes(this.opts.freq) && this.opts.byDay.every((tok) => /^[A-Z]{2}$/.test(tok))) {
|
|
329
|
+
deltas = this.opts.byDay.map(
|
|
330
|
+
(tok) => (dayMap[tok] - zdt.dayOfWeek + 7) % 7
|
|
331
|
+
);
|
|
332
|
+
} else {
|
|
333
|
+
deltas = this.opts.byDay.map((tok) => {
|
|
334
|
+
var _a2;
|
|
335
|
+
const wdTok = (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
|
|
336
|
+
return wdTok ? (dayMap[wdTok] - zdt.dayOfWeek + 7) % 7 : null;
|
|
337
|
+
}).filter((d) => d !== null);
|
|
338
|
+
}
|
|
339
|
+
if (deltas.length) {
|
|
340
|
+
zdt = zdt.add({ days: Math.min(...deltas) });
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
const { byHour, byMinute, bySecond } = this.opts;
|
|
344
|
+
if (byHour || byMinute || bySecond) {
|
|
345
|
+
zdt = this.applyTimeOverride(zdt);
|
|
346
|
+
if (Temporal.Instant.compare(
|
|
347
|
+
zdt.toInstant(),
|
|
348
|
+
this.originalDtstart.toInstant()
|
|
349
|
+
) < 0) {
|
|
350
|
+
zdt = this.applyTimeOverride(this.rawAdvance(this.originalDtstart));
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return zdt;
|
|
354
|
+
}
|
|
355
|
+
// inside class RRuleTemporal:
|
|
356
|
+
generateWeeklyOccurrences(sample) {
|
|
357
|
+
var _a;
|
|
358
|
+
const dayMap = {
|
|
359
|
+
MO: 1,
|
|
360
|
+
TU: 2,
|
|
361
|
+
WE: 3,
|
|
362
|
+
TH: 4,
|
|
363
|
+
FR: 5,
|
|
364
|
+
SA: 6,
|
|
365
|
+
SU: 7
|
|
366
|
+
};
|
|
367
|
+
const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? [...this.opts.byDay] : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
|
|
368
|
+
const occs = tokens.flatMap((tok) => {
|
|
369
|
+
const targetDow = dayMap[tok];
|
|
370
|
+
const delta = (targetDow - sample.dayOfWeek + 7) % 7;
|
|
371
|
+
const sameDate = sample.add({ days: delta });
|
|
372
|
+
return this.expandByTime(sameDate);
|
|
373
|
+
});
|
|
374
|
+
return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
375
|
+
}
|
|
376
|
+
// --- NEW: constraint checks ---
|
|
377
|
+
// 2) Replace your matchesByDay with this:
|
|
378
|
+
matchesByDay(zdt) {
|
|
379
|
+
const { byDay } = this.opts;
|
|
380
|
+
if (!byDay) return true;
|
|
381
|
+
const dayMap = {
|
|
382
|
+
MO: 1,
|
|
383
|
+
TU: 2,
|
|
384
|
+
WE: 3,
|
|
385
|
+
TH: 4,
|
|
386
|
+
FR: 5,
|
|
387
|
+
SA: 6,
|
|
388
|
+
SU: 7
|
|
389
|
+
};
|
|
390
|
+
for (const token of byDay) {
|
|
391
|
+
const m = token.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
392
|
+
if (!m) continue;
|
|
393
|
+
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
394
|
+
const weekday = m[2];
|
|
395
|
+
if (!weekday) continue;
|
|
396
|
+
const wd = dayMap[weekday];
|
|
397
|
+
if (ord === 0) {
|
|
398
|
+
if (zdt.dayOfWeek === wd) return true;
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
const month = zdt.month;
|
|
402
|
+
let dt = zdt.with({ day: 1 });
|
|
403
|
+
const candidates = [];
|
|
404
|
+
while (dt.month === month) {
|
|
405
|
+
if (dt.dayOfWeek === wd) candidates.push(dt.day);
|
|
406
|
+
dt = dt.add({ days: 1 });
|
|
407
|
+
}
|
|
408
|
+
const idx = ord > 0 ? ord - 1 : candidates.length + ord;
|
|
409
|
+
if (candidates[idx] === zdt.day) return true;
|
|
410
|
+
}
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
matchesByMonth(zdt) {
|
|
414
|
+
const { byMonth } = this.opts;
|
|
415
|
+
if (!byMonth) return true;
|
|
416
|
+
return byMonth.includes(zdt.month);
|
|
417
|
+
}
|
|
418
|
+
matchesByMonthDay(zdt) {
|
|
419
|
+
const { byMonthDay } = this.opts;
|
|
420
|
+
if (!byMonthDay) return true;
|
|
421
|
+
const lastDay = zdt.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
422
|
+
return byMonthDay.some(
|
|
423
|
+
(d) => d > 0 ? zdt.day === d : zdt.day === lastDay + d + 1
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
matchesAll(zdt) {
|
|
427
|
+
return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt) && this.matchesByYearDay(zdt) && this.matchesByWeekNo(zdt);
|
|
428
|
+
}
|
|
429
|
+
matchesByYearDay(zdt) {
|
|
430
|
+
const { byYearDay } = this.opts;
|
|
431
|
+
if (!byYearDay) return true;
|
|
432
|
+
const dayOfYear = zdt.dayOfYear;
|
|
433
|
+
const last = zdt.with({ month: 12, day: 31 }).dayOfYear;
|
|
434
|
+
return byYearDay.some((d) => d > 0 ? dayOfYear === d : dayOfYear === last + d + 1);
|
|
435
|
+
}
|
|
436
|
+
matchesByWeekNo(zdt) {
|
|
437
|
+
const { byWeekNo, wkst } = this.opts;
|
|
438
|
+
if (!byWeekNo) return true;
|
|
439
|
+
const dayMap = { MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6, SU: 7 };
|
|
440
|
+
const startDow = dayMap[wkst != null ? wkst : "MO"];
|
|
441
|
+
const jan1 = zdt.with({ month: 1, day: 1 });
|
|
442
|
+
const delta = (jan1.dayOfWeek - startDow + 7) % 7;
|
|
443
|
+
const firstWeekStart = jan1.subtract({ days: delta });
|
|
444
|
+
const diffDays = zdt.toPlainDate().since(firstWeekStart.toPlainDate()).days;
|
|
445
|
+
const week = Math.floor(diffDays / 7) + 1;
|
|
446
|
+
const lastWeekDiff = zdt.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
|
|
447
|
+
const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
|
|
448
|
+
return byWeekNo.some((n) => n > 0 ? week === n : week === lastWeek + n + 1);
|
|
449
|
+
}
|
|
450
|
+
options() {
|
|
451
|
+
return this.opts;
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Returns all occurrences of the rule.
|
|
455
|
+
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
456
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
457
|
+
*/
|
|
458
|
+
all(iterator) {
|
|
459
|
+
var _a;
|
|
460
|
+
if (!this.opts.count && !this.opts.until && !iterator) {
|
|
461
|
+
throw new Error("all() requires iterator when no COUNT/UNTIL");
|
|
462
|
+
}
|
|
463
|
+
const dates = [];
|
|
464
|
+
let iterationCount = 0;
|
|
465
|
+
if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
466
|
+
const start = this.originalDtstart;
|
|
467
|
+
let monthCursor = start.with({ day: 1 });
|
|
468
|
+
let matchCount2 = 0;
|
|
469
|
+
if (this.includeDtstart && !this.matchesAll(start)) {
|
|
470
|
+
if (iterator && !iterator(start, matchCount2)) {
|
|
471
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
472
|
+
}
|
|
473
|
+
dates.push(start);
|
|
474
|
+
matchCount2++;
|
|
475
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
476
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
outer_month: while (true) {
|
|
480
|
+
if (++iterationCount > this.maxIterations) {
|
|
481
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
482
|
+
}
|
|
483
|
+
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
484
|
+
if (monthCursor.month === start.month && occs.some((o) => Temporal.ZonedDateTime.compare(o, start) < 0) && occs.some((o) => Temporal.ZonedDateTime.compare(o, start) === 0)) {
|
|
485
|
+
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
486
|
+
continue outer_month;
|
|
487
|
+
}
|
|
488
|
+
for (const occ of occs) {
|
|
489
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
490
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
491
|
+
break outer_month;
|
|
492
|
+
}
|
|
493
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
494
|
+
break outer_month;
|
|
495
|
+
}
|
|
496
|
+
dates.push(occ);
|
|
497
|
+
matchCount2++;
|
|
498
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
499
|
+
break outer_month;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
503
|
+
}
|
|
504
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
505
|
+
}
|
|
506
|
+
if (this.opts.freq === "WEEKLY") {
|
|
507
|
+
const start = this.originalDtstart;
|
|
508
|
+
if (this.includeDtstart && !this.matchesAll(start)) {
|
|
509
|
+
if (iterator && !iterator(start, 0)) {
|
|
510
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
511
|
+
}
|
|
512
|
+
dates.push(start);
|
|
513
|
+
if (this.shouldBreakForCountLimit(1)) {
|
|
514
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const dayMap = {
|
|
518
|
+
MO: 1,
|
|
519
|
+
TU: 2,
|
|
520
|
+
WE: 3,
|
|
521
|
+
TH: 4,
|
|
522
|
+
FR: 5,
|
|
523
|
+
SA: 6,
|
|
524
|
+
SU: 7
|
|
525
|
+
};
|
|
526
|
+
const tokens = this.opts.byDay ? [...this.opts.byDay] : [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
|
|
527
|
+
const dows = tokens.map((tok) => dayMap[tok]).filter((d) => d !== void 0).sort((a, b) => a - b);
|
|
528
|
+
const firstWeekDates = dows.map((dw) => {
|
|
529
|
+
const delta = (dw - start.dayOfWeek + 7) % 7;
|
|
530
|
+
return start.add({ days: delta });
|
|
531
|
+
});
|
|
532
|
+
let firstOccurrence = firstWeekDates.reduce(
|
|
533
|
+
(a, b) => Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b
|
|
534
|
+
);
|
|
535
|
+
const wkstDay = (_a = dayMap[this.opts.wkst || "MO"]) != null ? _a : 1;
|
|
536
|
+
const firstOccWeekOffset = (firstOccurrence.dayOfWeek - wkstDay + 7) % 7;
|
|
537
|
+
let weekCursor = firstOccurrence.subtract({ days: firstOccWeekOffset });
|
|
538
|
+
let matchCount2 = 0;
|
|
539
|
+
outer_week: while (true) {
|
|
540
|
+
if (++iterationCount > this.maxIterations) {
|
|
541
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
542
|
+
}
|
|
543
|
+
const occs = dows.flatMap((dw) => {
|
|
544
|
+
const delta = (dw - wkstDay + 7) % 7;
|
|
545
|
+
const sameDate = weekCursor.add({ days: delta });
|
|
546
|
+
return this.expandByTime(sameDate);
|
|
547
|
+
}).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
548
|
+
for (const occ of occs) {
|
|
549
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
550
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
551
|
+
break outer_week;
|
|
552
|
+
}
|
|
553
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
554
|
+
break outer_week;
|
|
555
|
+
}
|
|
556
|
+
dates.push(occ);
|
|
557
|
+
matchCount2++;
|
|
558
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
559
|
+
break outer_week;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
563
|
+
}
|
|
564
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
565
|
+
}
|
|
566
|
+
if (this.opts.freq === "MONTHLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
|
|
567
|
+
const start = this.originalDtstart;
|
|
568
|
+
if (this.includeDtstart && !this.matchesAll(start)) {
|
|
569
|
+
if (iterator && !iterator(start, 0)) {
|
|
570
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
571
|
+
}
|
|
572
|
+
dates.push(start);
|
|
573
|
+
if (this.shouldBreakForCountLimit(1)) {
|
|
574
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
578
|
+
let monthOffset = 0;
|
|
579
|
+
let matchCount2 = dates.length;
|
|
580
|
+
let startMonthIndex = months.findIndex((m) => m >= start.month);
|
|
581
|
+
if (startMonthIndex === -1) {
|
|
582
|
+
startMonthIndex = 0;
|
|
583
|
+
monthOffset = 1;
|
|
584
|
+
}
|
|
585
|
+
while (true) {
|
|
586
|
+
if (++iterationCount > this.maxIterations) {
|
|
587
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
588
|
+
}
|
|
589
|
+
const monthIndex = startMonthIndex + monthOffset;
|
|
590
|
+
const targetMonth = months[monthIndex % months.length];
|
|
591
|
+
const yearsToAdd = Math.floor(monthIndex / months.length);
|
|
592
|
+
const candidate = start.with({
|
|
593
|
+
year: start.year + yearsToAdd,
|
|
594
|
+
month: targetMonth
|
|
595
|
+
});
|
|
596
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(candidate, this.opts.until) > 0) {
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
if (Temporal.ZonedDateTime.compare(candidate, start) >= 0) {
|
|
600
|
+
if (iterator && !iterator(candidate, matchCount2)) {
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
dates.push(candidate);
|
|
604
|
+
matchCount2++;
|
|
605
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
monthOffset++;
|
|
610
|
+
}
|
|
611
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
612
|
+
}
|
|
613
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
|
|
614
|
+
const start = this.originalDtstart;
|
|
615
|
+
if (this.includeDtstart && !this.matchesAll(start)) {
|
|
616
|
+
if (iterator && !iterator(start, 0)) {
|
|
617
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
618
|
+
}
|
|
619
|
+
dates.push(start);
|
|
620
|
+
if (this.shouldBreakForCountLimit(1)) {
|
|
621
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
625
|
+
let yearOffset = 0;
|
|
626
|
+
let matchCount2 = 0;
|
|
627
|
+
while (true) {
|
|
628
|
+
if (++iterationCount > this.maxIterations) {
|
|
629
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
630
|
+
}
|
|
631
|
+
const year = start.year + yearOffset * this.opts.interval;
|
|
632
|
+
for (const month of months) {
|
|
633
|
+
let occ = start.with({ year, month });
|
|
634
|
+
occ = this.applyTimeOverride(occ);
|
|
635
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) {
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
639
|
+
return dates;
|
|
640
|
+
}
|
|
641
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
642
|
+
return dates;
|
|
643
|
+
}
|
|
644
|
+
dates.push(occ);
|
|
645
|
+
matchCount2++;
|
|
646
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
647
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
yearOffset++;
|
|
651
|
+
}
|
|
652
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
653
|
+
}
|
|
654
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
655
|
+
const start = this.originalDtstart;
|
|
656
|
+
if (this.includeDtstart && !this.matchesAll(start)) {
|
|
657
|
+
if (iterator && !iterator(start, 0)) {
|
|
658
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
659
|
+
}
|
|
660
|
+
dates.push(start);
|
|
661
|
+
if (this.shouldBreakForCountLimit(1)) {
|
|
662
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
666
|
+
let matchCount2 = 0;
|
|
667
|
+
outer_year: while (true) {
|
|
668
|
+
if (++iterationCount > this.maxIterations) {
|
|
669
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
670
|
+
}
|
|
671
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
672
|
+
for (const occ of occs) {
|
|
673
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
674
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
675
|
+
break outer_year;
|
|
676
|
+
}
|
|
677
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
678
|
+
break outer_year;
|
|
679
|
+
}
|
|
680
|
+
dates.push(occ);
|
|
681
|
+
matchCount2++;
|
|
682
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
683
|
+
break outer_year;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
687
|
+
}
|
|
688
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
689
|
+
}
|
|
690
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
|
|
691
|
+
const start = this.originalDtstart;
|
|
692
|
+
if (this.includeDtstart && !this.matchesAll(start)) {
|
|
693
|
+
if (iterator && !iterator(start, 0)) {
|
|
694
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
695
|
+
}
|
|
696
|
+
dates.push(start);
|
|
697
|
+
if (this.shouldBreakForCountLimit(1)) {
|
|
698
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
702
|
+
let matchCount2 = 0;
|
|
703
|
+
outer_year2: while (true) {
|
|
704
|
+
if (++iterationCount > this.maxIterations) {
|
|
705
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
706
|
+
}
|
|
707
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
708
|
+
for (const occ of occs) {
|
|
709
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
710
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
711
|
+
break outer_year2;
|
|
712
|
+
}
|
|
713
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
714
|
+
break outer_year2;
|
|
715
|
+
}
|
|
716
|
+
dates.push(occ);
|
|
717
|
+
matchCount2++;
|
|
718
|
+
if (this.shouldBreakForCountLimit(matchCount2)) {
|
|
719
|
+
break outer_year2;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
723
|
+
}
|
|
724
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
725
|
+
}
|
|
726
|
+
let current = this.computeFirst();
|
|
727
|
+
let matchCount = 0;
|
|
728
|
+
if (this.includeDtstart && Temporal.ZonedDateTime.compare(current, this.originalDtstart) > 0) {
|
|
729
|
+
if (iterator && !iterator(this.originalDtstart, matchCount)) {
|
|
730
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
731
|
+
}
|
|
732
|
+
dates.push(this.originalDtstart);
|
|
733
|
+
matchCount++;
|
|
734
|
+
if (this.shouldBreakForCountLimit(matchCount)) {
|
|
735
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
while (true) {
|
|
739
|
+
if (++iterationCount > this.maxIterations) {
|
|
740
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in all()`);
|
|
741
|
+
}
|
|
742
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
|
|
743
|
+
break;
|
|
744
|
+
}
|
|
745
|
+
if (this.matchesAll(current)) {
|
|
746
|
+
if (iterator && !iterator(current, matchCount)) {
|
|
747
|
+
break;
|
|
748
|
+
}
|
|
749
|
+
dates.push(current);
|
|
750
|
+
matchCount++;
|
|
751
|
+
if (this.shouldBreakForCountLimit(matchCount)) {
|
|
752
|
+
break;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
current = this.nextCandidateSameDate(current);
|
|
756
|
+
}
|
|
757
|
+
return this.applyCountLimitAndMergeRDates(dates, iterator);
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Converts rDate entries to ZonedDateTime and merges with existing dates.
|
|
761
|
+
* @param dates - Array of dates to merge with
|
|
762
|
+
* @returns Merged and deduplicated array of dates
|
|
763
|
+
*/
|
|
764
|
+
mergeAndDeduplicateRDates(dates) {
|
|
765
|
+
if (!this.opts.rDate) return dates;
|
|
766
|
+
dates.push(...this.opts.rDate);
|
|
767
|
+
dates.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
768
|
+
const dedup = [];
|
|
769
|
+
for (const d of dates) {
|
|
770
|
+
if (dedup.length === 0 || Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
|
|
771
|
+
dedup.push(d);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
return dedup;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Excludes exDate entries from the given array of dates.
|
|
778
|
+
* @param dates - Array of dates to filter
|
|
779
|
+
* @returns Filtered array with exDate entries removed
|
|
780
|
+
*/
|
|
781
|
+
excludeExDates(dates) {
|
|
782
|
+
if (!this.opts.exDate || this.opts.exDate.length === 0) return dates;
|
|
783
|
+
return dates.filter((date) => {
|
|
784
|
+
return !this.opts.exDate.some(
|
|
785
|
+
(exDate) => Temporal.ZonedDateTime.compare(date, exDate) === 0
|
|
786
|
+
);
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Applies count limit and merges rDates with the rule-generated dates.
|
|
791
|
+
* @param dates - Array of dates generated by the rule
|
|
792
|
+
* @param iterator - Optional iterator function
|
|
793
|
+
* @returns Final array of dates after merging and applying count limit
|
|
794
|
+
*/
|
|
795
|
+
applyCountLimitAndMergeRDates(dates, iterator) {
|
|
796
|
+
const merged = this.mergeAndDeduplicateRDates(dates);
|
|
797
|
+
const excluded = this.excludeExDates(merged);
|
|
798
|
+
if (this.opts.count !== void 0) {
|
|
799
|
+
let finalCount = 0;
|
|
800
|
+
const finalDates = [];
|
|
801
|
+
for (const d of excluded) {
|
|
802
|
+
if (finalCount >= this.opts.count) break;
|
|
803
|
+
if (iterator && !iterator(d, finalCount)) break;
|
|
804
|
+
finalDates.push(d);
|
|
805
|
+
finalCount++;
|
|
806
|
+
}
|
|
807
|
+
return finalDates;
|
|
808
|
+
}
|
|
809
|
+
return excluded;
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Checks if the count limit should break the loop based on rDate presence.
|
|
813
|
+
* @param matchCount - Current number of matches
|
|
814
|
+
* @returns true if the loop should break
|
|
815
|
+
*/
|
|
816
|
+
shouldBreakForCountLimit(matchCount) {
|
|
817
|
+
if (this.opts.count === void 0) return false;
|
|
818
|
+
if (!this.opts.rDate) {
|
|
819
|
+
return matchCount >= this.opts.count;
|
|
820
|
+
}
|
|
821
|
+
const rDateCount = this.opts.rDate.length;
|
|
822
|
+
const targetRuleCount = Math.max(this.opts.count - rDateCount, 0);
|
|
823
|
+
const safetyMargin = Math.min(targetRuleCount, 10);
|
|
824
|
+
return matchCount >= targetRuleCount + safetyMargin;
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* Returns all occurrences of the rule within a specified time window.
|
|
828
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
829
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
830
|
+
* @param inc - Optional boolean flag to include the end date in the results.
|
|
831
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
832
|
+
*/
|
|
833
|
+
between(after, before, inc = false) {
|
|
834
|
+
const startInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
835
|
+
const endInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
836
|
+
const results = [];
|
|
837
|
+
let iterationCount = 0;
|
|
838
|
+
if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
839
|
+
let monthCursor = this.computeFirst().with({ day: 1 });
|
|
840
|
+
outer: while (true) {
|
|
841
|
+
if (++iterationCount > this.maxIterations) {
|
|
842
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
|
|
843
|
+
}
|
|
844
|
+
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
845
|
+
for (const occ of occs) {
|
|
846
|
+
const inst = occ.toInstant();
|
|
847
|
+
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
848
|
+
break outer;
|
|
849
|
+
}
|
|
850
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
851
|
+
if (startOk) {
|
|
852
|
+
results.push(occ);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
856
|
+
}
|
|
857
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
858
|
+
}
|
|
859
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
|
|
860
|
+
const start = this.originalDtstart;
|
|
861
|
+
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
862
|
+
let yearOffset = 0;
|
|
863
|
+
outer: while (true) {
|
|
864
|
+
if (++iterationCount > this.maxIterations) {
|
|
865
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
|
|
866
|
+
}
|
|
867
|
+
const year = start.year + yearOffset * this.opts.interval;
|
|
868
|
+
for (const month of months) {
|
|
869
|
+
let occ = start.with({ year, month });
|
|
870
|
+
occ = this.applyTimeOverride(occ);
|
|
871
|
+
const inst = occ.toInstant();
|
|
872
|
+
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
873
|
+
break outer;
|
|
874
|
+
}
|
|
875
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
876
|
+
if (startOk) {
|
|
877
|
+
results.push(occ);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
yearOffset++;
|
|
881
|
+
}
|
|
882
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
883
|
+
}
|
|
884
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
885
|
+
const start = this.originalDtstart;
|
|
886
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
887
|
+
outer_year: while (true) {
|
|
888
|
+
if (++iterationCount > this.maxIterations) {
|
|
889
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
|
|
890
|
+
}
|
|
891
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
892
|
+
for (const occ of occs) {
|
|
893
|
+
const inst = occ.toInstant();
|
|
894
|
+
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
895
|
+
break outer_year;
|
|
896
|
+
}
|
|
897
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
898
|
+
if (startOk) {
|
|
899
|
+
results.push(occ);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
903
|
+
}
|
|
904
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
905
|
+
}
|
|
906
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byYearDay || this.opts.byWeekNo)) {
|
|
907
|
+
const start = this.originalDtstart;
|
|
908
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
909
|
+
outer_year2: while (true) {
|
|
910
|
+
if (++iterationCount > this.maxIterations) {
|
|
911
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
|
|
912
|
+
}
|
|
913
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
914
|
+
for (const occ of occs) {
|
|
915
|
+
const inst = occ.toInstant();
|
|
916
|
+
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
917
|
+
break outer_year2;
|
|
918
|
+
}
|
|
919
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
920
|
+
if (startOk) {
|
|
921
|
+
results.push(occ);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
925
|
+
}
|
|
926
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
927
|
+
}
|
|
928
|
+
if (this.opts.freq === "WEEKLY") {
|
|
929
|
+
const startInst2 = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
930
|
+
const endInst2 = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
931
|
+
let weekCursor = this.computeFirst();
|
|
932
|
+
const results2 = [];
|
|
933
|
+
outer: while (true) {
|
|
934
|
+
if (++iterationCount > this.maxIterations) {
|
|
935
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
|
|
936
|
+
}
|
|
937
|
+
const occs = this.generateWeeklyOccurrences(weekCursor);
|
|
938
|
+
for (const occ of occs) {
|
|
939
|
+
const inst = occ.toInstant();
|
|
940
|
+
if (inc ? Temporal.Instant.compare(inst, endInst2) > 0 : Temporal.Instant.compare(inst, endInst2) >= 0) {
|
|
941
|
+
break outer;
|
|
942
|
+
}
|
|
943
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst2) >= 0 : Temporal.Instant.compare(inst, startInst2) > 0;
|
|
944
|
+
if (startOk) {
|
|
945
|
+
results2.push(occ);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
949
|
+
}
|
|
950
|
+
return this.mergeRDates(results2, startInst2, endInst2, inc);
|
|
951
|
+
}
|
|
952
|
+
let current = this.computeFirst();
|
|
953
|
+
while (true) {
|
|
954
|
+
if (++iterationCount > this.maxIterations) {
|
|
955
|
+
throw new Error(`Maximum iterations (${this.maxIterations}) exceeded in between()`);
|
|
956
|
+
}
|
|
957
|
+
const inst = current.toInstant();
|
|
958
|
+
if (inc) {
|
|
959
|
+
if (Temporal.Instant.compare(inst, endInst) > 0) break;
|
|
960
|
+
} else {
|
|
961
|
+
if (Temporal.Instant.compare(inst, endInst) >= 0) break;
|
|
962
|
+
}
|
|
963
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
964
|
+
if (startOk && this.matchesAll(current)) {
|
|
965
|
+
results.push(current);
|
|
966
|
+
}
|
|
967
|
+
current = this.nextCandidateSameDate(current);
|
|
968
|
+
}
|
|
969
|
+
return this.mergeRDates(results, startInst, endInst, inc);
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Returns the next occurrence of the rule after a specified date.
|
|
973
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
974
|
+
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
975
|
+
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
976
|
+
*/
|
|
977
|
+
next(after = /* @__PURE__ */ new Date(), inc = false) {
|
|
978
|
+
const afterInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
979
|
+
let result = null;
|
|
980
|
+
this.all((occ) => {
|
|
981
|
+
const inst = occ.toInstant();
|
|
982
|
+
const ok = inc ? Temporal.Instant.compare(inst, afterInst) >= 0 : Temporal.Instant.compare(inst, afterInst) > 0;
|
|
983
|
+
if (ok) {
|
|
984
|
+
result = occ;
|
|
985
|
+
return false;
|
|
986
|
+
}
|
|
987
|
+
return true;
|
|
988
|
+
});
|
|
989
|
+
return result;
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Returns the previous occurrence of the rule before a specified date.
|
|
993
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
994
|
+
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
995
|
+
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
996
|
+
*/
|
|
997
|
+
previous(before = /* @__PURE__ */ new Date(), inc = false) {
|
|
998
|
+
const beforeInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
999
|
+
let prev = null;
|
|
1000
|
+
this.all((occ) => {
|
|
1001
|
+
const inst = occ.toInstant();
|
|
1002
|
+
const beyond = inc ? Temporal.Instant.compare(inst, beforeInst) > 0 : Temporal.Instant.compare(inst, beforeInst) >= 0;
|
|
1003
|
+
if (beyond) return false;
|
|
1004
|
+
prev = occ;
|
|
1005
|
+
return true;
|
|
1006
|
+
});
|
|
1007
|
+
return prev;
|
|
1008
|
+
}
|
|
1009
|
+
toString() {
|
|
1010
|
+
const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
|
|
1011
|
+
const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
|
|
1012
|
+
const rule = [];
|
|
1013
|
+
const {
|
|
1014
|
+
freq,
|
|
1015
|
+
interval,
|
|
1016
|
+
count,
|
|
1017
|
+
until,
|
|
1018
|
+
byHour,
|
|
1019
|
+
byMinute,
|
|
1020
|
+
bySecond,
|
|
1021
|
+
byDay,
|
|
1022
|
+
byMonth,
|
|
1023
|
+
byMonthDay,
|
|
1024
|
+
bySetPos,
|
|
1025
|
+
byWeekNo,
|
|
1026
|
+
byYearDay,
|
|
1027
|
+
wkst,
|
|
1028
|
+
rDate,
|
|
1029
|
+
exDate
|
|
1030
|
+
} = this.opts;
|
|
1031
|
+
rule.push(`FREQ=${freq}`);
|
|
1032
|
+
if (interval !== 1) rule.push(`INTERVAL=${interval}`);
|
|
1033
|
+
if (count !== void 0) rule.push(`COUNT=${count}`);
|
|
1034
|
+
if (until) {
|
|
1035
|
+
const u = until.toInstant().toString().replace(/[-:]/g, "");
|
|
1036
|
+
rule.push(`UNTIL=${u.slice(0, 15)}Z`);
|
|
1037
|
+
}
|
|
1038
|
+
if (byHour) rule.push(`BYHOUR=${byHour.join(",")}`);
|
|
1039
|
+
if (byMinute) rule.push(`BYMINUTE=${byMinute.join(",")}`);
|
|
1040
|
+
if (bySecond) rule.push(`BYSECOND=${bySecond.join(",")}`);
|
|
1041
|
+
if (byDay) rule.push(`BYDAY=${byDay.join(",")}`);
|
|
1042
|
+
if (byMonth) rule.push(`BYMONTH=${byMonth.join(",")}`);
|
|
1043
|
+
if (byMonthDay) rule.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
|
|
1044
|
+
if (bySetPos) rule.push(`BYSETPOS=${bySetPos.join(",")}`);
|
|
1045
|
+
if (byWeekNo) rule.push(`BYWEEKNO=${byWeekNo.join(",")}`);
|
|
1046
|
+
if (byYearDay) rule.push(`BYYEARDAY=${byYearDay.join(",")}`);
|
|
1047
|
+
if (wkst) rule.push(`WKST=${wkst}`);
|
|
1048
|
+
const lines = [dtLine, `RRULE:${rule.join(";")}`];
|
|
1049
|
+
if (rDate) {
|
|
1050
|
+
lines.push(`RDATE:${this.joinDates(rDate)}`);
|
|
1051
|
+
}
|
|
1052
|
+
if (exDate) {
|
|
1053
|
+
lines.push(`EXDATE:${this.joinDates(exDate)}`);
|
|
1054
|
+
}
|
|
1055
|
+
return lines.join("\n");
|
|
1056
|
+
}
|
|
1057
|
+
joinDates(dates) {
|
|
1058
|
+
return dates.map((d) => d.toInstant().toString().replace(/[-:]/g, "").slice(0, 15) + "Z");
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
1062
|
+
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
1063
|
+
*/
|
|
1064
|
+
generateMonthlyOccurrences(sample) {
|
|
1065
|
+
var _a;
|
|
1066
|
+
const { byDay, byMonth, byMonthDay } = this.opts;
|
|
1067
|
+
if (byMonth && !byMonth.includes(sample.month)) return [];
|
|
1068
|
+
const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
1069
|
+
let byMonthDayHits = [];
|
|
1070
|
+
if (byMonthDay && byMonthDay.length > 0) {
|
|
1071
|
+
byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
|
|
1072
|
+
}
|
|
1073
|
+
if (!byDay && byMonthDay && byMonthDay.length > 0) {
|
|
1074
|
+
if (byMonthDayHits.length === 0) {
|
|
1075
|
+
return [];
|
|
1076
|
+
}
|
|
1077
|
+
const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
|
|
1078
|
+
return dates.flatMap((z) => this.expandByTime(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1079
|
+
}
|
|
1080
|
+
if (!byDay) {
|
|
1081
|
+
return this.expandByTime(sample);
|
|
1082
|
+
}
|
|
1083
|
+
const dayMap = {
|
|
1084
|
+
MO: 1,
|
|
1085
|
+
TU: 2,
|
|
1086
|
+
WE: 3,
|
|
1087
|
+
TH: 4,
|
|
1088
|
+
FR: 5,
|
|
1089
|
+
SA: 6,
|
|
1090
|
+
SU: 7
|
|
1091
|
+
};
|
|
1092
|
+
const tokens = byDay.map((tok) => {
|
|
1093
|
+
const m = tok.match(/^([+-]?\d{1,2})?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
1094
|
+
if (!m) return null;
|
|
1095
|
+
return { ord: m[1] ? parseInt(m[1], 10) : 0, wd: dayMap[m[2]] };
|
|
1096
|
+
}).filter((x) => x !== null);
|
|
1097
|
+
const buckets = {};
|
|
1098
|
+
let cursor = sample.with({ day: 1 });
|
|
1099
|
+
while (cursor.month === sample.month) {
|
|
1100
|
+
const dow = cursor.dayOfWeek;
|
|
1101
|
+
(buckets[dow] || (buckets[dow] = [])).push(cursor.day);
|
|
1102
|
+
cursor = cursor.add({ days: 1 });
|
|
1103
|
+
}
|
|
1104
|
+
const byDayHits = [];
|
|
1105
|
+
for (const { ord, wd } of tokens) {
|
|
1106
|
+
const list = (_a = buckets[wd]) != null ? _a : [];
|
|
1107
|
+
if (!list.length) continue;
|
|
1108
|
+
if (ord === 0) {
|
|
1109
|
+
for (const d of list) byDayHits.push(d);
|
|
1110
|
+
} else {
|
|
1111
|
+
const idx = ord > 0 ? ord - 1 : list.length + ord;
|
|
1112
|
+
const dayN = list[idx];
|
|
1113
|
+
if (dayN) byDayHits.push(dayN);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
let finalDays = byDayHits;
|
|
1117
|
+
if (byMonthDay && byMonthDay.length > 0) {
|
|
1118
|
+
if (byMonthDayHits.length === 0) {
|
|
1119
|
+
return [];
|
|
1120
|
+
}
|
|
1121
|
+
finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
|
|
1122
|
+
}
|
|
1123
|
+
const hits = finalDays.map((d) => sample.with({ day: d }));
|
|
1124
|
+
let expanded = hits.flatMap((z) => this.expandByTime(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1125
|
+
expanded = this.applyBySetPos(expanded);
|
|
1126
|
+
return expanded;
|
|
1127
|
+
}
|
|
1128
|
+
/**
|
|
1129
|
+
* Given any date in a year, return all ZonedDateTimes in that year matching
|
|
1130
|
+
* the BYDAY/BYMONTHDAY/BYMONTH constraints. Months default to DTSTART's month
|
|
1131
|
+
* if BYMONTH is not specified.
|
|
1132
|
+
*/
|
|
1133
|
+
generateYearlyOccurrences(sample) {
|
|
1134
|
+
var _a;
|
|
1135
|
+
const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
|
|
1136
|
+
let occs = [];
|
|
1137
|
+
if (this.opts.byDay && this.opts.byDay.some((t) => /\d{2}/.test(t))) {
|
|
1138
|
+
const dayMap = {
|
|
1139
|
+
MO: 1,
|
|
1140
|
+
TU: 2,
|
|
1141
|
+
WE: 3,
|
|
1142
|
+
TH: 4,
|
|
1143
|
+
FR: 5,
|
|
1144
|
+
SA: 6,
|
|
1145
|
+
SU: 7
|
|
1146
|
+
};
|
|
1147
|
+
for (const tok of this.opts.byDay) {
|
|
1148
|
+
const m = tok.match(/^([+-]?\d{1,2})(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
1149
|
+
if (!m) continue;
|
|
1150
|
+
const ord = parseInt(m[1], 10);
|
|
1151
|
+
const wd = dayMap[m[2]];
|
|
1152
|
+
let dt;
|
|
1153
|
+
if (ord > 0) {
|
|
1154
|
+
const jan1 = sample.with({ month: 1, day: 1 });
|
|
1155
|
+
const delta = (wd - jan1.dayOfWeek + 7) % 7;
|
|
1156
|
+
dt = jan1.add({ days: delta + 7 * (ord - 1) });
|
|
1157
|
+
} else {
|
|
1158
|
+
const dec31 = sample.with({ month: 12, day: 31 });
|
|
1159
|
+
const delta = (dec31.dayOfWeek - wd + 7) % 7;
|
|
1160
|
+
dt = dec31.subtract({ days: delta + 7 * (-ord - 1) });
|
|
1161
|
+
}
|
|
1162
|
+
if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
|
|
1163
|
+
occs.push(...this.expandByTime(dt));
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
} else if (!this.opts.byYearDay && !this.opts.byWeekNo) {
|
|
1167
|
+
occs = months.flatMap((m) => {
|
|
1168
|
+
const monthSample = sample.with({ month: m, day: 1 });
|
|
1169
|
+
return this.generateMonthlyOccurrences(monthSample);
|
|
1170
|
+
});
|
|
1171
|
+
}
|
|
1172
|
+
if (this.opts.byYearDay) {
|
|
1173
|
+
const last = sample.with({ month: 12, day: 31 }).dayOfYear;
|
|
1174
|
+
for (const d of this.opts.byYearDay) {
|
|
1175
|
+
const dayNum = d > 0 ? d : last + d + 1;
|
|
1176
|
+
const dt = sample.with({ month: 1, day: 1 }).add({ days: dayNum - 1 });
|
|
1177
|
+
if (!this.opts.byMonth || this.opts.byMonth.includes(dt.month)) {
|
|
1178
|
+
occs.push(...this.expandByTime(dt));
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
if (this.opts.byWeekNo) {
|
|
1183
|
+
const dayMap = {
|
|
1184
|
+
MO: 1,
|
|
1185
|
+
TU: 2,
|
|
1186
|
+
WE: 3,
|
|
1187
|
+
TH: 4,
|
|
1188
|
+
FR: 5,
|
|
1189
|
+
SA: 6,
|
|
1190
|
+
SU: 7
|
|
1191
|
+
};
|
|
1192
|
+
const wkst = dayMap[this.opts.wkst || "MO"];
|
|
1193
|
+
const jan1 = sample.with({ month: 1, day: 1 });
|
|
1194
|
+
const delta = (jan1.dayOfWeek - wkst + 7) % 7;
|
|
1195
|
+
const firstWeekStart = jan1.subtract({ days: delta });
|
|
1196
|
+
const lastWeekDiff = sample.with({ month: 12, day: 31 }).toPlainDate().since(firstWeekStart.toPlainDate()).days;
|
|
1197
|
+
const lastWeek = Math.floor(lastWeekDiff / 7) + 1;
|
|
1198
|
+
const tokens = ((_a = this.opts.byDay) == null ? void 0 : _a.length) ? this.opts.byDay.map((tok) => {
|
|
1199
|
+
var _a2;
|
|
1200
|
+
return (_a2 = tok.match(/(MO|TU|WE|TH|FR|SA|SU)$/)) == null ? void 0 : _a2[1];
|
|
1201
|
+
}) : [this.opts.wkst || "MO"];
|
|
1202
|
+
for (const weekNo of this.opts.byWeekNo) {
|
|
1203
|
+
const weekIndex = weekNo > 0 ? weekNo - 1 : lastWeek + weekNo;
|
|
1204
|
+
const weekStart = firstWeekStart.add({ weeks: weekIndex });
|
|
1205
|
+
for (const tok of tokens) {
|
|
1206
|
+
if (!tok) continue;
|
|
1207
|
+
const targetDow = dayMap[tok];
|
|
1208
|
+
const inst = weekStart.add({ days: (targetDow - wkst + 7) % 7 });
|
|
1209
|
+
if (!this.opts.byMonth || this.opts.byMonth.includes(inst.month)) {
|
|
1210
|
+
occs.push(...this.expandByTime(inst));
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
occs = occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1216
|
+
occs = this.applyBySetPos(occs);
|
|
1217
|
+
return occs;
|
|
1218
|
+
}
|
|
1219
|
+
applyBySetPos(list) {
|
|
1220
|
+
const { bySetPos } = this.opts;
|
|
1221
|
+
if (!bySetPos || !bySetPos.length) return list;
|
|
1222
|
+
const sorted = [...list].sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1223
|
+
const out = [];
|
|
1224
|
+
const len = sorted.length;
|
|
1225
|
+
for (const pos of bySetPos) {
|
|
1226
|
+
const idx = pos > 0 ? pos - 1 : len + pos;
|
|
1227
|
+
if (idx >= 0 && idx < len) out.push(sorted[idx]);
|
|
1228
|
+
}
|
|
1229
|
+
return out.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1230
|
+
}
|
|
1231
|
+
mergeRDates(list, startInst, endInst, inc) {
|
|
1232
|
+
if (!this.opts.rDate) return this.excludeExDates(list);
|
|
1233
|
+
for (const z of this.opts.rDate) {
|
|
1234
|
+
const inst = z.toInstant();
|
|
1235
|
+
const startOk = inc ? Temporal.Instant.compare(inst, startInst) >= 0 : Temporal.Instant.compare(inst, startInst) > 0;
|
|
1236
|
+
const endOk = inc ? Temporal.Instant.compare(inst, endInst) <= 0 : Temporal.Instant.compare(inst, endInst) < 0;
|
|
1237
|
+
if (startOk && endOk) list.push(z);
|
|
1238
|
+
}
|
|
1239
|
+
list.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
1240
|
+
const dedup = [];
|
|
1241
|
+
for (const d of list) {
|
|
1242
|
+
if (dedup.length === 0 || Temporal.ZonedDateTime.compare(d, dedup[dedup.length - 1]) !== 0) {
|
|
1243
|
+
dedup.push(d);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
return this.excludeExDates(dedup);
|
|
1247
|
+
}
|
|
1248
|
+
};
|
|
1249
|
+
|
|
1250
|
+
export {
|
|
1251
|
+
RRuleTemporal
|
|
1252
|
+
};
|