rrule-temporal 0.1.7 → 0.2.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 +71 -154
- package/dist/index.cjs +256 -16
- package/dist/index.d.cts +15 -2
- package/dist/index.d.ts +15 -2
- package/dist/index.js +256 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# rrule-temporal
|
|
2
2
|
|
|
3
|
-
Recurrence rule (RFC
|
|
3
|
+
Recurrence rule (RFC 5545) processing built on the Temporal API.
|
|
4
|
+
The library accepts the familiar `RRULE` format and returns
|
|
5
|
+
`Temporal.ZonedDateTime` instances for easy time‑zone aware scheduling.
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
See the [demo site](https://ggaabe.github.io/rrule-temporal/) for an interactive playground.
|
|
6
8
|
|
|
7
|
-
https://github.com/jkbrzt/rrule/issues/
|
|
9
|
+
> This was created to advance the rrule library to use Temporal, and to provide a more modern API, as the original rrule library is [not maintained anymore](https://github.com/jkbrzt/rrule/issues/615). Maintainers suggested to use Temporal instead of Date:
|
|
10
|
+
>https://github.com/jkbrzt/rrule/issues/450#issuecomment-1055853095
|
|
8
11
|
|
|
9
12
|
## Installation
|
|
10
13
|
|
|
@@ -12,192 +15,106 @@ https://github.com/jkbrzt/rrule/issues/450#issuecomment-1055853095
|
|
|
12
15
|
npm install rrule-temporal
|
|
13
16
|
```
|
|
14
17
|
|
|
15
|
-
##
|
|
18
|
+
## Quick start
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
Parse a full DTSTART/RRULE snippet and reproduce it or enumerate all occurrences:
|
|
20
|
-
|
|
21
|
-
```typescript
|
|
22
|
-
// Daily at 17:00 America/Chicago, 5 total occurrences
|
|
23
|
-
const ics = `
|
|
24
|
-
DTSTART;TZID=America/Chicago:20250320T170000
|
|
25
|
-
RRULE:FREQ=DAILY;BYHOUR=17;BYMINUTE=0;COUNT=5
|
|
26
|
-
`.trim();
|
|
27
|
-
|
|
28
|
-
const rule = new RRuleTemporal({ rruleString: ics });
|
|
29
|
-
|
|
30
|
-
// toString() outputs the original DTSTART and RRULE line:
|
|
31
|
-
console.log(rule.toString());
|
|
32
|
-
/*
|
|
33
|
-
DTSTART;TZID=America/Chicago:20250320T170000
|
|
34
|
-
RRULE:FREQ=DAILY;BYHOUR=17;BYMINUTE=0;COUNT=5
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
// all() returns an array of Temporal.ZonedDateTime:
|
|
38
|
-
const dates = rule.all();
|
|
39
|
-
dates.forEach(dt => console.log(dt.toString()));
|
|
40
|
-
// [
|
|
41
|
-
// "2025-03-20T17:00:00[America/Chicago]",
|
|
42
|
-
// "2025-03-21T17:00:00[America/Chicago]",
|
|
43
|
-
// // … total of 5 days
|
|
44
|
-
// ]
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## 2. Enumerate occurrences in a time window
|
|
20
|
+
Parse an ICS snippet and enumerate the occurrences:
|
|
48
21
|
|
|
49
22
|
```typescript
|
|
23
|
+
import { RRuleTemporal } from "rrule-temporal";
|
|
50
24
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const start = new Date(Date.UTC(2025, 3, 2, 0, 0)); // Apr 2 00:00 UTC
|
|
55
|
-
// after:
|
|
56
|
-
const end = new Date(
|
|
57
|
-
// 2025-04-04 00:00 America/Chicago → 05:00 UTC
|
|
58
|
-
Date.UTC(2025, 3, 4, 5, 0, 0)
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
test("between returns occurrences in window inclusive/exclusive", () => {
|
|
62
|
-
const arrExc = rule.between(start, end, false);
|
|
63
|
-
expect(arrExc.map((d) => d.day)).toEqual([2, 3]);
|
|
25
|
+
const rule = new RRuleTemporal({
|
|
26
|
+
rruleString: `DTSTART;TZID=UTC:20250101T090000\nRRULE:FREQ=DAILY;COUNT=3`
|
|
27
|
+
});
|
|
64
28
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
29
|
+
rule.all().forEach(dt => console.log(dt.toString()));
|
|
30
|
+
// 2025-01-01T09:00:00[UTC]
|
|
31
|
+
// 2025-01-02T09:00:00[UTC]
|
|
32
|
+
// 2025-01-03T09:00:00[UTC]
|
|
33
|
+
```
|
|
69
34
|
|
|
70
|
-
##
|
|
35
|
+
## Creating a rule with options
|
|
71
36
|
|
|
72
|
-
|
|
37
|
+
Instead of a full ICS string you can supply the recurrence parameters directly:
|
|
73
38
|
|
|
74
39
|
```typescript
|
|
75
|
-
|
|
76
|
-
const dtstart = Temporal.ZonedDateTime.from({
|
|
77
|
-
year: 2025, month: 4, day: 20,
|
|
78
|
-
hour: 8, minute: 30,
|
|
79
|
-
timeZone: "America/Chicago"
|
|
80
|
-
});
|
|
40
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
81
41
|
|
|
82
42
|
const rule = new RRuleTemporal({
|
|
83
|
-
freq:
|
|
43
|
+
freq: "DAILY",
|
|
84
44
|
interval: 2,
|
|
85
|
-
count:
|
|
86
|
-
byHour:
|
|
45
|
+
count: 3,
|
|
46
|
+
byHour: [9],
|
|
87
47
|
byMinute: [15],
|
|
88
|
-
|
|
89
|
-
|
|
48
|
+
tzid: "America/Chicago",
|
|
49
|
+
dtstart: Temporal.ZonedDateTime.from({
|
|
50
|
+
year: 2025, month: 4, day: 20,
|
|
51
|
+
hour: 8, minute: 30,
|
|
52
|
+
timeZone: "America/Chicago"
|
|
53
|
+
})
|
|
90
54
|
});
|
|
91
55
|
|
|
92
|
-
rule.all().forEach(
|
|
93
|
-
console.log(i, dt.toString());
|
|
94
|
-
});
|
|
95
|
-
// 0 "2025-04-20T09:15:00[America/Chicago]"
|
|
96
|
-
// 1 "2025-04-22T09:15:00[America/Chicago]"
|
|
97
|
-
// 2 "2025-04-24T09:15:00[America/Chicago]"
|
|
56
|
+
rule.all().forEach(dt => console.log(dt.toString()));
|
|
98
57
|
```
|
|
99
58
|
|
|
100
|
-
##
|
|
59
|
+
## Querying occurrences
|
|
101
60
|
|
|
102
|
-
|
|
103
|
-
```typescript
|
|
104
|
-
// Daily at 00:00 CT until 2025-04-05T00:00Z inclusive
|
|
105
|
-
const ics2 = `
|
|
106
|
-
DTSTART;TZID=America/Chicago:20250401T000000
|
|
107
|
-
RRULE:FREQ=DAILY;BYHOUR=0;BYMINUTE=0;UNTIL=20250405T000000Z
|
|
108
|
-
`.trim();
|
|
109
|
-
|
|
110
|
-
const rule2 = new RRuleTemporal({ rruleString: ics2 });
|
|
61
|
+
Use the provided methods to enumerate or search for occurrences:
|
|
111
62
|
|
|
112
|
-
|
|
63
|
+
```typescript
|
|
64
|
+
// Get all events within a window
|
|
113
65
|
const start = new Date(Date.UTC(2025, 3, 2, 0, 0));
|
|
114
|
-
const end
|
|
66
|
+
const end = new Date(Date.UTC(2025, 3, 4, 5, 0));
|
|
67
|
+
const hits = rule.between(start, end, true);
|
|
115
68
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
// "2025-04-03T00:00:00[America/Chicago]"
|
|
120
|
-
// ]
|
|
121
|
-
|
|
122
|
-
console.log(rule2.between(start, end, true).map(d => d.toString()));
|
|
123
|
-
// adds "2025-04-04T00:00:00[America/Chicago]"
|
|
69
|
+
// Next and previous occurrences
|
|
70
|
+
const next = rule.next();
|
|
71
|
+
const prev = rule.previous(new Date("2025-05-01T00:00Z"));
|
|
124
72
|
```
|
|
125
73
|
|
|
126
|
-
##
|
|
127
|
-
|
|
128
|
-
Find the next or previous occurrence relative to any date:
|
|
74
|
+
## Converting back to text
|
|
129
75
|
|
|
130
76
|
```typescript
|
|
131
|
-
//
|
|
132
|
-
|
|
133
|
-
DTSTART;TZID=America/Chicago:20250101T120000
|
|
134
|
-
RRULE:FREQ=MONTHLY;BYHOUR=12;BYMINUTE=0;COUNT=12
|
|
135
|
-
`.trim();
|
|
136
|
-
|
|
137
|
-
const rule3 = new RRuleTemporal({ rruleString: ics3 });
|
|
138
|
-
|
|
139
|
-
// Next occurrence after the current time in Central Time
|
|
140
|
-
// Where hour and minute conditions are met
|
|
141
|
-
const firstOccurrence = rule.next(
|
|
142
|
-
Temporal.Now.zonedDateTimeISO("America/Chicago")
|
|
143
|
-
);
|
|
144
|
-
|
|
145
|
-
// Next after 2025-03-15T00:00:00Z
|
|
146
|
-
const nxt = rule3.next(new Date("2025-03-15T00:00:00Z"));
|
|
147
|
-
console.log(nxt?.toString()); // "2025-04-01T12:00:00[UTC]"
|
|
148
|
-
|
|
149
|
-
// Previous on or before 2025-06-05T00:00:00Z
|
|
150
|
-
const prev = rule3.previous(new Date("2025-06-05T00:00:00Z"), true);
|
|
151
|
-
console.log(prev?.toString()); // "2025-06-01T12:00:00[UTC]"
|
|
77
|
+
rule.toString(); // DTSTART and RRULE lines
|
|
78
|
+
rule.toText(); // human readable description
|
|
152
79
|
```
|
|
153
80
|
|
|
154
|
-
##
|
|
81
|
+
## API
|
|
155
82
|
|
|
156
|
-
|
|
83
|
+
| Method | Description |
|
|
84
|
+
| ------ | ----------- |
|
|
85
|
+
| `new RRuleTemporal(opts)` | Create a rule from an ICS snippet or manual options. |
|
|
86
|
+
| `all(iterator?)` | Return every occurrence. When the rule has no end the optional iterator is required. |
|
|
87
|
+
| `between(after, before, inclusive?)` | Occurrences within a time range. |
|
|
88
|
+
| `next(after?, inclusive?)` | Next occurrence after a given date. |
|
|
89
|
+
| `previous(before?, inclusive?)` | Previous occurrence before a date. |
|
|
90
|
+
| `toString()` | Convert the rule back into `DTSTART` and `RRULE` lines. |
|
|
91
|
+
| `toText(formatter?)` | English description of the rule. |
|
|
92
|
+
| `options()` | Return the normalized options object. |
|
|
157
93
|
|
|
158
|
-
|
|
159
|
-
console.log(rule.toString());
|
|
160
|
-
// DTSTART;TZID=America/Chicago:20250320T170000
|
|
161
|
-
// RRULE:FREQ=DAILY;BYHOUR=17;BYMINUTE=0;COUNT=5
|
|
162
|
-
```
|
|
94
|
+
## Further examples
|
|
163
95
|
|
|
164
|
-
|
|
96
|
+
Enumerating weekdays within a month or rotating through months can be achieved
|
|
97
|
+
with the more advanced RFC 5545 fields:
|
|
165
98
|
|
|
166
99
|
```typescript
|
|
167
|
-
// 2nd & 4th Fridays each month at
|
|
168
|
-
const
|
|
169
|
-
DTSTART;TZID=America/Chicago:20250325T000000
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
// [
|
|
176
|
-
// "2025-04-11T00:00:00[America/Chicago]",
|
|
177
|
-
// "2025-04-25T00:00:00[America/Chicago]",
|
|
178
|
-
// "2025-05-09T00:00:00[America/Chicago]",
|
|
179
|
-
// …
|
|
180
|
-
]
|
|
181
|
-
|
|
182
|
-
// Yearly rotated through Jan, Jun, Dec at 09:00 UTC, 4 occurrences
|
|
183
|
-
const dtstart5 = Temporal.ZonedDateTime.from({
|
|
100
|
+
// 2nd & 4th Fridays each month at midnight CT, first 6 occurrences
|
|
101
|
+
const ruleA = new RRuleTemporal({
|
|
102
|
+
rruleString: `DTSTART;TZID=America/Chicago:20250325T000000\nRRULE:FREQ=MONTHLY;BYDAY=2FR,4FR;BYHOUR=0;BYMINUTE=0;COUNT=6`
|
|
103
|
+
});
|
|
104
|
+
ruleA.all().forEach(dt => console.log(dt.toString()));
|
|
105
|
+
|
|
106
|
+
// Rotate yearly through Jan, Jun and Dec at 09:00 UTC
|
|
107
|
+
const dtstart = Temporal.ZonedDateTime.from({
|
|
184
108
|
year: 2025, month: 1, day: 10, hour: 9, minute: 0, timeZone: "UTC"
|
|
185
109
|
});
|
|
186
|
-
const
|
|
187
|
-
freq:
|
|
110
|
+
const ruleB = new RRuleTemporal({
|
|
111
|
+
freq: "YEARLY",
|
|
188
112
|
interval: 1,
|
|
189
|
-
count:
|
|
190
|
-
byMonth:
|
|
191
|
-
byHour:
|
|
113
|
+
count: 4,
|
|
114
|
+
byMonth: [1, 6, 12],
|
|
115
|
+
byHour: [9],
|
|
192
116
|
byMinute: [0],
|
|
193
|
-
dtstart
|
|
117
|
+
dtstart
|
|
194
118
|
});
|
|
195
|
-
|
|
196
|
-
// [
|
|
197
|
-
// "2025-01-10T09:00:00[UTC]",
|
|
198
|
-
// "2026-06-10T09:00:00[UTC]",
|
|
199
|
-
// "2027-12-10T09:00:00[UTC]",
|
|
200
|
-
// "2028-01-10T09:00:00[UTC]"
|
|
201
|
-
// ]
|
|
119
|
+
ruleB.all().forEach(dt => console.log(dt.toString()));
|
|
202
120
|
```
|
|
203
|
-
|
package/dist/index.cjs
CHANGED
|
@@ -24,6 +24,60 @@ __export(index_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(index_exports);
|
|
26
26
|
var import_polyfill = require("@js-temporal/polyfill");
|
|
27
|
+
var WEEKDAY_NAMES = [
|
|
28
|
+
"Monday",
|
|
29
|
+
"Tuesday",
|
|
30
|
+
"Wednesday",
|
|
31
|
+
"Thursday",
|
|
32
|
+
"Friday",
|
|
33
|
+
"Saturday",
|
|
34
|
+
"Sunday"
|
|
35
|
+
];
|
|
36
|
+
var MONTH_NAMES = [
|
|
37
|
+
"January",
|
|
38
|
+
"February",
|
|
39
|
+
"March",
|
|
40
|
+
"April",
|
|
41
|
+
"May",
|
|
42
|
+
"June",
|
|
43
|
+
"July",
|
|
44
|
+
"August",
|
|
45
|
+
"September",
|
|
46
|
+
"October",
|
|
47
|
+
"November",
|
|
48
|
+
"December"
|
|
49
|
+
];
|
|
50
|
+
var defaultDateFormatter = (year, month, day) => `${month} ${day}, ${year}`;
|
|
51
|
+
function ordinal(n) {
|
|
52
|
+
const abs = Math.abs(n);
|
|
53
|
+
const suffix = abs % 10 === 1 && abs % 100 !== 11 ? "st" : abs % 10 === 2 && abs % 100 !== 12 ? "nd" : abs % 10 === 3 && abs % 100 !== 13 ? "rd" : "th";
|
|
54
|
+
return n < 0 ? `last` : `${abs}${suffix}`;
|
|
55
|
+
}
|
|
56
|
+
function list(arr, mapFn = (x) => `${x}`, final = "and") {
|
|
57
|
+
const mapped = arr.map(mapFn);
|
|
58
|
+
if (mapped.length === 1) return mapped[0];
|
|
59
|
+
return mapped.slice(0, -1).join(", ") + ` ${final} ` + mapped[mapped.length - 1];
|
|
60
|
+
}
|
|
61
|
+
function formatByDayToken(tok) {
|
|
62
|
+
if (typeof tok === "number") return tok.toString();
|
|
63
|
+
const m = tok.match(/^([+-]?\d+)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
64
|
+
if (!m) return tok;
|
|
65
|
+
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
66
|
+
const weekdayMap = {
|
|
67
|
+
MO: 0,
|
|
68
|
+
TU: 1,
|
|
69
|
+
WE: 2,
|
|
70
|
+
TH: 3,
|
|
71
|
+
FR: 4,
|
|
72
|
+
SA: 5,
|
|
73
|
+
SU: 6
|
|
74
|
+
};
|
|
75
|
+
const idx = weekdayMap[m[2]];
|
|
76
|
+
const name = WEEKDAY_NAMES[idx];
|
|
77
|
+
if (ord === 0) return name;
|
|
78
|
+
if (ord === -1) return `last ${name}`;
|
|
79
|
+
return `${ordinal(ord)} ${name}`;
|
|
80
|
+
}
|
|
27
81
|
function parseRRuleString(input, fallbackDtstart) {
|
|
28
82
|
let dtstart;
|
|
29
83
|
let tzid = "UTC";
|
|
@@ -83,10 +137,30 @@ function parseRRuleString(input, fallbackDtstart) {
|
|
|
83
137
|
case "BYMONTH":
|
|
84
138
|
opts.byMonth = val.split(",").map((n) => parseInt(n, 10));
|
|
85
139
|
break;
|
|
140
|
+
case "BYMONTHDAY":
|
|
141
|
+
opts.byMonthDay = val.split(",").map((n) => parseInt(n, 10));
|
|
142
|
+
break;
|
|
86
143
|
}
|
|
87
144
|
}
|
|
88
145
|
return opts;
|
|
89
146
|
}
|
|
147
|
+
function formatTime(hour, minute = 0) {
|
|
148
|
+
const hr12 = (hour + 11) % 12 + 1;
|
|
149
|
+
const ampm = hour < 12 ? "AM" : "PM";
|
|
150
|
+
if (minute) {
|
|
151
|
+
return `${hr12}:${String(minute).padStart(2, "0")} ${ampm}`;
|
|
152
|
+
}
|
|
153
|
+
return `${hr12} ${ampm}`;
|
|
154
|
+
}
|
|
155
|
+
function tzAbbreviation(zdt) {
|
|
156
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
157
|
+
timeZone: zdt.timeZoneId,
|
|
158
|
+
timeZoneName: "short",
|
|
159
|
+
hour: "numeric"
|
|
160
|
+
}).formatToParts(new Date(zdt.epochMilliseconds));
|
|
161
|
+
const tzPart = parts.find((p) => p.type === "timeZoneName");
|
|
162
|
+
return tzPart?.value || zdt.timeZoneId;
|
|
163
|
+
}
|
|
90
164
|
var RRuleTemporal = class {
|
|
91
165
|
tzid;
|
|
92
166
|
originalDtstart;
|
|
@@ -239,7 +313,6 @@ var RRuleTemporal = class {
|
|
|
239
313
|
if (zdt.dayOfWeek === wd) return true;
|
|
240
314
|
continue;
|
|
241
315
|
}
|
|
242
|
-
const year = zdt.year;
|
|
243
316
|
const month = zdt.month;
|
|
244
317
|
let dt = zdt.with({ day: 1 });
|
|
245
318
|
const candidates = [];
|
|
@@ -257,8 +330,16 @@ var RRuleTemporal = class {
|
|
|
257
330
|
if (!byMonth) return true;
|
|
258
331
|
return byMonth.includes(zdt.month);
|
|
259
332
|
}
|
|
333
|
+
matchesByMonthDay(zdt) {
|
|
334
|
+
const { byMonthDay } = this.opts;
|
|
335
|
+
if (!byMonthDay) return true;
|
|
336
|
+
const lastDay = zdt.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
337
|
+
return byMonthDay.some(
|
|
338
|
+
(d) => d > 0 ? zdt.day === d : zdt.day === lastDay + d + 1
|
|
339
|
+
);
|
|
340
|
+
}
|
|
260
341
|
matchesAll(zdt) {
|
|
261
|
-
return this.matchesByDay(zdt) && this.matchesByMonth(zdt);
|
|
342
|
+
return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt);
|
|
262
343
|
}
|
|
263
344
|
options() {
|
|
264
345
|
return this.opts;
|
|
@@ -273,7 +354,7 @@ var RRuleTemporal = class {
|
|
|
273
354
|
throw new Error("all() requires iterator when no COUNT/UNTIL");
|
|
274
355
|
}
|
|
275
356
|
const dates = [];
|
|
276
|
-
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
357
|
+
if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
277
358
|
const start = this.originalDtstart;
|
|
278
359
|
let monthCursor = start.with({ day: 1 });
|
|
279
360
|
let matchCount2 = 0;
|
|
@@ -347,7 +428,7 @@ var RRuleTemporal = class {
|
|
|
347
428
|
}
|
|
348
429
|
return dates;
|
|
349
430
|
}
|
|
350
|
-
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
431
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
|
|
351
432
|
const start = this.originalDtstart;
|
|
352
433
|
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
353
434
|
let i = 0;
|
|
@@ -374,6 +455,30 @@ var RRuleTemporal = class {
|
|
|
374
455
|
}
|
|
375
456
|
return dates;
|
|
376
457
|
}
|
|
458
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
459
|
+
const start = this.originalDtstart;
|
|
460
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
461
|
+
let matchCount2 = 0;
|
|
462
|
+
outer_year: while (true) {
|
|
463
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
464
|
+
for (const occ of occs) {
|
|
465
|
+
if (import_polyfill.Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
466
|
+
if (this.opts.until && import_polyfill.Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
467
|
+
break outer_year;
|
|
468
|
+
}
|
|
469
|
+
if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
|
|
470
|
+
break outer_year;
|
|
471
|
+
}
|
|
472
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
473
|
+
break outer_year;
|
|
474
|
+
}
|
|
475
|
+
dates.push(occ);
|
|
476
|
+
matchCount2++;
|
|
477
|
+
}
|
|
478
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
479
|
+
}
|
|
480
|
+
return dates;
|
|
481
|
+
}
|
|
377
482
|
let current = this.computeFirst();
|
|
378
483
|
let matchCount = 0;
|
|
379
484
|
while (true) {
|
|
@@ -405,7 +510,7 @@ var RRuleTemporal = class {
|
|
|
405
510
|
const startInst = after instanceof Date ? import_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
406
511
|
const endInst = before instanceof Date ? import_polyfill.Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
407
512
|
const results = [];
|
|
408
|
-
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
513
|
+
if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
409
514
|
let monthCursor = this.computeFirst().with({ day: 1 });
|
|
410
515
|
outer: while (true) {
|
|
411
516
|
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
@@ -422,7 +527,7 @@ var RRuleTemporal = class {
|
|
|
422
527
|
}
|
|
423
528
|
return results;
|
|
424
529
|
}
|
|
425
|
-
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
530
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
|
|
426
531
|
const start = this.originalDtstart;
|
|
427
532
|
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
428
533
|
let i = 0;
|
|
@@ -442,6 +547,24 @@ var RRuleTemporal = class {
|
|
|
442
547
|
}
|
|
443
548
|
return results;
|
|
444
549
|
}
|
|
550
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
551
|
+
const start = this.originalDtstart;
|
|
552
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
553
|
+
outer_year: while (true) {
|
|
554
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
555
|
+
for (const occ of occs) {
|
|
556
|
+
const inst = occ.toInstant();
|
|
557
|
+
if (inc ? import_polyfill.Temporal.Instant.compare(inst, endInst) > 0 : import_polyfill.Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
558
|
+
break outer_year;
|
|
559
|
+
}
|
|
560
|
+
if (import_polyfill.Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
561
|
+
results.push(occ);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
565
|
+
}
|
|
566
|
+
return results;
|
|
567
|
+
}
|
|
445
568
|
if (this.opts.freq === "WEEKLY") {
|
|
446
569
|
const startInst2 = after instanceof Date ? import_polyfill.Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
447
570
|
const endInst2 = before instanceof Date ? import_polyfill.Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
@@ -532,24 +655,123 @@ var RRuleTemporal = class {
|
|
|
532
655
|
const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
|
|
533
656
|
const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
|
|
534
657
|
const parts = [];
|
|
535
|
-
const {
|
|
658
|
+
const {
|
|
659
|
+
freq,
|
|
660
|
+
interval,
|
|
661
|
+
count,
|
|
662
|
+
until,
|
|
663
|
+
byHour,
|
|
664
|
+
byMinute,
|
|
665
|
+
byDay,
|
|
666
|
+
byMonth,
|
|
667
|
+
byMonthDay
|
|
668
|
+
} = this.opts;
|
|
536
669
|
parts.push(`FREQ=${freq}`);
|
|
537
670
|
if (interval !== 1) parts.push(`INTERVAL=${interval}`);
|
|
538
671
|
if (count !== void 0) parts.push(`COUNT=${count}`);
|
|
672
|
+
if (until) {
|
|
673
|
+
const u = until.toInstant().toString().replace(/[-:]/g, "");
|
|
674
|
+
parts.push(`UNTIL=${u.slice(0, 15)}Z`);
|
|
675
|
+
}
|
|
539
676
|
if (byHour) parts.push(`BYHOUR=${byHour.join(",")}`);
|
|
540
677
|
if (byMinute) parts.push(`BYMINUTE=${byMinute.join(",")}`);
|
|
541
678
|
if (byDay) parts.push(`BYDAY=${byDay.join(",")}`);
|
|
542
679
|
if (byMonth) parts.push(`BYMONTH=${byMonth.join(",")}`);
|
|
680
|
+
if (byMonthDay) parts.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
|
|
543
681
|
return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
|
|
544
682
|
}
|
|
683
|
+
/**
|
|
684
|
+
* Convert this rule to a human readable English description.
|
|
685
|
+
*/
|
|
686
|
+
toText(formatter = defaultDateFormatter) {
|
|
687
|
+
const {
|
|
688
|
+
freq,
|
|
689
|
+
interval = 1,
|
|
690
|
+
count,
|
|
691
|
+
until,
|
|
692
|
+
byDay,
|
|
693
|
+
byHour,
|
|
694
|
+
byMinute,
|
|
695
|
+
byMonth,
|
|
696
|
+
byMonthDay
|
|
697
|
+
} = this.opts;
|
|
698
|
+
const parts = ["every"];
|
|
699
|
+
const base = {
|
|
700
|
+
YEARLY: "year",
|
|
701
|
+
MONTHLY: "month",
|
|
702
|
+
WEEKLY: "week",
|
|
703
|
+
DAILY: "day",
|
|
704
|
+
HOURLY: "hour",
|
|
705
|
+
MINUTELY: "minute",
|
|
706
|
+
SECONDLY: "second"
|
|
707
|
+
}[freq];
|
|
708
|
+
const daysNormalized = byDay?.map((d) => d.toUpperCase());
|
|
709
|
+
const isWeekdays = daysNormalized && daysNormalized.length === 5 && ["MO", "TU", "WE", "TH", "FR"].every((d) => daysNormalized.includes(d));
|
|
710
|
+
const isEveryday = daysNormalized && daysNormalized.length === 7 && ["MO", "TU", "WE", "TH", "FR", "SA", "SU"].every(
|
|
711
|
+
(d) => daysNormalized.includes(d)
|
|
712
|
+
);
|
|
713
|
+
if (freq === "WEEKLY" && interval === 1 && isWeekdays) {
|
|
714
|
+
parts.push("weekday");
|
|
715
|
+
} else if (freq === "WEEKLY" && interval === 1 && isEveryday) {
|
|
716
|
+
parts.push("day");
|
|
717
|
+
} else {
|
|
718
|
+
if (interval !== 1) {
|
|
719
|
+
parts.push(interval.toString(), base + "s");
|
|
720
|
+
} else {
|
|
721
|
+
parts.push(base);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
if (freq === "WEEKLY" && byDay && !isWeekdays && !isEveryday) {
|
|
725
|
+
parts.push("on", list(byDay, formatByDayToken));
|
|
726
|
+
} else if (byDay && freq !== "WEEKLY") {
|
|
727
|
+
parts.push("on", list(byDay, formatByDayToken));
|
|
728
|
+
}
|
|
729
|
+
if (byMonth) {
|
|
730
|
+
parts.push(
|
|
731
|
+
"in",
|
|
732
|
+
list(byMonth, (m) => MONTH_NAMES[m - 1])
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
if (byMonthDay) {
|
|
736
|
+
parts.push(
|
|
737
|
+
"on the",
|
|
738
|
+
list(byMonthDay, (d) => ordinal(d)),
|
|
739
|
+
"day of the month"
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
if (byHour) {
|
|
743
|
+
const minutes = byMinute ?? [0];
|
|
744
|
+
const times = byHour.flatMap((h) => minutes.map((m) => formatTime(h, m)));
|
|
745
|
+
parts.push("at", list(times));
|
|
746
|
+
parts.push(tzAbbreviation(this.originalDtstart));
|
|
747
|
+
}
|
|
748
|
+
if (until) {
|
|
749
|
+
parts.push(
|
|
750
|
+
"until",
|
|
751
|
+
formatter(until.year, MONTH_NAMES[until.month - 1], until.day)
|
|
752
|
+
);
|
|
753
|
+
} else if (count !== void 0) {
|
|
754
|
+
parts.push("for", count.toString(), count === 1 ? "time" : "times");
|
|
755
|
+
}
|
|
756
|
+
return parts.join(" ");
|
|
757
|
+
}
|
|
545
758
|
/**
|
|
546
759
|
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
547
760
|
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
548
761
|
*/
|
|
549
762
|
generateMonthlyOccurrences(sample) {
|
|
550
|
-
const { byDay, byMonth,
|
|
763
|
+
const { byDay, byMonth, byMonthDay } = this.opts;
|
|
551
764
|
if (byMonth && !byMonth.includes(sample.month)) return [];
|
|
552
|
-
|
|
765
|
+
const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
766
|
+
let byMonthDayHits = [];
|
|
767
|
+
if (byMonthDay && byMonthDay.length > 0) {
|
|
768
|
+
byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
|
|
769
|
+
}
|
|
770
|
+
if (!byDay && byMonthDayHits.length) {
|
|
771
|
+
const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
|
|
772
|
+
return dates.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
|
|
773
|
+
}
|
|
774
|
+
if (!byDay) {
|
|
553
775
|
return this.expandByHourMinute(sample);
|
|
554
776
|
}
|
|
555
777
|
const dayMap = {
|
|
@@ -573,20 +795,38 @@ var RRuleTemporal = class {
|
|
|
573
795
|
(buckets[dow] ||= []).push(cursor.day);
|
|
574
796
|
cursor = cursor.add({ days: 1 });
|
|
575
797
|
}
|
|
576
|
-
const
|
|
798
|
+
const byDayHits = [];
|
|
577
799
|
for (const { ord, wd } of tokens) {
|
|
578
|
-
const
|
|
579
|
-
if (!
|
|
800
|
+
const list2 = buckets[wd] ?? [];
|
|
801
|
+
if (!list2.length) continue;
|
|
580
802
|
if (ord === 0) {
|
|
581
|
-
for (const d of
|
|
803
|
+
for (const d of list2) byDayHits.push(d);
|
|
582
804
|
} else {
|
|
583
|
-
const idx = ord > 0 ? ord - 1 :
|
|
584
|
-
const dayN =
|
|
585
|
-
if (dayN)
|
|
805
|
+
const idx = ord > 0 ? ord - 1 : list2.length + ord;
|
|
806
|
+
const dayN = list2[idx];
|
|
807
|
+
if (dayN) byDayHits.push(dayN);
|
|
586
808
|
}
|
|
587
809
|
}
|
|
810
|
+
let finalDays = byDayHits;
|
|
811
|
+
if (byMonthDayHits.length) {
|
|
812
|
+
finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
|
|
813
|
+
}
|
|
814
|
+
const hits = finalDays.map((d) => sample.with({ day: d }));
|
|
588
815
|
return hits.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
|
|
589
816
|
}
|
|
817
|
+
/**
|
|
818
|
+
* Given any date in a year, return all ZonedDateTimes in that year matching
|
|
819
|
+
* the BYDAY/BYMONTHDAY/BYMONTH constraints. Months default to DTSTART's month
|
|
820
|
+
* if BYMONTH is not specified.
|
|
821
|
+
*/
|
|
822
|
+
generateYearlyOccurrences(sample) {
|
|
823
|
+
const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
|
|
824
|
+
const occs = months.flatMap((m) => {
|
|
825
|
+
const monthSample = sample.with({ month: m, day: 1 });
|
|
826
|
+
return this.generateMonthlyOccurrences(monthSample);
|
|
827
|
+
});
|
|
828
|
+
return occs.sort((a, b) => import_polyfill.Temporal.ZonedDateTime.compare(a, b));
|
|
829
|
+
}
|
|
590
830
|
};
|
|
591
831
|
// Annotate the CommonJS export names for ESM import in node:
|
|
592
832
|
0 && (module.exports = {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
2
|
|
|
3
|
+
type DateFormatter = (year: number, month: string, day: number) => string;
|
|
3
4
|
type Freq = "YEARLY" | "MONTHLY" | "WEEKLY" | "DAILY" | "HOURLY" | "MINUTELY" | "SECONDLY";
|
|
4
5
|
interface BaseOpts {
|
|
5
6
|
tzid?: string;
|
|
@@ -13,12 +14,13 @@ interface ManualOpts extends BaseOpts {
|
|
|
13
14
|
byMinute?: number[];
|
|
14
15
|
byDay?: string[];
|
|
15
16
|
byMonth?: number[];
|
|
17
|
+
byMonthDay?: number[];
|
|
16
18
|
dtstart: Temporal.ZonedDateTime;
|
|
17
19
|
}
|
|
18
20
|
interface IcsOpts extends BaseOpts {
|
|
19
21
|
rruleString: string;
|
|
20
22
|
}
|
|
21
|
-
type
|
|
23
|
+
type RRuleOptions = ManualOpts | IcsOpts;
|
|
22
24
|
declare class RRuleTemporal {
|
|
23
25
|
private tzid;
|
|
24
26
|
private originalDtstart;
|
|
@@ -38,6 +40,7 @@ declare class RRuleTemporal {
|
|
|
38
40
|
private generateWeeklyOccurrences;
|
|
39
41
|
private matchesByDay;
|
|
40
42
|
private matchesByMonth;
|
|
43
|
+
private matchesByMonthDay;
|
|
41
44
|
private matchesAll;
|
|
42
45
|
options(): ManualOpts;
|
|
43
46
|
/**
|
|
@@ -69,11 +72,21 @@ declare class RRuleTemporal {
|
|
|
69
72
|
*/
|
|
70
73
|
previous(before?: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime | null;
|
|
71
74
|
toString(): string;
|
|
75
|
+
/**
|
|
76
|
+
* Convert this rule to a human readable English description.
|
|
77
|
+
*/
|
|
78
|
+
toText(formatter?: DateFormatter): string;
|
|
72
79
|
/**
|
|
73
80
|
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
74
81
|
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
75
82
|
*/
|
|
76
83
|
private generateMonthlyOccurrences;
|
|
84
|
+
/**
|
|
85
|
+
* Given any date in a year, return all ZonedDateTimes in that year matching
|
|
86
|
+
* the BYDAY/BYMONTHDAY/BYMONTH constraints. Months default to DTSTART's month
|
|
87
|
+
* if BYMONTH is not specified.
|
|
88
|
+
*/
|
|
89
|
+
private generateYearlyOccurrences;
|
|
77
90
|
}
|
|
78
91
|
|
|
79
|
-
export { type
|
|
92
|
+
export { type DateFormatter, type RRuleOptions, RRuleTemporal };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
2
|
|
|
3
|
+
type DateFormatter = (year: number, month: string, day: number) => string;
|
|
3
4
|
type Freq = "YEARLY" | "MONTHLY" | "WEEKLY" | "DAILY" | "HOURLY" | "MINUTELY" | "SECONDLY";
|
|
4
5
|
interface BaseOpts {
|
|
5
6
|
tzid?: string;
|
|
@@ -13,12 +14,13 @@ interface ManualOpts extends BaseOpts {
|
|
|
13
14
|
byMinute?: number[];
|
|
14
15
|
byDay?: string[];
|
|
15
16
|
byMonth?: number[];
|
|
17
|
+
byMonthDay?: number[];
|
|
16
18
|
dtstart: Temporal.ZonedDateTime;
|
|
17
19
|
}
|
|
18
20
|
interface IcsOpts extends BaseOpts {
|
|
19
21
|
rruleString: string;
|
|
20
22
|
}
|
|
21
|
-
type
|
|
23
|
+
type RRuleOptions = ManualOpts | IcsOpts;
|
|
22
24
|
declare class RRuleTemporal {
|
|
23
25
|
private tzid;
|
|
24
26
|
private originalDtstart;
|
|
@@ -38,6 +40,7 @@ declare class RRuleTemporal {
|
|
|
38
40
|
private generateWeeklyOccurrences;
|
|
39
41
|
private matchesByDay;
|
|
40
42
|
private matchesByMonth;
|
|
43
|
+
private matchesByMonthDay;
|
|
41
44
|
private matchesAll;
|
|
42
45
|
options(): ManualOpts;
|
|
43
46
|
/**
|
|
@@ -69,11 +72,21 @@ declare class RRuleTemporal {
|
|
|
69
72
|
*/
|
|
70
73
|
previous(before?: Date | Temporal.ZonedDateTime, inc?: boolean): Temporal.ZonedDateTime | null;
|
|
71
74
|
toString(): string;
|
|
75
|
+
/**
|
|
76
|
+
* Convert this rule to a human readable English description.
|
|
77
|
+
*/
|
|
78
|
+
toText(formatter?: DateFormatter): string;
|
|
72
79
|
/**
|
|
73
80
|
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
74
81
|
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
75
82
|
*/
|
|
76
83
|
private generateMonthlyOccurrences;
|
|
84
|
+
/**
|
|
85
|
+
* Given any date in a year, return all ZonedDateTimes in that year matching
|
|
86
|
+
* the BYDAY/BYMONTHDAY/BYMONTH constraints. Months default to DTSTART's month
|
|
87
|
+
* if BYMONTH is not specified.
|
|
88
|
+
*/
|
|
89
|
+
private generateYearlyOccurrences;
|
|
77
90
|
}
|
|
78
91
|
|
|
79
|
-
export { type
|
|
92
|
+
export { type DateFormatter, type RRuleOptions, RRuleTemporal };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,59 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { Temporal } from "@js-temporal/polyfill";
|
|
3
|
+
var WEEKDAY_NAMES = [
|
|
4
|
+
"Monday",
|
|
5
|
+
"Tuesday",
|
|
6
|
+
"Wednesday",
|
|
7
|
+
"Thursday",
|
|
8
|
+
"Friday",
|
|
9
|
+
"Saturday",
|
|
10
|
+
"Sunday"
|
|
11
|
+
];
|
|
12
|
+
var MONTH_NAMES = [
|
|
13
|
+
"January",
|
|
14
|
+
"February",
|
|
15
|
+
"March",
|
|
16
|
+
"April",
|
|
17
|
+
"May",
|
|
18
|
+
"June",
|
|
19
|
+
"July",
|
|
20
|
+
"August",
|
|
21
|
+
"September",
|
|
22
|
+
"October",
|
|
23
|
+
"November",
|
|
24
|
+
"December"
|
|
25
|
+
];
|
|
26
|
+
var defaultDateFormatter = (year, month, day) => `${month} ${day}, ${year}`;
|
|
27
|
+
function ordinal(n) {
|
|
28
|
+
const abs = Math.abs(n);
|
|
29
|
+
const suffix = abs % 10 === 1 && abs % 100 !== 11 ? "st" : abs % 10 === 2 && abs % 100 !== 12 ? "nd" : abs % 10 === 3 && abs % 100 !== 13 ? "rd" : "th";
|
|
30
|
+
return n < 0 ? `last` : `${abs}${suffix}`;
|
|
31
|
+
}
|
|
32
|
+
function list(arr, mapFn = (x) => `${x}`, final = "and") {
|
|
33
|
+
const mapped = arr.map(mapFn);
|
|
34
|
+
if (mapped.length === 1) return mapped[0];
|
|
35
|
+
return mapped.slice(0, -1).join(", ") + ` ${final} ` + mapped[mapped.length - 1];
|
|
36
|
+
}
|
|
37
|
+
function formatByDayToken(tok) {
|
|
38
|
+
if (typeof tok === "number") return tok.toString();
|
|
39
|
+
const m = tok.match(/^([+-]?\d+)?(MO|TU|WE|TH|FR|SA|SU)$/);
|
|
40
|
+
if (!m) return tok;
|
|
41
|
+
const ord = m[1] ? parseInt(m[1], 10) : 0;
|
|
42
|
+
const weekdayMap = {
|
|
43
|
+
MO: 0,
|
|
44
|
+
TU: 1,
|
|
45
|
+
WE: 2,
|
|
46
|
+
TH: 3,
|
|
47
|
+
FR: 4,
|
|
48
|
+
SA: 5,
|
|
49
|
+
SU: 6
|
|
50
|
+
};
|
|
51
|
+
const idx = weekdayMap[m[2]];
|
|
52
|
+
const name = WEEKDAY_NAMES[idx];
|
|
53
|
+
if (ord === 0) return name;
|
|
54
|
+
if (ord === -1) return `last ${name}`;
|
|
55
|
+
return `${ordinal(ord)} ${name}`;
|
|
56
|
+
}
|
|
3
57
|
function parseRRuleString(input, fallbackDtstart) {
|
|
4
58
|
let dtstart;
|
|
5
59
|
let tzid = "UTC";
|
|
@@ -59,10 +113,30 @@ function parseRRuleString(input, fallbackDtstart) {
|
|
|
59
113
|
case "BYMONTH":
|
|
60
114
|
opts.byMonth = val.split(",").map((n) => parseInt(n, 10));
|
|
61
115
|
break;
|
|
116
|
+
case "BYMONTHDAY":
|
|
117
|
+
opts.byMonthDay = val.split(",").map((n) => parseInt(n, 10));
|
|
118
|
+
break;
|
|
62
119
|
}
|
|
63
120
|
}
|
|
64
121
|
return opts;
|
|
65
122
|
}
|
|
123
|
+
function formatTime(hour, minute = 0) {
|
|
124
|
+
const hr12 = (hour + 11) % 12 + 1;
|
|
125
|
+
const ampm = hour < 12 ? "AM" : "PM";
|
|
126
|
+
if (minute) {
|
|
127
|
+
return `${hr12}:${String(minute).padStart(2, "0")} ${ampm}`;
|
|
128
|
+
}
|
|
129
|
+
return `${hr12} ${ampm}`;
|
|
130
|
+
}
|
|
131
|
+
function tzAbbreviation(zdt) {
|
|
132
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
133
|
+
timeZone: zdt.timeZoneId,
|
|
134
|
+
timeZoneName: "short",
|
|
135
|
+
hour: "numeric"
|
|
136
|
+
}).formatToParts(new Date(zdt.epochMilliseconds));
|
|
137
|
+
const tzPart = parts.find((p) => p.type === "timeZoneName");
|
|
138
|
+
return tzPart?.value || zdt.timeZoneId;
|
|
139
|
+
}
|
|
66
140
|
var RRuleTemporal = class {
|
|
67
141
|
tzid;
|
|
68
142
|
originalDtstart;
|
|
@@ -215,7 +289,6 @@ var RRuleTemporal = class {
|
|
|
215
289
|
if (zdt.dayOfWeek === wd) return true;
|
|
216
290
|
continue;
|
|
217
291
|
}
|
|
218
|
-
const year = zdt.year;
|
|
219
292
|
const month = zdt.month;
|
|
220
293
|
let dt = zdt.with({ day: 1 });
|
|
221
294
|
const candidates = [];
|
|
@@ -233,8 +306,16 @@ var RRuleTemporal = class {
|
|
|
233
306
|
if (!byMonth) return true;
|
|
234
307
|
return byMonth.includes(zdt.month);
|
|
235
308
|
}
|
|
309
|
+
matchesByMonthDay(zdt) {
|
|
310
|
+
const { byMonthDay } = this.opts;
|
|
311
|
+
if (!byMonthDay) return true;
|
|
312
|
+
const lastDay = zdt.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
313
|
+
return byMonthDay.some(
|
|
314
|
+
(d) => d > 0 ? zdt.day === d : zdt.day === lastDay + d + 1
|
|
315
|
+
);
|
|
316
|
+
}
|
|
236
317
|
matchesAll(zdt) {
|
|
237
|
-
return this.matchesByDay(zdt) && this.matchesByMonth(zdt);
|
|
318
|
+
return this.matchesByDay(zdt) && this.matchesByMonth(zdt) && this.matchesByMonthDay(zdt);
|
|
238
319
|
}
|
|
239
320
|
options() {
|
|
240
321
|
return this.opts;
|
|
@@ -249,7 +330,7 @@ var RRuleTemporal = class {
|
|
|
249
330
|
throw new Error("all() requires iterator when no COUNT/UNTIL");
|
|
250
331
|
}
|
|
251
332
|
const dates = [];
|
|
252
|
-
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
333
|
+
if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
253
334
|
const start = this.originalDtstart;
|
|
254
335
|
let monthCursor = start.with({ day: 1 });
|
|
255
336
|
let matchCount2 = 0;
|
|
@@ -323,7 +404,7 @@ var RRuleTemporal = class {
|
|
|
323
404
|
}
|
|
324
405
|
return dates;
|
|
325
406
|
}
|
|
326
|
-
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
407
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
|
|
327
408
|
const start = this.originalDtstart;
|
|
328
409
|
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
329
410
|
let i = 0;
|
|
@@ -350,6 +431,30 @@ var RRuleTemporal = class {
|
|
|
350
431
|
}
|
|
351
432
|
return dates;
|
|
352
433
|
}
|
|
434
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
435
|
+
const start = this.originalDtstart;
|
|
436
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
437
|
+
let matchCount2 = 0;
|
|
438
|
+
outer_year: while (true) {
|
|
439
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
440
|
+
for (const occ of occs) {
|
|
441
|
+
if (Temporal.ZonedDateTime.compare(occ, start) < 0) continue;
|
|
442
|
+
if (this.opts.until && Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
|
|
443
|
+
break outer_year;
|
|
444
|
+
}
|
|
445
|
+
if (this.opts.count !== void 0 && matchCount2 >= this.opts.count) {
|
|
446
|
+
break outer_year;
|
|
447
|
+
}
|
|
448
|
+
if (iterator && !iterator(occ, matchCount2)) {
|
|
449
|
+
break outer_year;
|
|
450
|
+
}
|
|
451
|
+
dates.push(occ);
|
|
452
|
+
matchCount2++;
|
|
453
|
+
}
|
|
454
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
455
|
+
}
|
|
456
|
+
return dates;
|
|
457
|
+
}
|
|
353
458
|
let current = this.computeFirst();
|
|
354
459
|
let matchCount = 0;
|
|
355
460
|
while (true) {
|
|
@@ -381,7 +486,7 @@ var RRuleTemporal = class {
|
|
|
381
486
|
const startInst = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
382
487
|
const endInst = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
383
488
|
const results = [];
|
|
384
|
-
if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
|
|
489
|
+
if (this.opts.freq === "MONTHLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
385
490
|
let monthCursor = this.computeFirst().with({ day: 1 });
|
|
386
491
|
outer: while (true) {
|
|
387
492
|
const occs = this.generateMonthlyOccurrences(monthCursor);
|
|
@@ -398,7 +503,7 @@ var RRuleTemporal = class {
|
|
|
398
503
|
}
|
|
399
504
|
return results;
|
|
400
505
|
}
|
|
401
|
-
if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
|
|
506
|
+
if (this.opts.freq === "YEARLY" && this.opts.byMonth && !this.opts.byDay && !this.opts.byMonthDay) {
|
|
402
507
|
const start = this.originalDtstart;
|
|
403
508
|
const months = [...this.opts.byMonth].sort((a, b) => a - b);
|
|
404
509
|
let i = 0;
|
|
@@ -418,6 +523,24 @@ var RRuleTemporal = class {
|
|
|
418
523
|
}
|
|
419
524
|
return results;
|
|
420
525
|
}
|
|
526
|
+
if (this.opts.freq === "YEARLY" && (this.opts.byDay || this.opts.byMonthDay)) {
|
|
527
|
+
const start = this.originalDtstart;
|
|
528
|
+
let yearCursor = start.with({ month: 1, day: 1 });
|
|
529
|
+
outer_year: while (true) {
|
|
530
|
+
const occs = this.generateYearlyOccurrences(yearCursor);
|
|
531
|
+
for (const occ of occs) {
|
|
532
|
+
const inst = occ.toInstant();
|
|
533
|
+
if (inc ? Temporal.Instant.compare(inst, endInst) > 0 : Temporal.Instant.compare(inst, endInst) >= 0) {
|
|
534
|
+
break outer_year;
|
|
535
|
+
}
|
|
536
|
+
if (Temporal.Instant.compare(inst, startInst) >= 0) {
|
|
537
|
+
results.push(occ);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
yearCursor = yearCursor.add({ years: this.opts.interval });
|
|
541
|
+
}
|
|
542
|
+
return results;
|
|
543
|
+
}
|
|
421
544
|
if (this.opts.freq === "WEEKLY") {
|
|
422
545
|
const startInst2 = after instanceof Date ? Temporal.Instant.from(after.toISOString()) : after.toInstant();
|
|
423
546
|
const endInst2 = before instanceof Date ? Temporal.Instant.from(before.toISOString()) : before.toInstant();
|
|
@@ -508,24 +631,123 @@ var RRuleTemporal = class {
|
|
|
508
631
|
const iso = this.originalDtstart.toString({ smallestUnit: "second" }).replace(/[-:]/g, "");
|
|
509
632
|
const dtLine = `DTSTART;TZID=${this.tzid}:${iso.slice(0, 15)}`;
|
|
510
633
|
const parts = [];
|
|
511
|
-
const {
|
|
634
|
+
const {
|
|
635
|
+
freq,
|
|
636
|
+
interval,
|
|
637
|
+
count,
|
|
638
|
+
until,
|
|
639
|
+
byHour,
|
|
640
|
+
byMinute,
|
|
641
|
+
byDay,
|
|
642
|
+
byMonth,
|
|
643
|
+
byMonthDay
|
|
644
|
+
} = this.opts;
|
|
512
645
|
parts.push(`FREQ=${freq}`);
|
|
513
646
|
if (interval !== 1) parts.push(`INTERVAL=${interval}`);
|
|
514
647
|
if (count !== void 0) parts.push(`COUNT=${count}`);
|
|
648
|
+
if (until) {
|
|
649
|
+
const u = until.toInstant().toString().replace(/[-:]/g, "");
|
|
650
|
+
parts.push(`UNTIL=${u.slice(0, 15)}Z`);
|
|
651
|
+
}
|
|
515
652
|
if (byHour) parts.push(`BYHOUR=${byHour.join(",")}`);
|
|
516
653
|
if (byMinute) parts.push(`BYMINUTE=${byMinute.join(",")}`);
|
|
517
654
|
if (byDay) parts.push(`BYDAY=${byDay.join(",")}`);
|
|
518
655
|
if (byMonth) parts.push(`BYMONTH=${byMonth.join(",")}`);
|
|
656
|
+
if (byMonthDay) parts.push(`BYMONTHDAY=${byMonthDay.join(",")}`);
|
|
519
657
|
return [dtLine, `RRULE:${parts.join(";")}`].join("\n");
|
|
520
658
|
}
|
|
659
|
+
/**
|
|
660
|
+
* Convert this rule to a human readable English description.
|
|
661
|
+
*/
|
|
662
|
+
toText(formatter = defaultDateFormatter) {
|
|
663
|
+
const {
|
|
664
|
+
freq,
|
|
665
|
+
interval = 1,
|
|
666
|
+
count,
|
|
667
|
+
until,
|
|
668
|
+
byDay,
|
|
669
|
+
byHour,
|
|
670
|
+
byMinute,
|
|
671
|
+
byMonth,
|
|
672
|
+
byMonthDay
|
|
673
|
+
} = this.opts;
|
|
674
|
+
const parts = ["every"];
|
|
675
|
+
const base = {
|
|
676
|
+
YEARLY: "year",
|
|
677
|
+
MONTHLY: "month",
|
|
678
|
+
WEEKLY: "week",
|
|
679
|
+
DAILY: "day",
|
|
680
|
+
HOURLY: "hour",
|
|
681
|
+
MINUTELY: "minute",
|
|
682
|
+
SECONDLY: "second"
|
|
683
|
+
}[freq];
|
|
684
|
+
const daysNormalized = byDay?.map((d) => d.toUpperCase());
|
|
685
|
+
const isWeekdays = daysNormalized && daysNormalized.length === 5 && ["MO", "TU", "WE", "TH", "FR"].every((d) => daysNormalized.includes(d));
|
|
686
|
+
const isEveryday = daysNormalized && daysNormalized.length === 7 && ["MO", "TU", "WE", "TH", "FR", "SA", "SU"].every(
|
|
687
|
+
(d) => daysNormalized.includes(d)
|
|
688
|
+
);
|
|
689
|
+
if (freq === "WEEKLY" && interval === 1 && isWeekdays) {
|
|
690
|
+
parts.push("weekday");
|
|
691
|
+
} else if (freq === "WEEKLY" && interval === 1 && isEveryday) {
|
|
692
|
+
parts.push("day");
|
|
693
|
+
} else {
|
|
694
|
+
if (interval !== 1) {
|
|
695
|
+
parts.push(interval.toString(), base + "s");
|
|
696
|
+
} else {
|
|
697
|
+
parts.push(base);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
if (freq === "WEEKLY" && byDay && !isWeekdays && !isEveryday) {
|
|
701
|
+
parts.push("on", list(byDay, formatByDayToken));
|
|
702
|
+
} else if (byDay && freq !== "WEEKLY") {
|
|
703
|
+
parts.push("on", list(byDay, formatByDayToken));
|
|
704
|
+
}
|
|
705
|
+
if (byMonth) {
|
|
706
|
+
parts.push(
|
|
707
|
+
"in",
|
|
708
|
+
list(byMonth, (m) => MONTH_NAMES[m - 1])
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
if (byMonthDay) {
|
|
712
|
+
parts.push(
|
|
713
|
+
"on the",
|
|
714
|
+
list(byMonthDay, (d) => ordinal(d)),
|
|
715
|
+
"day of the month"
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
if (byHour) {
|
|
719
|
+
const minutes = byMinute ?? [0];
|
|
720
|
+
const times = byHour.flatMap((h) => minutes.map((m) => formatTime(h, m)));
|
|
721
|
+
parts.push("at", list(times));
|
|
722
|
+
parts.push(tzAbbreviation(this.originalDtstart));
|
|
723
|
+
}
|
|
724
|
+
if (until) {
|
|
725
|
+
parts.push(
|
|
726
|
+
"until",
|
|
727
|
+
formatter(until.year, MONTH_NAMES[until.month - 1], until.day)
|
|
728
|
+
);
|
|
729
|
+
} else if (count !== void 0) {
|
|
730
|
+
parts.push("for", count.toString(), count === 1 ? "time" : "times");
|
|
731
|
+
}
|
|
732
|
+
return parts.join(" ");
|
|
733
|
+
}
|
|
521
734
|
/**
|
|
522
735
|
* Given any date in a month, return all the ZonedDateTimes in that month
|
|
523
736
|
* matching your opts.byDay and opts.byMonth (or the single "same day" if no BYDAY).
|
|
524
737
|
*/
|
|
525
738
|
generateMonthlyOccurrences(sample) {
|
|
526
|
-
const { byDay, byMonth,
|
|
739
|
+
const { byDay, byMonth, byMonthDay } = this.opts;
|
|
527
740
|
if (byMonth && !byMonth.includes(sample.month)) return [];
|
|
528
|
-
|
|
741
|
+
const lastDay = sample.with({ day: 1 }).add({ months: 1 }).subtract({ days: 1 }).day;
|
|
742
|
+
let byMonthDayHits = [];
|
|
743
|
+
if (byMonthDay && byMonthDay.length > 0) {
|
|
744
|
+
byMonthDayHits = byMonthDay.map((d) => d > 0 ? d : lastDay + d + 1).filter((d) => d >= 1 && d <= lastDay);
|
|
745
|
+
}
|
|
746
|
+
if (!byDay && byMonthDayHits.length) {
|
|
747
|
+
const dates = byMonthDayHits.map((d) => sample.with({ day: d }));
|
|
748
|
+
return dates.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
749
|
+
}
|
|
750
|
+
if (!byDay) {
|
|
529
751
|
return this.expandByHourMinute(sample);
|
|
530
752
|
}
|
|
531
753
|
const dayMap = {
|
|
@@ -549,20 +771,38 @@ var RRuleTemporal = class {
|
|
|
549
771
|
(buckets[dow] ||= []).push(cursor.day);
|
|
550
772
|
cursor = cursor.add({ days: 1 });
|
|
551
773
|
}
|
|
552
|
-
const
|
|
774
|
+
const byDayHits = [];
|
|
553
775
|
for (const { ord, wd } of tokens) {
|
|
554
|
-
const
|
|
555
|
-
if (!
|
|
776
|
+
const list2 = buckets[wd] ?? [];
|
|
777
|
+
if (!list2.length) continue;
|
|
556
778
|
if (ord === 0) {
|
|
557
|
-
for (const d of
|
|
779
|
+
for (const d of list2) byDayHits.push(d);
|
|
558
780
|
} else {
|
|
559
|
-
const idx = ord > 0 ? ord - 1 :
|
|
560
|
-
const dayN =
|
|
561
|
-
if (dayN)
|
|
781
|
+
const idx = ord > 0 ? ord - 1 : list2.length + ord;
|
|
782
|
+
const dayN = list2[idx];
|
|
783
|
+
if (dayN) byDayHits.push(dayN);
|
|
562
784
|
}
|
|
563
785
|
}
|
|
786
|
+
let finalDays = byDayHits;
|
|
787
|
+
if (byMonthDayHits.length) {
|
|
788
|
+
finalDays = finalDays.filter((d) => byMonthDayHits.includes(d));
|
|
789
|
+
}
|
|
790
|
+
const hits = finalDays.map((d) => sample.with({ day: d }));
|
|
564
791
|
return hits.flatMap((z) => this.expandByHourMinute(z)).sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
565
792
|
}
|
|
793
|
+
/**
|
|
794
|
+
* Given any date in a year, return all ZonedDateTimes in that year matching
|
|
795
|
+
* the BYDAY/BYMONTHDAY/BYMONTH constraints. Months default to DTSTART's month
|
|
796
|
+
* if BYMONTH is not specified.
|
|
797
|
+
*/
|
|
798
|
+
generateYearlyOccurrences(sample) {
|
|
799
|
+
const months = this.opts.byMonth ? [...this.opts.byMonth].sort((a, b) => a - b) : [this.originalDtstart.month];
|
|
800
|
+
const occs = months.flatMap((m) => {
|
|
801
|
+
const monthSample = sample.with({ month: m, day: 1 });
|
|
802
|
+
return this.generateMonthlyOccurrences(monthSample);
|
|
803
|
+
});
|
|
804
|
+
return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
|
|
805
|
+
}
|
|
566
806
|
};
|
|
567
807
|
export {
|
|
568
808
|
RRuleTemporal
|