rrule-temporal 0.1.3 → 0.1.5
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 +577 -0
- package/dist/index.d.cts +72 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +521 -630
- package/package.json +10 -4
package/dist/index.js
CHANGED
|
@@ -1,661 +1,552 @@
|
|
|
1
|
-
//
|
|
1
|
+
// src/index.ts
|
|
2
2
|
import { Temporal } from "@js-temporal/polyfill";
|
|
3
|
-
/**
|
|
4
|
-
* Parse either a full ICS snippet or an RRULE line into ManualOpts
|
|
5
|
-
*/
|
|
6
3
|
function parseRRuleString(input, fallbackDtstart) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
opts.until = Temporal.Instant.from(iso).toZonedDateTimeISO(tzid || "UTC");
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-` +
|
|
54
|
-
`${val.slice(6, 8)}T${val.slice(9, 15)}`;
|
|
55
|
-
opts.until = Temporal.PlainDateTime.from(iso).toZonedDateTime(tzid || "UTC");
|
|
56
|
-
}
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
case "BYHOUR":
|
|
60
|
-
opts.byHour = val.split(",").map((n) => parseInt(n, 10));
|
|
61
|
-
break;
|
|
62
|
-
case "BYMINUTE":
|
|
63
|
-
opts.byMinute = val.split(",").map((n) => parseInt(n, 10));
|
|
64
|
-
break;
|
|
65
|
-
case "BYDAY":
|
|
66
|
-
opts.byDay = val.split(","); // e.g. ["MO","2FR","-1SU"]
|
|
67
|
-
break;
|
|
68
|
-
case "BYMONTH":
|
|
69
|
-
opts.byMonth = val.split(",").map((n) => parseInt(n, 10));
|
|
70
|
-
break;
|
|
4
|
+
let dtstart;
|
|
5
|
+
let tzid = "";
|
|
6
|
+
let rruleLine;
|
|
7
|
+
if (/^DTSTART/m.test(input)) {
|
|
8
|
+
const [dtLine, rrLine] = input.split(/\r?\n/);
|
|
9
|
+
const m = dtLine?.match(/DTSTART(?:;TZID=([^:]+))?:(\d{8}T\d{6})/);
|
|
10
|
+
if (!m) throw new Error("Invalid DTSTART in ICS snippet");
|
|
11
|
+
tzid = m[1] || tzid;
|
|
12
|
+
const isoDate = `${m[2].slice(0, 4)}-${m[2].slice(4, 6)}-${m[2].slice(6, 8)}T${m[2].slice(9)}`;
|
|
13
|
+
dtstart = Temporal.PlainDateTime.from(isoDate).toZonedDateTime(tzid);
|
|
14
|
+
rruleLine = rrLine;
|
|
15
|
+
} else {
|
|
16
|
+
if (!fallbackDtstart)
|
|
17
|
+
throw new Error("dtstart required when parsing RRULE alone");
|
|
18
|
+
dtstart = fallbackDtstart;
|
|
19
|
+
tzid = fallbackDtstart.timeZoneId;
|
|
20
|
+
rruleLine = input.replace(/^RRULE:/, "RRULE:");
|
|
21
|
+
}
|
|
22
|
+
const parts = rruleLine.replace(/^RRULE:/, "").split(";");
|
|
23
|
+
const opts = { dtstart, tzid };
|
|
24
|
+
for (const part of parts) {
|
|
25
|
+
const [key, val] = part.split("=");
|
|
26
|
+
switch (key) {
|
|
27
|
+
case "FREQ":
|
|
28
|
+
opts.freq = val;
|
|
29
|
+
break;
|
|
30
|
+
case "INTERVAL":
|
|
31
|
+
opts.interval = parseInt(val, 10);
|
|
32
|
+
break;
|
|
33
|
+
case "COUNT":
|
|
34
|
+
opts.count = parseInt(val, 10);
|
|
35
|
+
break;
|
|
36
|
+
case "UNTIL": {
|
|
37
|
+
if (/Z$/.test(val)) {
|
|
38
|
+
const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}Z`;
|
|
39
|
+
opts.until = Temporal.Instant.from(iso).toZonedDateTimeISO(
|
|
40
|
+
tzid || "UTC"
|
|
41
|
+
);
|
|
42
|
+
} else {
|
|
43
|
+
const iso = `${val.slice(0, 4)}-${val.slice(4, 6)}-${val.slice(6, 8)}T${val.slice(9, 15)}`;
|
|
44
|
+
opts.until = Temporal.PlainDateTime.from(iso).toZonedDateTime(
|
|
45
|
+
tzid || "UTC"
|
|
46
|
+
);
|
|
71
47
|
}
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case "BYHOUR":
|
|
51
|
+
opts.byHour = val.split(",").map((n) => parseInt(n, 10));
|
|
52
|
+
break;
|
|
53
|
+
case "BYMINUTE":
|
|
54
|
+
opts.byMinute = val.split(",").map((n) => parseInt(n, 10));
|
|
55
|
+
break;
|
|
56
|
+
case "BYDAY":
|
|
57
|
+
opts.byDay = val.split(",");
|
|
58
|
+
break;
|
|
59
|
+
case "BYMONTH":
|
|
60
|
+
opts.byMonth = val.split(",").map((n) => parseInt(n, 10));
|
|
61
|
+
break;
|
|
72
62
|
}
|
|
73
|
-
|
|
63
|
+
}
|
|
64
|
+
return opts;
|
|
74
65
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.originalDtstart = manual.dtstart;
|
|
94
|
-
}
|
|
95
|
-
if (!manual.freq)
|
|
96
|
-
throw new Error("RRULE must include FREQ");
|
|
97
|
-
manual.interval = manual.interval ?? 1;
|
|
98
|
-
this.opts = manual;
|
|
66
|
+
var RRuleTemporal = class {
|
|
67
|
+
tzid;
|
|
68
|
+
originalDtstart;
|
|
69
|
+
opts;
|
|
70
|
+
constructor(params) {
|
|
71
|
+
let manual;
|
|
72
|
+
if ("rruleString" in params) {
|
|
73
|
+
manual = parseRRuleString(params.rruleString);
|
|
74
|
+
this.tzid = manual.tzid || "UTC";
|
|
75
|
+
this.originalDtstart = manual.dtstart;
|
|
76
|
+
} else {
|
|
77
|
+
manual = { ...params };
|
|
78
|
+
if (typeof manual.dtstart === "string") {
|
|
79
|
+
throw new Error("Manual dtstart must be a ZonedDateTime");
|
|
80
|
+
}
|
|
81
|
+
manual.tzid = manual.tzid || manual.dtstart.timeZoneId;
|
|
82
|
+
this.tzid = manual.tzid;
|
|
83
|
+
this.originalDtstart = manual.dtstart;
|
|
99
84
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
85
|
+
if (!manual.freq) throw new Error("RRULE must include FREQ");
|
|
86
|
+
manual.interval = manual.interval ?? 1;
|
|
87
|
+
this.opts = manual;
|
|
88
|
+
}
|
|
89
|
+
rawAdvance(zdt) {
|
|
90
|
+
const { freq, interval } = this.opts;
|
|
91
|
+
switch (freq) {
|
|
92
|
+
case "DAILY":
|
|
93
|
+
return zdt.add({ days: interval });
|
|
94
|
+
case "WEEKLY":
|
|
95
|
+
return zdt.add({ weeks: interval });
|
|
96
|
+
case "MONTHLY":
|
|
97
|
+
return zdt.add({ months: interval });
|
|
98
|
+
case "YEARLY":
|
|
99
|
+
return zdt.add({ years: interval });
|
|
100
|
+
case "HOURLY":
|
|
101
|
+
return zdt.add({ hours: interval });
|
|
102
|
+
case "MINUTELY":
|
|
103
|
+
return zdt.add({ minutes: interval });
|
|
104
|
+
case "SECONDLY":
|
|
105
|
+
return zdt.add({ seconds: interval });
|
|
106
|
+
default:
|
|
107
|
+
throw new Error(`Unsupported FREQ: ${freq}`);
|
|
120
108
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
109
|
+
}
|
|
110
|
+
applyTimeOverride(zdt) {
|
|
111
|
+
const { byHour, byMinute } = this.opts;
|
|
112
|
+
let dt = zdt;
|
|
113
|
+
if (byHour) dt = dt.with({ hour: byHour[0] });
|
|
114
|
+
if (byMinute) dt = dt.with({ minute: byMinute[0] });
|
|
115
|
+
return dt;
|
|
116
|
+
}
|
|
117
|
+
computeFirst() {
|
|
118
|
+
let zdt = this.originalDtstart;
|
|
119
|
+
if (this.opts.freq === "WEEKLY" && this.opts.byDay?.length) {
|
|
120
|
+
const dayMap = {
|
|
121
|
+
MO: 1,
|
|
122
|
+
TU: 2,
|
|
123
|
+
WE: 3,
|
|
124
|
+
TH: 4,
|
|
125
|
+
FR: 5,
|
|
126
|
+
SA: 6,
|
|
127
|
+
SU: 7
|
|
128
|
+
};
|
|
129
|
+
const targetDow = dayMap[this.opts.byDay[0]];
|
|
130
|
+
const delta = (targetDow - zdt.dayOfWeek + 7) % 7;
|
|
131
|
+
zdt = zdt.add({ days: delta });
|
|
129
132
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
FR: 5,
|
|
140
|
-
SA: 6,
|
|
141
|
-
SU: 7,
|
|
142
|
-
};
|
|
143
|
-
const targetDow = dayMap[this.opts.byDay[0]];
|
|
144
|
-
const delta = (targetDow - zdt.dayOfWeek + 7) % 7;
|
|
145
|
-
zdt = zdt.add({ days: delta });
|
|
146
|
-
}
|
|
147
|
-
// then your existing BYHOUR/BYMINUTE override logic:
|
|
148
|
-
const { byHour, byMinute } = this.opts;
|
|
149
|
-
if (byHour || byMinute) {
|
|
150
|
-
zdt = this.applyTimeOverride(zdt);
|
|
151
|
-
if (Temporal.Instant.compare(zdt.toInstant(), this.originalDtstart.toInstant()) < 0) {
|
|
152
|
-
zdt = this.applyTimeOverride(this.rawAdvance(this.originalDtstart));
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return zdt;
|
|
156
|
-
}
|
|
157
|
-
// inside class RRuleTemporal:
|
|
158
|
-
generateWeeklyOccurrences(sample) {
|
|
159
|
-
const { byDay } = this.opts;
|
|
160
|
-
// Map token → ISO dayOfWeek
|
|
161
|
-
const dayMap = {
|
|
162
|
-
MO: 1,
|
|
163
|
-
TU: 2,
|
|
164
|
-
WE: 3,
|
|
165
|
-
TH: 4,
|
|
166
|
-
FR: 5,
|
|
167
|
-
SA: 6,
|
|
168
|
-
SU: 7,
|
|
169
|
-
};
|
|
170
|
-
// If no BYDAY, default to the sample’s own weekday token
|
|
171
|
-
const tokens = byDay && byDay.length
|
|
172
|
-
? byDay
|
|
173
|
-
: [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
|
|
174
|
-
// For each token, find the date in [sample, sample+6d] matching that weekday
|
|
175
|
-
const occs = tokens.map((tok) => {
|
|
176
|
-
const targetDow = dayMap[tok];
|
|
177
|
-
const delta = (targetDow - sample.dayOfWeek + 7) % 7;
|
|
178
|
-
return this.applyTimeOverride(sample.add({ days: delta }));
|
|
179
|
-
});
|
|
180
|
-
// Return in chronological order
|
|
181
|
-
return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
133
|
+
const { byHour, byMinute } = this.opts;
|
|
134
|
+
if (byHour || byMinute) {
|
|
135
|
+
zdt = this.applyTimeOverride(zdt);
|
|
136
|
+
if (Temporal.Instant.compare(
|
|
137
|
+
zdt.toInstant(),
|
|
138
|
+
this.originalDtstart.toInstant()
|
|
139
|
+
) < 0) {
|
|
140
|
+
zdt = this.applyTimeOverride(this.rawAdvance(this.originalDtstart));
|
|
141
|
+
}
|
|
182
142
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
143
|
+
return zdt;
|
|
144
|
+
}
|
|
145
|
+
// inside class RRuleTemporal:
|
|
146
|
+
generateWeeklyOccurrences(sample) {
|
|
147
|
+
const { byDay } = this.opts;
|
|
148
|
+
const dayMap = {
|
|
149
|
+
MO: 1,
|
|
150
|
+
TU: 2,
|
|
151
|
+
WE: 3,
|
|
152
|
+
TH: 4,
|
|
153
|
+
FR: 5,
|
|
154
|
+
SA: 6,
|
|
155
|
+
SU: 7
|
|
156
|
+
};
|
|
157
|
+
const tokens = byDay && byDay.length ? byDay : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
|
|
158
|
+
const occs = tokens.map((tok) => {
|
|
159
|
+
const targetDow = dayMap[tok];
|
|
160
|
+
const delta = (targetDow - sample.dayOfWeek + 7) % 7;
|
|
161
|
+
return this.applyTimeOverride(sample.add({ days: delta }));
|
|
162
|
+
});
|
|
163
|
+
return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
164
|
+
}
|
|
165
|
+
// --- NEW: constraint checks ---
|
|
166
|
+
// 2) Replace your matchesByDay with this:
|
|
167
|
+
matchesByDay(zdt) {
|
|
168
|
+
const { byDay } = this.opts;
|
|
169
|
+
if (!byDay) return true;
|
|
170
|
+
const dayMap = {
|
|
171
|
+
MO: 1,
|
|
172
|
+
TU: 2,
|
|
173
|
+
WE: 3,
|
|
174
|
+
TH: 4,
|
|
175
|
+
FR: 5,
|
|
176
|
+
SA: 6,
|
|
177
|
+
SU: 7
|
|
178
|
+
};
|
|
179
|
+
for (const token of byDay) {
|
|
180
|
+
const m = token.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
181
|
+
if (!m) continue;
|
|
182
|
+
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
183
|
+
const weekday = m[2];
|
|
184
|
+
if (!weekday) continue;
|
|
185
|
+
const wd = dayMap[weekday];
|
|
186
|
+
if (ord === 0) {
|
|
187
|
+
if (zdt.dayOfWeek === wd) return true;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const year = zdt.year;
|
|
191
|
+
const month = zdt.month;
|
|
192
|
+
let dt = zdt.with({ day: 1 });
|
|
193
|
+
const candidates = [];
|
|
194
|
+
while (dt.month === month) {
|
|
195
|
+
if (dt.dayOfWeek === wd) candidates.push(dt.day);
|
|
196
|
+
dt = dt.add({ days: 1 });
|
|
197
|
+
}
|
|
198
|
+
const idx = ord > 0 ? ord - 1 : candidates.length + ord;
|
|
199
|
+
if (candidates[idx] === zdt.day) return true;
|
|
232
200
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
matchesByMonth(zdt) {
|
|
204
|
+
const { byMonth } = this.opts;
|
|
205
|
+
if (!byMonth) return true;
|
|
206
|
+
return byMonth.includes(zdt.month);
|
|
207
|
+
}
|
|
208
|
+
matchesAll(zdt) {
|
|
209
|
+
return this.matchesByDay(zdt) && this.matchesByMonth(zdt);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Returns all occurrences of the rule.
|
|
213
|
+
* @param iterator - An optional callback iterator function that can be used to filter or modify the occurrences.
|
|
214
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule.
|
|
215
|
+
*/
|
|
216
|
+
all(iterator) {
|
|
217
|
+
if (!this.opts.count && !this.opts.until && !iterator) {
|
|
218
|
+
throw new Error("all() requires iterator when no COUNT/UNTIL");
|
|
238
219
|
}
|
|
239
|
-
|
|
240
|
-
|
|
220
|
+
const dates = [];
|
|
221
|
+
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
222
|
+
const start = this.originalDtstart;
|
|
223
|
+
let monthCursor = start.with({ day: 1 });
|
|
224
|
+
let matchCount2 = 0;
|
|
225
|
+
outer_month: while (true) {
|
|
226
|
+
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
227
|
+
for (const occ of occs) {
|
|
228
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
229
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
230
|
+
break outer_month;
|
|
231
|
+
}
|
|
232
|
+
if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
|
|
233
|
+
break outer_month;
|
|
234
|
+
}
|
|
235
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
236
|
+
break outer_month;
|
|
237
|
+
}
|
|
238
|
+
dates.push(occ);
|
|
239
|
+
matchCount2++;
|
|
240
|
+
}
|
|
241
|
+
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
242
|
+
}
|
|
243
|
+
return dates;
|
|
241
244
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
245
|
+
if (this.opts.freq === "WEEKLY") {
|
|
246
|
+
const start = this.originalDtstart;
|
|
247
|
+
const dayMap = {
|
|
248
|
+
MO: 1,
|
|
249
|
+
TU: 2,
|
|
250
|
+
WE: 3,
|
|
251
|
+
TH: 4,
|
|
252
|
+
FR: 5,
|
|
253
|
+
SA: 6,
|
|
254
|
+
SU: 7
|
|
255
|
+
};
|
|
256
|
+
const tokens = this.opts.byDay ? [...this.opts.byDay] : [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
|
|
257
|
+
const dows = tokens.map((tok) => dayMap[tok]).filter((d) => d !== void 0).sort((a, b) => a - b);
|
|
258
|
+
const firstWeekDates = dows.map((dw) => {
|
|
259
|
+
const delta = (dw - start.dayOfWeek + 7) % 7;
|
|
260
|
+
return start.add({ days: delta });
|
|
261
|
+
});
|
|
262
|
+
let weekCursor = firstWeekDates.reduce(
|
|
263
|
+
(a, b) => Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b
|
|
264
|
+
);
|
|
265
|
+
let matchCount2 = 0;
|
|
266
|
+
outer_week: while (true) {
|
|
267
|
+
const baseDow = weekCursor.dayOfWeek;
|
|
268
|
+
const occs = dows.map((dw) => {
|
|
269
|
+
const delta = dw - baseDow;
|
|
270
|
+
return this.applyTimeOverride(weekCursor.add({ days: delta }));
|
|
271
|
+
}).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
272
|
+
for (const occ of occs) {
|
|
273
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
274
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
275
|
+
break outer_week;
|
|
276
|
+
}
|
|
277
|
+
if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
|
|
278
|
+
break outer_week;
|
|
279
|
+
}
|
|
280
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
281
|
+
break outer_week;
|
|
282
|
+
}
|
|
283
|
+
dates.push(occ);
|
|
284
|
+
matchCount2++;
|
|
250
285
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
break outer_month;
|
|
268
|
-
}
|
|
269
|
-
if (iterator && !iterator(occ, matchCount)) {
|
|
270
|
-
break outer_month;
|
|
271
|
-
}
|
|
272
|
-
dates.push(occ);
|
|
273
|
-
matchCount++;
|
|
274
|
-
}
|
|
275
|
-
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
276
|
-
}
|
|
277
|
-
return dates;
|
|
286
|
+
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
287
|
+
}
|
|
288
|
+
return dates;
|
|
289
|
+
}
|
|
290
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
291
|
+
const start = this.originalDtstart;
|
|
292
|
+
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
293
|
+
let i = 0;
|
|
294
|
+
while (true) {
|
|
295
|
+
const year = start.year + i * this.opts.interval;
|
|
296
|
+
const month = months[i % months.length];
|
|
297
|
+
let occ = start.with({ year, month });
|
|
298
|
+
occ = this.applyTimeOverride(occ);
|
|
299
|
+
if (i === 0 && Temporal.ZonedDateTime.compare(occ, start) < 0) {
|
|
300
|
+
i++;
|
|
301
|
+
continue;
|
|
278
302
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
const start = this.originalDtstart;
|
|
282
|
-
// Build the list of target weekdays (1=Mon..7=Sun)
|
|
283
|
-
const dayMap = {
|
|
284
|
-
MO: 1,
|
|
285
|
-
TU: 2,
|
|
286
|
-
WE: 3,
|
|
287
|
-
TH: 4,
|
|
288
|
-
FR: 5,
|
|
289
|
-
SA: 6,
|
|
290
|
-
SU: 7,
|
|
291
|
-
};
|
|
292
|
-
// If no BYDAY, default to DTSTART’s weekday token
|
|
293
|
-
const tokens = this.opts.byDay
|
|
294
|
-
? [...this.opts.byDay]
|
|
295
|
-
: [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
|
|
296
|
-
const dows = tokens
|
|
297
|
-
.map((tok) => dayMap[tok])
|
|
298
|
-
.filter((d) => d !== undefined)
|
|
299
|
-
.sort((a, b) => a - b);
|
|
300
|
-
// Find the very first weekCursor: the earliest of this week’s matching days ≥ start
|
|
301
|
-
const firstWeekDates = dows.map((dw) => {
|
|
302
|
-
const delta = (dw - start.dayOfWeek + 7) % 7;
|
|
303
|
-
return start.add({ days: delta });
|
|
304
|
-
});
|
|
305
|
-
let weekCursor = firstWeekDates.reduce((a, b) => Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
|
|
306
|
-
let matchCount = 0;
|
|
307
|
-
outer_week: while (true) {
|
|
308
|
-
// Generate this week’s occurrences
|
|
309
|
-
const baseDow = weekCursor.dayOfWeek;
|
|
310
|
-
const occs = dows
|
|
311
|
-
.map((dw) => {
|
|
312
|
-
const delta = dw - baseDow;
|
|
313
|
-
return this.applyTimeOverride(weekCursor.add({ days: delta }));
|
|
314
|
-
})
|
|
315
|
-
.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
316
|
-
for (const occ of occs) {
|
|
317
|
-
if (Temporal.ZonedDateTime.compare(occ, start) < 0)
|
|
318
|
-
continue;
|
|
319
|
-
if (this.opts.until &&
|
|
320
|
-
Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
321
|
-
break outer_week;
|
|
322
|
-
}
|
|
323
|
-
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
324
|
-
break outer_week;
|
|
325
|
-
}
|
|
326
|
-
if (iterator && !iterator(occ, matchCount)) {
|
|
327
|
-
break outer_week;
|
|
328
|
-
}
|
|
329
|
-
dates.push(occ);
|
|
330
|
-
matchCount++;
|
|
331
|
-
}
|
|
332
|
-
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
333
|
-
}
|
|
334
|
-
return dates;
|
|
303
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
304
|
+
break;
|
|
335
305
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
const start = this.originalDtstart;
|
|
339
|
-
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
340
|
-
let i = 0;
|
|
341
|
-
while (true) {
|
|
342
|
-
const year = start.year + i * this.opts.interval;
|
|
343
|
-
const month = months[i % months.length];
|
|
344
|
-
let occ = start.with({ year, month });
|
|
345
|
-
occ = this.applyTimeOverride(occ);
|
|
346
|
-
if (i === 0 && Temporal.ZonedDateTime.compare(occ, start) < 0) {
|
|
347
|
-
i++;
|
|
348
|
-
continue;
|
|
349
|
-
}
|
|
350
|
-
if (this.opts.until &&
|
|
351
|
-
Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
352
|
-
break;
|
|
353
|
-
}
|
|
354
|
-
if (this.opts.count !== undefined && i >= this.opts.count) {
|
|
355
|
-
break;
|
|
356
|
-
}
|
|
357
|
-
if (iterator && !iterator(occ, i)) {
|
|
358
|
-
break;
|
|
359
|
-
}
|
|
360
|
-
dates.push(occ);
|
|
361
|
-
i++;
|
|
362
|
-
}
|
|
363
|
-
return dates;
|
|
306
|
+
if (this.opts.count !== void 0 && i >= this.opts.count) {
|
|
307
|
+
break;
|
|
364
308
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
let matchCount = 0;
|
|
368
|
-
while (true) {
|
|
369
|
-
if (this.opts.until &&
|
|
370
|
-
Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
|
|
371
|
-
break;
|
|
372
|
-
}
|
|
373
|
-
if (this.matchesAll(current)) {
|
|
374
|
-
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
375
|
-
break;
|
|
376
|
-
}
|
|
377
|
-
if (iterator && !iterator(current, matchCount)) {
|
|
378
|
-
break;
|
|
379
|
-
}
|
|
380
|
-
dates.push(current);
|
|
381
|
-
matchCount++;
|
|
382
|
-
}
|
|
383
|
-
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
309
|
+
if (iterator && !iterator(occ, i)) {
|
|
310
|
+
break;
|
|
384
311
|
}
|
|
385
|
-
|
|
312
|
+
dates.push(occ);
|
|
313
|
+
i++;
|
|
314
|
+
}
|
|
315
|
+
return dates;
|
|
386
316
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
? Temporal.Instant.from(after.toISOString())
|
|
397
|
-
: after.toInstant();
|
|
398
|
-
const endInst = before instanceof Date
|
|
399
|
-
? Temporal.Instant.from(before.toISOString())
|
|
400
|
-
: before.toInstant();
|
|
401
|
-
const results = [];
|
|
402
|
-
// 1) MONTHLY + BYDAY
|
|
403
|
-
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
404
|
-
let monthCursor = this.computeFirst().with({ day: 1 });
|
|
405
|
-
outer: while (true) {
|
|
406
|
-
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
407
|
-
for (const occ of occs) {
|
|
408
|
-
const inst = occ.toInstant();
|
|
409
|
-
if (inc
|
|
410
|
-
? Temporal.Instant.compare(inst, endInst) > 0
|
|
411
|
-
: Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
412
|
-
break outer;
|
|
413
|
-
}
|
|
414
|
-
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
415
|
-
results.push(occ);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
419
|
-
}
|
|
420
|
-
return results;
|
|
317
|
+
let current = this.computeFirst();
|
|
318
|
+
let matchCount = 0;
|
|
319
|
+
while (true) {
|
|
320
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(current, this.opts.until) > 0) {
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
if (this.matchesAll(current)) {
|
|
324
|
+
if (this.opts.count !== void 0 && matchCount >= this.opts.count) {
|
|
325
|
+
break;
|
|
421
326
|
}
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
const start = this.originalDtstart;
|
|
425
|
-
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
426
|
-
let i = 0;
|
|
427
|
-
outer: while (true) {
|
|
428
|
-
const year = start.year + i * this.opts.interval;
|
|
429
|
-
const month = months[i % months.length];
|
|
430
|
-
let occ = start.with({ year, month });
|
|
431
|
-
occ = this.applyTimeOverride(occ);
|
|
432
|
-
const inst = occ.toInstant();
|
|
433
|
-
if (inc
|
|
434
|
-
? Temporal.Instant.compare(inst, endInst) > 0
|
|
435
|
-
: Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
436
|
-
break;
|
|
437
|
-
}
|
|
438
|
-
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
439
|
-
results.push(occ);
|
|
440
|
-
}
|
|
441
|
-
i++;
|
|
442
|
-
}
|
|
443
|
-
return results;
|
|
327
|
+
if (iterator && !iterator(current, matchCount)) {
|
|
328
|
+
break;
|
|
444
329
|
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
while (true) {
|
|
476
|
-
const inst = current.toInstant();
|
|
477
|
-
if (inc) {
|
|
478
|
-
if (Temporal.Instant.compare(inst, endInst) > 0)
|
|
479
|
-
break;
|
|
480
|
-
}
|
|
481
|
-
else {
|
|
482
|
-
if (Temporal.Instant.compare(inst, endInst) >= 0)
|
|
483
|
-
break;
|
|
484
|
-
}
|
|
485
|
-
if (Temporal.Instant.compare(inst, startInst) >= 0 &&
|
|
486
|
-
this.matchesAll(current)) {
|
|
487
|
-
results.push(current);
|
|
488
|
-
}
|
|
489
|
-
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
330
|
+
dates.push(current);
|
|
331
|
+
matchCount++;
|
|
332
|
+
}
|
|
333
|
+
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
334
|
+
}
|
|
335
|
+
return dates;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Returns all occurrences of the rule within a specified time window.
|
|
339
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
340
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
341
|
+
* @param inc - Optional boolean flag to include the end date in the results.
|
|
342
|
+
* @returns An array of Temporal.ZonedDateTime objects representing all occurrences of the rule within the specified time window.
|
|
343
|
+
*/
|
|
344
|
+
between(after, before, inc = false) {
|
|
345
|
+
const startInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
346
|
+
const endInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
347
|
+
const results = [];
|
|
348
|
+
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
349
|
+
let monthCursor = this.computeFirst().with({ day: 1 });
|
|
350
|
+
outer: while (true) {
|
|
351
|
+
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
352
|
+
for (const occ of occs) {
|
|
353
|
+
const inst = occ.toInstant();
|
|
354
|
+
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
355
|
+
break outer;
|
|
356
|
+
}
|
|
357
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
358
|
+
results.push(occ);
|
|
359
|
+
}
|
|
490
360
|
}
|
|
491
|
-
|
|
361
|
+
monthCursor = monthCursor.add({ months: this.opts.interval });
|
|
362
|
+
}
|
|
363
|
+
return results;
|
|
492
364
|
}
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
if (this.matchesAll(current)) {
|
|
509
|
-
const ok = inc
|
|
510
|
-
? Temporal.Instant.compare(inst, afterInst) >= 0
|
|
511
|
-
: Temporal.Instant.compare(inst, afterInst) > 0;
|
|
512
|
-
if (ok)
|
|
513
|
-
return current;
|
|
514
|
-
// count it toward COUNT
|
|
515
|
-
matchCount++;
|
|
516
|
-
if (this.opts.count !== undefined && matchCount >= this.opts.count) {
|
|
517
|
-
return null;
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
// stop on UNTIL
|
|
521
|
-
if (this.opts.until &&
|
|
522
|
-
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
|
|
523
|
-
return null;
|
|
524
|
-
}
|
|
525
|
-
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
365
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
366
|
+
const start = this.originalDtstart;
|
|
367
|
+
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
368
|
+
let i = 0;
|
|
369
|
+
outer: while (true) {
|
|
370
|
+
const year = start.year + i * this.opts.interval;
|
|
371
|
+
const month = months[i % months.length];
|
|
372
|
+
let occ = start.with({ year, month });
|
|
373
|
+
occ = this.applyTimeOverride(occ);
|
|
374
|
+
const inst = occ.toInstant();
|
|
375
|
+
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
379
|
+
results.push(occ);
|
|
526
380
|
}
|
|
381
|
+
i++;
|
|
382
|
+
}
|
|
383
|
+
return results;
|
|
527
384
|
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
if (inc
|
|
544
|
-
? Temporal.Instant.compare(inst, beforeInst) > 0
|
|
545
|
-
: Temporal.Instant.compare(inst, beforeInst) >= 0) {
|
|
546
|
-
break;
|
|
547
|
-
}
|
|
548
|
-
// track it if it matches constraints
|
|
549
|
-
if (this.matchesAll(current)) {
|
|
550
|
-
prev = current;
|
|
551
|
-
}
|
|
552
|
-
if (this.opts.until &&
|
|
553
|
-
Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
|
|
554
|
-
break;
|
|
555
|
-
}
|
|
556
|
-
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
385
|
+
if (this.opts.freq === "WEEKLY") {
|
|
386
|
+
const startInst2 = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
387
|
+
const endInst2 = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
388
|
+
let weekCursor = this.computeFirst();
|
|
389
|
+
const results2 = [];
|
|
390
|
+
outer: while (true) {
|
|
391
|
+
const occs = this.generateWeeklyOccurrences(weekCursor);
|
|
392
|
+
for (const occ of occs) {
|
|
393
|
+
const inst = occ.toInstant();
|
|
394
|
+
if (inc ? Temporal.Instant.compare(inst, endInst2) > 0 : Temporal.Instant.compare(inst, endInst2) >= 0) {
|
|
395
|
+
break outer;
|
|
396
|
+
}
|
|
397
|
+
if (Temporal.Instant.compare(inst, startInst2) >= 0) {
|
|
398
|
+
results2.push(occ);
|
|
399
|
+
}
|
|
557
400
|
}
|
|
558
|
-
|
|
401
|
+
weekCursor = weekCursor.add({ weeks: this.opts.interval });
|
|
402
|
+
}
|
|
403
|
+
return results2;
|
|
559
404
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
if (byHour)
|
|
573
|
-
parts.push(`BYHOUR=${byHour.join(",")}`);
|
|
574
|
-
if (byMinute)
|
|
575
|
-
parts.push(`BYMINUTE=${byMinute.join(",")}`);
|
|
576
|
-
if (byDay)
|
|
577
|
-
parts.push(`BYDAY=${byDay.join(",")}`);
|
|
578
|
-
if (byMonth)
|
|
579
|
-
parts.push(`BYMONTH=${byMonth.join(",")}`);
|
|
580
|
-
return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
|
|
405
|
+
let current = this.computeFirst();
|
|
406
|
+
while (true) {
|
|
407
|
+
const inst = current.toInstant();
|
|
408
|
+
if (inc) {
|
|
409
|
+
if (Temporal.Instant.compare(inst, endInst) > 0) break;
|
|
410
|
+
} else {
|
|
411
|
+
if (Temporal.Instant.compare(inst, endInst) >= 0) break;
|
|
412
|
+
}
|
|
413
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0 && this.matchesAll(current)) {
|
|
414
|
+
results.push(current);
|
|
415
|
+
}
|
|
416
|
+
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
581
417
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
418
|
+
return results;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Returns the next occurrence of the rule after a specified date.
|
|
422
|
+
* @param after - The start date or Temporal.ZonedDateTime object.
|
|
423
|
+
* @param inc - Optional boolean flag to include occurrences on the start date.
|
|
424
|
+
* @returns The next occurrence of the rule after the specified date or null if no occurrences are found.
|
|
425
|
+
*/
|
|
426
|
+
next(after = /* @__PURE__ */ new Date(), inc = false) {
|
|
427
|
+
const afterInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
428
|
+
let current = this.computeFirst();
|
|
429
|
+
let matchCount = 0;
|
|
430
|
+
while (true) {
|
|
431
|
+
const inst = current.toInstant();
|
|
432
|
+
if (this.matchesAll(current)) {
|
|
433
|
+
const ok = inc ? Temporal.Instant.compare(inst, afterInst) >= 0 : Temporal.Instant.compare(inst, afterInst) > 0;
|
|
434
|
+
if (ok) return current;
|
|
435
|
+
matchCount++;
|
|
436
|
+
if (this.opts.count !== void 0 && matchCount >= this.opts.count) {
|
|
437
|
+
return null;
|
|
591
438
|
}
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
439
|
+
}
|
|
440
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Returns the previous occurrence of the rule before a specified date.
|
|
448
|
+
* @param before - The end date or Temporal.ZonedDateTime object.
|
|
449
|
+
* @param inc - Optional boolean flag to include occurrences on the end date.
|
|
450
|
+
* @returns The previous occurrence of the rule before the specified date or null if no occurrences are found.
|
|
451
|
+
*/
|
|
452
|
+
previous(before = /* @__PURE__ */ new Date(), inc = false) {
|
|
453
|
+
const beforeInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
454
|
+
let current = this.computeFirst();
|
|
455
|
+
let prev = null;
|
|
456
|
+
while (true) {
|
|
457
|
+
const inst = current.toInstant();
|
|
458
|
+
if (inc ? Temporal.Instant.compare(inst, beforeInst) > 0 : Temporal.Instant.compare(inst, beforeInst) >= 0) {
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
if (this.matchesAll(current)) {
|
|
462
|
+
prev = current;
|
|
463
|
+
}
|
|
464
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(current, this.opts.until) >= 0) {
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
current = this.applyTimeOverride(this.rawAdvance(current));
|
|
468
|
+
}
|
|
469
|
+
return prev;
|
|
470
|
+
}
|
|
471
|
+
toString() {
|
|
472
|
+
const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
|
|
473
|
+
const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
|
|
474
|
+
const parts = [];
|
|
475
|
+
const { freq, interval, count, byHour, byMinute, byDay, byMonth } = this.opts;
|
|
476
|
+
parts.push(`FREQ=${freq}`);
|
|
477
|
+
if (interval !== 1) parts.push(`INTERVAL=${interval}`);
|
|
478
|
+
if (count !== void 0) parts.push(`COUNT=${count}`);
|
|
479
|
+
if (byHour) parts.push(`BYHOUR=${byHour.join(",")}`);
|
|
480
|
+
if (byMinute) parts.push(`BYMINUTE=${byMinute.join(",")}`);
|
|
481
|
+
if (byDay) parts.push(`BYDAY=${byDay.join(",")}`);
|
|
482
|
+
if (byMonth) parts.push(`BYMONTH=${byMonth.join(",")}`);
|
|
483
|
+
return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
487
|
+
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
488
|
+
*/
|
|
489
|
+
generateMonthlyOccurrences(sample) {
|
|
490
|
+
const { byDay, byMonth, tzid } = this.opts;
|
|
491
|
+
if (byMonth && !byMonth.includes(sample.month)) {
|
|
492
|
+
return [];
|
|
493
|
+
}
|
|
494
|
+
if (!byDay || byDay.length === 0) {
|
|
495
|
+
return [sample];
|
|
496
|
+
}
|
|
497
|
+
const dayMap = {
|
|
498
|
+
MO: 1,
|
|
499
|
+
TU: 2,
|
|
500
|
+
WE: 3,
|
|
501
|
+
TH: 4,
|
|
502
|
+
FR: 5,
|
|
503
|
+
SA: 6,
|
|
504
|
+
SU: 7
|
|
505
|
+
};
|
|
506
|
+
const tokens = byDay.map((tok) => {
|
|
507
|
+
const m = tok.match(/^([+-]?\d)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
508
|
+
if (!m) return null;
|
|
509
|
+
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
510
|
+
const wd = dayMap[m[2]];
|
|
511
|
+
return { ord, wd };
|
|
512
|
+
}).filter((x) => x !== null);
|
|
513
|
+
const year = sample.year;
|
|
514
|
+
const month = sample.month;
|
|
515
|
+
let cursor = Temporal.ZonedDateTime.from({
|
|
516
|
+
year,
|
|
517
|
+
month,
|
|
518
|
+
day: 1,
|
|
519
|
+
hour: sample.hour,
|
|
520
|
+
minute: sample.minute,
|
|
521
|
+
second: sample.second,
|
|
522
|
+
timeZone: tzid
|
|
523
|
+
});
|
|
524
|
+
const weekdayBuckets = {};
|
|
525
|
+
while (cursor.month === month) {
|
|
526
|
+
const dow = cursor.dayOfWeek;
|
|
527
|
+
weekdayBuckets[dow] = weekdayBuckets[dow] || [];
|
|
528
|
+
weekdayBuckets[dow].push(cursor.day);
|
|
529
|
+
cursor = cursor.add({ days: 1 });
|
|
530
|
+
}
|
|
531
|
+
const results = [];
|
|
532
|
+
for (const { ord, wd } of tokens) {
|
|
533
|
+
const days = weekdayBuckets[wd] || [];
|
|
534
|
+
if (!days.length) continue;
|
|
535
|
+
if (ord === 0) {
|
|
536
|
+
for (const d of days) {
|
|
537
|
+
results.push(sample.with({ day: d }));
|
|
634
538
|
}
|
|
635
|
-
|
|
636
|
-
const
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
continue;
|
|
641
|
-
if (ord === 0) {
|
|
642
|
-
// simple weekday: include *all* Fridays, e.g.
|
|
643
|
-
for (const d of days) {
|
|
644
|
-
results.push(sample.with({ day: d }));
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
else {
|
|
648
|
-
// ordinal: 2FR → days[1], -1FR → days[days.length-1], etc.
|
|
649
|
-
const idx = ord > 0 ? ord - 1 : days.length + ord;
|
|
650
|
-
const dayNum = days[idx];
|
|
651
|
-
if (dayNum) {
|
|
652
|
-
results.push(sample.with({ day: dayNum }));
|
|
653
|
-
}
|
|
654
|
-
}
|
|
539
|
+
} else {
|
|
540
|
+
const idx = ord > 0 ? ord - 1 : days.length + ord;
|
|
541
|
+
const dayNum = days[idx];
|
|
542
|
+
if (dayNum) {
|
|
543
|
+
results.push(sample.with({ day: dayNum }));
|
|
655
544
|
}
|
|
656
|
-
|
|
657
|
-
return results
|
|
658
|
-
.map((zdt) => this.applyTimeOverride(zdt))
|
|
659
|
-
.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
545
|
+
}
|
|
660
546
|
}
|
|
661
|
-
|
|
547
|
+
return results.map((zdt) => this.applyTimeOverride(zdt)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
export {
|
|
551
|
+
RRuleTemporal
|
|
552
|
+
};
|