@marktiderman/genesis-core 2.4.1 → 2.5.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 +42 -0
- package/dist/chunk-227N654S.js +497 -0
- package/dist/chunk-227N654S.js.map +1 -0
- package/dist/{chunk-ZIMQUFXD.js → chunk-37RWGNIO.js} +9 -4
- package/dist/chunk-37RWGNIO.js.map +1 -0
- package/dist/{chunk-5NEOX2N7.js → chunk-H2RGMX6L.js} +4 -4
- package/dist/{chunk-5NEOX2N7.js.map → chunk-H2RGMX6L.js.map} +1 -1
- package/dist/dates.d.ts +173 -1
- package/dist/dates.js +1 -1
- package/dist/errors.d.ts +8 -1
- package/dist/errors.js +1 -1
- package/dist/hooks/index.js +3 -3
- package/dist/index.js +3 -3
- package/dist/resource-view/index.d.ts +34 -1
- package/dist/resource-view/index.js +28 -3
- package/dist/resource-view/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-3B4IFDMK.js +0 -117
- package/dist/chunk-3B4IFDMK.js.map +0 -1
- package/dist/chunk-ZIMQUFXD.js.map +0 -1
package/README.md
CHANGED
|
@@ -686,4 +686,46 @@ const { start, end } = dayRangeIn(today, profile.timeZone); // the instants, for
|
|
|
686
686
|
- `addDays`, `daysBetween`, `weekdayOf`, `startOfWeek` and `isDay`: plain calendar arithmetic on
|
|
687
687
|
`YYYY-MM-DD` strings, with no zone involved. They refuse days that don't exist, such as `2026-02-30`.
|
|
688
688
|
|
|
689
|
+
### Calendar ranges
|
|
690
|
+
|
|
691
|
+
Ranges are `{ start, end }` days, `end` exclusive, for the windows a calendar shows:
|
|
692
|
+
|
|
693
|
+
- `startOfMonth`, `addMonths` (31 January + 1 is the last day of February), `daysInMonth`,
|
|
694
|
+
`startOfQuarter`, `addWeeks`.
|
|
695
|
+
- `monthRange(day)`, `quarterRange(day)`, `weeksRange(day, weeks, weekStartsOn)` (whole weeks from
|
|
696
|
+
the week `day` is in) and `monthGridRange(day, weekStartsOn)` (the whole weeks covering a month).
|
|
697
|
+
- `eachDay(range)` lists the days; `rangeIn(range, zone)` gives the instants to filter a timestamp by.
|
|
698
|
+
- `instantIn(day, "09:30", zone)` and `wallTimeIn(zone, at)` convert between a wall time and an
|
|
699
|
+
instant. A time the clock skips moves forward by the jump; a time it shows twice is the first.
|
|
700
|
+
- `readEventTime(value, { dateOnly, timeZone })` reads a stored date or timestamp as a day or an
|
|
701
|
+
instant; a timestamp without a zone is a wall time on `timeZone`'s clock.
|
|
702
|
+
|
|
703
|
+
### Repeating events
|
|
704
|
+
|
|
705
|
+
`expandRecurrence` lists a series' occurrences inside a window, with no dependencies:
|
|
706
|
+
|
|
707
|
+
```ts
|
|
708
|
+
import { expandRecurrence } from "@marktiderman/genesis-core/dates";
|
|
709
|
+
|
|
710
|
+
const { occurrences, truncated } = expandRecurrence({
|
|
711
|
+
rule: "FREQ=MONTHLY;BYDAY=2MO;COUNT=10",
|
|
712
|
+
start: { day: "2026-11-09", time: "18:30", timeZone: "America/Denver" },
|
|
713
|
+
exdates: ["2026-12-14"],
|
|
714
|
+
window: { start: "2026-11-01", end: "2027-03-01" },
|
|
715
|
+
}); // occurrences: [{ day, start }…], 18:30 in Denver whatever the daylight saving
|
|
716
|
+
if (truncated) {
|
|
717
|
+
// more than `limit` (default 1,000) in the window
|
|
718
|
+
}
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
- The rule is RFC 5545's: FREQ (DAILY, WEEKLY, MONTHLY, YEARLY), INTERVAL, COUNT, UNTIL, BYDAY
|
|
722
|
+
(with positions such as `2MO` or `-1FR` for MONTHLY and YEARLY), BYMONTHDAY, BYMONTH and WKST.
|
|
723
|
+
Any other part, a part set twice, or a combination the RFC forbids throws a `RangeError` naming
|
|
724
|
+
it (`parseRRule` does the same checks, and a rule passed as an object gets them too).
|
|
725
|
+
- A timed series keeps its wall time on its own zone's clock; an all-day series (`start` a day) has
|
|
726
|
+
days only. COUNT counts from the start whatever the window, EXDATE removes after counting, and a
|
|
727
|
+
floating UNTIL is read on the series' clock.
|
|
728
|
+
- `window` is required, and `limit` caps the result. Widen the window's start by an event's length
|
|
729
|
+
to catch ones that began earlier.
|
|
730
|
+
|
|
689
731
|
In SQL, the same day is `(now() at time zone person_zone)::date`.
|
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
// src/dates.ts
|
|
2
|
+
var DAY = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
3
|
+
function isDay(value) {
|
|
4
|
+
if (typeof value !== "string") return false;
|
|
5
|
+
const m = DAY.exec(value);
|
|
6
|
+
if (!m) return false;
|
|
7
|
+
const d = new Date(Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3])));
|
|
8
|
+
return d.getUTCFullYear() === Number(m[1]) && d.getUTCMonth() === Number(m[2]) - 1 && d.getUTCDate() === Number(m[3]);
|
|
9
|
+
}
|
|
10
|
+
function assertDay(day, where) {
|
|
11
|
+
if (!isDay(day)) throw new RangeError(`${where}: "${String(day)}" is not a YYYY-MM-DD day`);
|
|
12
|
+
const m = DAY.exec(day);
|
|
13
|
+
return [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
14
|
+
}
|
|
15
|
+
var formatters = /* @__PURE__ */ new Map();
|
|
16
|
+
function isTimeZone(zone) {
|
|
17
|
+
if (typeof zone !== "string" || !zone) return false;
|
|
18
|
+
if (formatters.has(zone)) return true;
|
|
19
|
+
try {
|
|
20
|
+
new Intl.DateTimeFormat("en-US", { timeZone: zone });
|
|
21
|
+
return true;
|
|
22
|
+
} catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function deviceTimeZone() {
|
|
27
|
+
try {
|
|
28
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
|
29
|
+
} catch {
|
|
30
|
+
return "UTC";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function partsFormatter(zone) {
|
|
34
|
+
let f = formatters.get(zone);
|
|
35
|
+
if (!f) {
|
|
36
|
+
f = new Intl.DateTimeFormat("en-US", {
|
|
37
|
+
timeZone: zone,
|
|
38
|
+
hourCycle: "h23",
|
|
39
|
+
year: "numeric",
|
|
40
|
+
month: "2-digit",
|
|
41
|
+
day: "2-digit",
|
|
42
|
+
hour: "2-digit",
|
|
43
|
+
minute: "2-digit",
|
|
44
|
+
second: "2-digit",
|
|
45
|
+
weekday: "short"
|
|
46
|
+
});
|
|
47
|
+
formatters.set(zone, f);
|
|
48
|
+
}
|
|
49
|
+
return f;
|
|
50
|
+
}
|
|
51
|
+
var WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
52
|
+
function wallClock(zone, at = /* @__PURE__ */ new Date()) {
|
|
53
|
+
if (!isTimeZone(zone)) throw new RangeError(`wallClock: unknown time zone "${zone}"`);
|
|
54
|
+
const parts = {};
|
|
55
|
+
for (const p of partsFormatter(zone).formatToParts(at)) parts[p.type] = p.value;
|
|
56
|
+
return {
|
|
57
|
+
year: Number(parts["year"]),
|
|
58
|
+
month: Number(parts["month"]),
|
|
59
|
+
day: Number(parts["day"]),
|
|
60
|
+
hour: Number(parts["hour"]),
|
|
61
|
+
minute: Number(parts["minute"]),
|
|
62
|
+
second: Number(parts["second"]),
|
|
63
|
+
weekday: WEEKDAYS.indexOf(parts["weekday"] ?? "")
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
var pad = (n, width = 2) => String(n).padStart(width, "0");
|
|
67
|
+
var dayOf = (y, m, d) => `${pad(y, 4)}-${pad(m)}-${pad(d)}`;
|
|
68
|
+
function dayIn(zone, at = /* @__PURE__ */ new Date()) {
|
|
69
|
+
const c = wallClock(zone, at);
|
|
70
|
+
return dayOf(c.year, c.month, c.day);
|
|
71
|
+
}
|
|
72
|
+
function personTimeZone(person) {
|
|
73
|
+
const saved = person?.timeZone;
|
|
74
|
+
return saved && isTimeZone(saved) ? saved : deviceTimeZone();
|
|
75
|
+
}
|
|
76
|
+
function personDay(person, at = /* @__PURE__ */ new Date()) {
|
|
77
|
+
return dayIn(personTimeZone(person), at);
|
|
78
|
+
}
|
|
79
|
+
function addDays(day, days) {
|
|
80
|
+
const [y, m, d] = assertDay(day, "addDays");
|
|
81
|
+
if (!Number.isInteger(days)) throw new RangeError(`addDays: ${days} is not a whole number of days`);
|
|
82
|
+
const t = new Date(Date.UTC(y, m - 1, d + days));
|
|
83
|
+
return dayOf(t.getUTCFullYear(), t.getUTCMonth() + 1, t.getUTCDate());
|
|
84
|
+
}
|
|
85
|
+
function daysBetween(from, to) {
|
|
86
|
+
const [y1, m1, d1] = assertDay(from, "daysBetween");
|
|
87
|
+
const [y2, m2, d2] = assertDay(to, "daysBetween");
|
|
88
|
+
return Math.round((Date.UTC(y2, m2 - 1, d2) - Date.UTC(y1, m1 - 1, d1)) / 864e5);
|
|
89
|
+
}
|
|
90
|
+
function weekdayOf(day) {
|
|
91
|
+
const [y, m, d] = assertDay(day, "weekdayOf");
|
|
92
|
+
return new Date(Date.UTC(y, m - 1, d)).getUTCDay();
|
|
93
|
+
}
|
|
94
|
+
function startOfWeek(day, weekStartsOn = 1) {
|
|
95
|
+
if (!Number.isInteger(weekStartsOn) || weekStartsOn < 0 || weekStartsOn > 6) throw new RangeError("startOfWeek: weekStartsOn is 0 to 6");
|
|
96
|
+
return addDays(day, -((weekdayOf(day) - weekStartsOn + 7) % 7));
|
|
97
|
+
}
|
|
98
|
+
function startOfDayIn(day, zone) {
|
|
99
|
+
const [y, m, d] = assertDay(day, "startOfDayIn");
|
|
100
|
+
const target = Date.UTC(y, m - 1, d);
|
|
101
|
+
let at = target;
|
|
102
|
+
for (let i = 0; i < 2; i++) {
|
|
103
|
+
const c = wallClock(zone, new Date(at));
|
|
104
|
+
const shown = Date.UTC(c.year, c.month - 1, c.day, c.hour, c.minute, c.second);
|
|
105
|
+
at -= shown - target;
|
|
106
|
+
}
|
|
107
|
+
while (dayIn(zone, new Date(at)) < day) at += 15 * 6e4;
|
|
108
|
+
while (dayIn(zone, new Date(at - 1)) === day) at -= 15 * 6e4;
|
|
109
|
+
return new Date(at);
|
|
110
|
+
}
|
|
111
|
+
function dayRangeIn(day, zone) {
|
|
112
|
+
return { start: startOfDayIn(day, zone), end: startOfDayIn(addDays(day, 1), zone) };
|
|
113
|
+
}
|
|
114
|
+
function daysInMonth(day) {
|
|
115
|
+
const [y, m] = assertDay(day, "daysInMonth");
|
|
116
|
+
return new Date(Date.UTC(y, m, 0)).getUTCDate();
|
|
117
|
+
}
|
|
118
|
+
function startOfMonth(day) {
|
|
119
|
+
const [y, m] = assertDay(day, "startOfMonth");
|
|
120
|
+
return dayOf(y, m, 1);
|
|
121
|
+
}
|
|
122
|
+
function addMonths(day, months) {
|
|
123
|
+
const [y, m, d] = assertDay(day, "addMonths");
|
|
124
|
+
if (!Number.isInteger(months)) throw new RangeError(`addMonths: ${months} is not a whole number of months`);
|
|
125
|
+
const index = y * 12 + (m - 1) + months;
|
|
126
|
+
const ny = Math.floor(index / 12);
|
|
127
|
+
const nm = index - ny * 12 + 1;
|
|
128
|
+
const last = new Date(Date.UTC(ny, nm, 0)).getUTCDate();
|
|
129
|
+
return dayOf(ny, nm, Math.min(d, last));
|
|
130
|
+
}
|
|
131
|
+
function startOfQuarter(day) {
|
|
132
|
+
const [y, m] = assertDay(day, "startOfQuarter");
|
|
133
|
+
return dayOf(y, m - (m - 1) % 3, 1);
|
|
134
|
+
}
|
|
135
|
+
function addWeeks(day, weeks) {
|
|
136
|
+
if (!Number.isInteger(weeks)) throw new RangeError(`addWeeks: ${weeks} is not a whole number of weeks`);
|
|
137
|
+
return addDays(day, weeks * 7);
|
|
138
|
+
}
|
|
139
|
+
function monthRange(day) {
|
|
140
|
+
const start = startOfMonth(day);
|
|
141
|
+
return { start, end: addMonths(start, 1) };
|
|
142
|
+
}
|
|
143
|
+
function quarterRange(day) {
|
|
144
|
+
const start = startOfQuarter(day);
|
|
145
|
+
return { start, end: addMonths(start, 3) };
|
|
146
|
+
}
|
|
147
|
+
function weeksRange(day, weeks, weekStartsOn = 1) {
|
|
148
|
+
if (!Number.isInteger(weeks) || weeks < 1) throw new RangeError(`weeksRange: ${weeks} is not a whole number of weeks, 1 or more`);
|
|
149
|
+
const start = startOfWeek(day, weekStartsOn);
|
|
150
|
+
return { start, end: addWeeks(start, weeks) };
|
|
151
|
+
}
|
|
152
|
+
function monthGridRange(day, weekStartsOn = 1) {
|
|
153
|
+
const month = monthRange(day);
|
|
154
|
+
const start = startOfWeek(month.start, weekStartsOn);
|
|
155
|
+
const lastWeek = startOfWeek(addDays(month.end, -1), weekStartsOn);
|
|
156
|
+
return { start, end: addWeeks(lastWeek, 1) };
|
|
157
|
+
}
|
|
158
|
+
function eachDay(range) {
|
|
159
|
+
const n = daysBetween(range.start, range.end);
|
|
160
|
+
return Array.from({ length: Math.max(0, n) }, (_, i) => addDays(range.start, i));
|
|
161
|
+
}
|
|
162
|
+
function rangeIn(range, zone) {
|
|
163
|
+
return { start: startOfDayIn(range.start, zone), end: startOfDayIn(range.end, zone) };
|
|
164
|
+
}
|
|
165
|
+
var WALL_TIME = /^(\d{2}):(\d{2})(?::(\d{2}))?$/;
|
|
166
|
+
function validWallTime(time) {
|
|
167
|
+
const m = WALL_TIME.exec(time);
|
|
168
|
+
return Boolean(m) && Number(m[1]) <= 23 && Number(m[2]) <= 59 && Number(m[3] ?? 0) <= 59;
|
|
169
|
+
}
|
|
170
|
+
function parseWallTime(time, where) {
|
|
171
|
+
const m = WALL_TIME.exec(time);
|
|
172
|
+
if (!m || Number(m[1]) > 23 || Number(m[2]) > 59 || Number(m[3] ?? 0) > 59) {
|
|
173
|
+
throw new RangeError(`${where}: "${time}" is not an HH:MM[:SS] time`);
|
|
174
|
+
}
|
|
175
|
+
return [Number(m[1]), Number(m[2]), Number(m[3] ?? 0)];
|
|
176
|
+
}
|
|
177
|
+
function offsetAt(zone, at) {
|
|
178
|
+
const c = wallClock(zone, new Date(at));
|
|
179
|
+
return Date.UTC(c.year, c.month - 1, c.day, c.hour, c.minute, c.second) - (at - (at % 1e3 + 1e3) % 1e3);
|
|
180
|
+
}
|
|
181
|
+
function instantIn(day, time, zone) {
|
|
182
|
+
const [y, m, d] = assertDay(day, "instantIn");
|
|
183
|
+
const [h, mi, s] = parseWallTime(time, "instantIn");
|
|
184
|
+
if (!isTimeZone(zone)) throw new RangeError(`instantIn: unknown time zone "${zone}"`);
|
|
185
|
+
const target = Date.UTC(y, m - 1, d, h, mi, s);
|
|
186
|
+
const before = offsetAt(zone, target - 864e5);
|
|
187
|
+
const after = offsetAt(zone, target + 864e5);
|
|
188
|
+
const shows = (at) => offsetAt(zone, at) + at === target;
|
|
189
|
+
const early = target - Math.max(before, after);
|
|
190
|
+
const late = target - Math.min(before, after);
|
|
191
|
+
if (shows(early)) return new Date(early);
|
|
192
|
+
if (shows(late)) return new Date(late);
|
|
193
|
+
return new Date(target - before);
|
|
194
|
+
}
|
|
195
|
+
function wallTimeIn(zone, at) {
|
|
196
|
+
const c = wallClock(zone, at);
|
|
197
|
+
return { day: dayOf(c.year, c.month, c.day), time: `${pad(c.hour)}:${pad(c.minute)}:${pad(c.second)}` };
|
|
198
|
+
}
|
|
199
|
+
var RULE_DAYS = ["SU", "MO", "TU", "WE", "TH", "FR", "SA"];
|
|
200
|
+
var FREQS = ["DAILY", "WEEKLY", "MONTHLY", "YEARLY"];
|
|
201
|
+
function ruleInts(value, part, min, max, signed) {
|
|
202
|
+
return value.split(",").map((raw) => {
|
|
203
|
+
const n = Number(raw);
|
|
204
|
+
if (!/^[+-]?\d+$/.test(raw) || Math.abs(n) < min || Math.abs(n) > max || !signed && n < 0) {
|
|
205
|
+
throw new RangeError(`parseRRule: ${part}=${value} is out of range`);
|
|
206
|
+
}
|
|
207
|
+
return n;
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
function ruleDate(value) {
|
|
211
|
+
const m = /^(\d{4})(\d{2})(\d{2})(?:T(\d{2})(\d{2})(\d{2})(Z)?)?$/.exec(value);
|
|
212
|
+
if (!m) throw new RangeError(`parseRRule: UNTIL=${value} is not a date`);
|
|
213
|
+
const day = `${m[1]}-${m[2]}-${m[3]}`;
|
|
214
|
+
if (!isDay(day)) throw new RangeError(`parseRRule: UNTIL=${value} is not a date`);
|
|
215
|
+
if (!m[4]) return day;
|
|
216
|
+
if (!m[7]) {
|
|
217
|
+
const time = `${m[4]}:${m[5]}:${m[6]}`;
|
|
218
|
+
if (!WALL_TIME.test(time) || Number(m[4]) > 23 || Number(m[5]) > 59 || Number(m[6]) > 59) throw new RangeError(`parseRRule: UNTIL=${value} is not a date`);
|
|
219
|
+
return { day, time };
|
|
220
|
+
}
|
|
221
|
+
return new Date(Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]), Number(m[4]), Number(m[5]), Number(m[6])));
|
|
222
|
+
}
|
|
223
|
+
function parseRRule(text) {
|
|
224
|
+
const body = text.trim().replace(/^RRULE:/i, "");
|
|
225
|
+
const rule = { interval: 1, weekStart: 1 };
|
|
226
|
+
const seen = /* @__PURE__ */ new Set();
|
|
227
|
+
for (const piece of body.split(";")) {
|
|
228
|
+
if (!piece) continue;
|
|
229
|
+
const eq = piece.indexOf("=");
|
|
230
|
+
const key = piece.slice(0, eq).toUpperCase();
|
|
231
|
+
const value = piece.slice(eq + 1).toUpperCase();
|
|
232
|
+
if (eq < 1 || !value) throw new RangeError(`parseRRule: "${piece}" is not KEY=VALUE`);
|
|
233
|
+
if (seen.has(key)) throw new RangeError(`parseRRule: ${key} is set twice`);
|
|
234
|
+
seen.add(key);
|
|
235
|
+
switch (key) {
|
|
236
|
+
case "FREQ":
|
|
237
|
+
if (!FREQS.includes(value)) throw new RangeError(`parseRRule: FREQ=${value} isn't supported`);
|
|
238
|
+
rule.freq = value;
|
|
239
|
+
break;
|
|
240
|
+
case "INTERVAL":
|
|
241
|
+
rule.interval = ruleInts(value, key, 1, 1e4, false)[0];
|
|
242
|
+
break;
|
|
243
|
+
case "COUNT":
|
|
244
|
+
rule.count = ruleInts(value, key, 1, 1e5, false)[0];
|
|
245
|
+
break;
|
|
246
|
+
case "UNTIL":
|
|
247
|
+
rule.until = ruleDate(value);
|
|
248
|
+
break;
|
|
249
|
+
case "BYDAY":
|
|
250
|
+
rule.byDay = value.split(",").map((raw) => {
|
|
251
|
+
const m = /^([+-]?\d{1,2})?(SU|MO|TU|WE|TH|FR|SA)$/.exec(raw);
|
|
252
|
+
if (!m) throw new RangeError(`parseRRule: BYDAY=${value} isn't a weekday list`);
|
|
253
|
+
const nth = m[1] ? Number(m[1]) : void 0;
|
|
254
|
+
if (nth !== void 0 && (nth === 0 || Math.abs(nth) > 53)) throw new RangeError(`parseRRule: BYDAY=${value} is out of range`);
|
|
255
|
+
return nth === void 0 ? { weekday: RULE_DAYS.indexOf(m[2]) } : { weekday: RULE_DAYS.indexOf(m[2]), nth };
|
|
256
|
+
});
|
|
257
|
+
break;
|
|
258
|
+
case "BYMONTHDAY":
|
|
259
|
+
rule.byMonthDay = ruleInts(value, key, 1, 31, true);
|
|
260
|
+
break;
|
|
261
|
+
case "BYMONTH":
|
|
262
|
+
rule.byMonth = ruleInts(value, key, 1, 12, false);
|
|
263
|
+
break;
|
|
264
|
+
case "WKST":
|
|
265
|
+
if (!RULE_DAYS.includes(value)) throw new RangeError(`parseRRule: WKST=${value} isn't a weekday`);
|
|
266
|
+
rule.weekStart = RULE_DAYS.indexOf(value);
|
|
267
|
+
break;
|
|
268
|
+
default:
|
|
269
|
+
throw new RangeError(`parseRRule: ${key} isn't supported`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return checkRule(rule, "parseRRule");
|
|
273
|
+
}
|
|
274
|
+
var intIn = (n, min, max) => typeof n === "number" && Number.isInteger(n) && n >= min && n <= max;
|
|
275
|
+
function checkRule(rule, where) {
|
|
276
|
+
if (!rule || typeof rule !== "object") throw new RangeError(`${where}: not a rule`);
|
|
277
|
+
if (!rule.freq) throw new RangeError(`${where}: FREQ is required`);
|
|
278
|
+
if (!FREQS.includes(rule.freq)) throw new RangeError(`${where}: FREQ=${String(rule.freq)} isn't supported`);
|
|
279
|
+
if (!intIn(rule.interval, 1, 1e4)) throw new RangeError(`${where}: INTERVAL=${String(rule.interval)} is out of range`);
|
|
280
|
+
if (rule.count !== void 0 && !intIn(rule.count, 1, 1e5)) throw new RangeError(`${where}: COUNT=${String(rule.count)} is out of range`);
|
|
281
|
+
if (rule.count !== void 0 && rule.until !== void 0) throw new RangeError(`${where}: COUNT and UNTIL can't both be set`);
|
|
282
|
+
if (!intIn(rule.weekStart, 0, 6)) throw new RangeError(`${where}: WKST=${String(rule.weekStart)} isn't a weekday`);
|
|
283
|
+
for (const b of rule.byDay ?? []) {
|
|
284
|
+
if (!intIn(b.weekday, 0, 6) || b.nth !== void 0 && (!intIn(Math.abs(b.nth), 1, 53) || b.nth === 0)) throw new RangeError(`${where}: BYDAY is out of range`);
|
|
285
|
+
if (b.nth !== void 0 && rule.freq !== "MONTHLY" && rule.freq !== "YEARLY") {
|
|
286
|
+
throw new RangeError(`${where}: BYDAY with a position (${b.nth}${RULE_DAYS[b.weekday]}) needs FREQ=MONTHLY or YEARLY`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
for (const d of rule.byMonthDay ?? []) if (!intIn(Math.abs(d), 1, 31)) throw new RangeError(`${where}: BYMONTHDAY=${d} is out of range`);
|
|
290
|
+
if (rule.byMonthDay && rule.freq === "WEEKLY") throw new RangeError(`${where}: BYMONTHDAY can't be used with FREQ=WEEKLY`);
|
|
291
|
+
for (const m of rule.byMonth ?? []) if (!intIn(m, 1, 12)) throw new RangeError(`${where}: BYMONTH=${m} is out of range`);
|
|
292
|
+
const until = rule.until;
|
|
293
|
+
if (until !== void 0) {
|
|
294
|
+
const ok = typeof until === "string" ? isDay(until) : until instanceof Date ? !Number.isNaN(until.getTime()) : Boolean(until) && typeof until === "object" && isDay(until.day) && typeof until.time === "string" && validWallTime(until.time);
|
|
295
|
+
if (!ok) throw new RangeError(`${where}: UNTIL isn't a day, an instant or a { day, time } wall time`);
|
|
296
|
+
}
|
|
297
|
+
for (const key of Object.keys(rule)) {
|
|
298
|
+
if (!["freq", "interval", "count", "until", "byDay", "byMonthDay", "byMonth", "weekStart"].includes(key)) throw new RangeError(`${where}: ${key} isn't supported`);
|
|
299
|
+
}
|
|
300
|
+
return rule;
|
|
301
|
+
}
|
|
302
|
+
function weekdayPositions(days, spec) {
|
|
303
|
+
const matching = days.filter((d) => weekdayOf(d) === spec.weekday);
|
|
304
|
+
if (spec.nth === void 0) return matching;
|
|
305
|
+
const pick = spec.nth > 0 ? matching[spec.nth - 1] : matching[matching.length + spec.nth];
|
|
306
|
+
return pick ? [pick] : [];
|
|
307
|
+
}
|
|
308
|
+
function monthDays(y, m) {
|
|
309
|
+
const n = new Date(Date.UTC(y, m, 0)).getUTCDate();
|
|
310
|
+
return Array.from({ length: n }, (_, i) => dayOf(y, m, i + 1));
|
|
311
|
+
}
|
|
312
|
+
function byMonthDays(days, byMonthDay) {
|
|
313
|
+
return byMonthDay.map((n) => n > 0 ? days[n - 1] : days[days.length + n]).filter((d) => d !== void 0);
|
|
314
|
+
}
|
|
315
|
+
function monthCandidates(rule, y, m, startDay, scope) {
|
|
316
|
+
const days = monthDays(y, m);
|
|
317
|
+
if (rule.byMonthDay) {
|
|
318
|
+
const picked = byMonthDays(days, rule.byMonthDay);
|
|
319
|
+
if (!rule.byDay) return picked;
|
|
320
|
+
const allowed = new Set(rule.byDay.flatMap((b) => weekdayPositions(scope ?? days, b)));
|
|
321
|
+
return picked.filter((d2) => allowed.has(d2));
|
|
322
|
+
}
|
|
323
|
+
if (rule.byDay) return rule.byDay.flatMap((b) => weekdayPositions(days, b));
|
|
324
|
+
const d = days[startDay - 1];
|
|
325
|
+
return d ? [d] : [];
|
|
326
|
+
}
|
|
327
|
+
function periodCandidates(rule, period, first) {
|
|
328
|
+
const [py, pm] = assertDay(period, "expandRecurrence");
|
|
329
|
+
let days;
|
|
330
|
+
switch (rule.freq) {
|
|
331
|
+
case "DAILY":
|
|
332
|
+
days = [period];
|
|
333
|
+
if (rule.byMonthDay) days = days.filter((d) => byMonthDays(monthDays(py, pm), rule.byMonthDay).includes(d));
|
|
334
|
+
if (rule.byDay) days = days.filter((d) => rule.byDay.some((b) => b.weekday === weekdayOf(d)));
|
|
335
|
+
break;
|
|
336
|
+
case "WEEKLY": {
|
|
337
|
+
const week = Array.from({ length: 7 }, (_, i) => addDays(period, i));
|
|
338
|
+
days = rule.byDay ? week.filter((d) => rule.byDay.some((b) => b.weekday === weekdayOf(d))) : week.filter((d) => weekdayOf(d) === weekdayOf(dayOf(...first)));
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
case "MONTHLY":
|
|
342
|
+
days = monthCandidates(rule, py, pm, first[2]);
|
|
343
|
+
break;
|
|
344
|
+
case "YEARLY":
|
|
345
|
+
if (rule.byDay && !rule.byMonth && !rule.byMonthDay) {
|
|
346
|
+
const year = Array.from({ length: 12 }, (_, i) => monthDays(py, i + 1)).flat();
|
|
347
|
+
days = rule.byDay.flatMap((b) => weekdayPositions(year, b));
|
|
348
|
+
} else {
|
|
349
|
+
const months = rule.byMonth ?? (rule.byDay || rule.byMonthDay ? [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] : [first[1]]);
|
|
350
|
+
const year = rule.byMonth ? void 0 : Array.from({ length: 12 }, (_, i) => monthDays(py, i + 1)).flat();
|
|
351
|
+
days = months.flatMap((m) => monthCandidates(rule, py, m, first[2], year));
|
|
352
|
+
}
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
if (rule.byMonth) days = days.filter((d) => rule.byMonth.includes(Number(d.slice(5, 7))));
|
|
356
|
+
return [...new Set(days)].sort();
|
|
357
|
+
}
|
|
358
|
+
function periodAt(rule, firstPeriod, k) {
|
|
359
|
+
switch (rule.freq) {
|
|
360
|
+
case "DAILY":
|
|
361
|
+
return addDays(firstPeriod, k);
|
|
362
|
+
case "WEEKLY":
|
|
363
|
+
return addWeeks(firstPeriod, k);
|
|
364
|
+
case "MONTHLY":
|
|
365
|
+
return addMonths(firstPeriod, k);
|
|
366
|
+
case "YEARLY":
|
|
367
|
+
return addMonths(firstPeriod, 12 * k);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
function periodsTo(rule, firstPeriod, day) {
|
|
371
|
+
switch (rule.freq) {
|
|
372
|
+
case "DAILY":
|
|
373
|
+
return daysBetween(firstPeriod, day);
|
|
374
|
+
case "WEEKLY":
|
|
375
|
+
return Math.floor(daysBetween(firstPeriod, day) / 7);
|
|
376
|
+
case "MONTHLY":
|
|
377
|
+
return (Number(day.slice(0, 4)) - Number(firstPeriod.slice(0, 4))) * 12 + Number(day.slice(5, 7)) - Number(firstPeriod.slice(5, 7));
|
|
378
|
+
case "YEARLY":
|
|
379
|
+
return Number(day.slice(0, 4)) - Number(firstPeriod.slice(0, 4));
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function startOf(start) {
|
|
383
|
+
if (typeof start === "string") {
|
|
384
|
+
assertDay(start, "expandRecurrence");
|
|
385
|
+
return { day: start };
|
|
386
|
+
}
|
|
387
|
+
if (!isTimeZone(start.timeZone)) throw new RangeError(`expandRecurrence: unknown time zone "${start.timeZone}"`);
|
|
388
|
+
if ("at" in start) return { ...wallTimeIn(start.timeZone, start.at), zone: start.timeZone };
|
|
389
|
+
assertDay(start.day, "expandRecurrence");
|
|
390
|
+
parseWallTime(start.time, "expandRecurrence");
|
|
391
|
+
return { day: start.day, time: start.time, zone: start.timeZone };
|
|
392
|
+
}
|
|
393
|
+
function expandRecurrence(options) {
|
|
394
|
+
const rule = typeof options.rule === "string" ? parseRRule(options.rule) : checkRule(options.rule, "expandRecurrence");
|
|
395
|
+
const given = options.start;
|
|
396
|
+
const { day: firstDay, time, zone } = startOf(given);
|
|
397
|
+
const { window } = options;
|
|
398
|
+
if (!window) throw new RangeError("expandRecurrence: a window is required");
|
|
399
|
+
if (options.limit !== void 0 && (!Number.isInteger(options.limit) || options.limit < 1)) {
|
|
400
|
+
throw new RangeError(`expandRecurrence: limit ${options.limit} is not a whole number, 1 or more`);
|
|
401
|
+
}
|
|
402
|
+
assertDay(window.start, "expandRecurrence");
|
|
403
|
+
assertDay(window.end, "expandRecurrence");
|
|
404
|
+
const limit = options.limit ?? 1e3;
|
|
405
|
+
const first = assertDay(firstDay, "expandRecurrence");
|
|
406
|
+
const timed = Boolean(time && zone);
|
|
407
|
+
const givenAt = typeof given === "object" && "at" in given ? given.at : void 0;
|
|
408
|
+
const ms = givenAt ? (givenAt.getTime() % 1e3 + 1e3) % 1e3 : 0;
|
|
409
|
+
const firstAt = givenAt ?? (timed ? instantIn(firstDay, time, zone) : void 0);
|
|
410
|
+
const at = (day) => day === firstDay ? firstAt : timed ? new Date(instantIn(day, time, zone).getTime() + ms) : void 0;
|
|
411
|
+
const exDays = /* @__PURE__ */ new Set();
|
|
412
|
+
const exInstants = /* @__PURE__ */ new Set();
|
|
413
|
+
for (const ex of options.exdates ?? []) {
|
|
414
|
+
if (ex instanceof Date) {
|
|
415
|
+
if (!timed) throw new RangeError("expandRecurrence: an all-day series' EXDATE is a day, not an instant");
|
|
416
|
+
exInstants.add(ex.getTime());
|
|
417
|
+
} else {
|
|
418
|
+
assertDay(ex, "expandRecurrence");
|
|
419
|
+
exDays.add(ex);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
const until = rule.until;
|
|
423
|
+
const untilAt = until === void 0 || typeof until === "string" ? void 0 : until instanceof Date ? until : timed ? instantIn(until.day, until.time, zone) : void 0;
|
|
424
|
+
const untilDay = typeof until === "string" ? until : until instanceof Date ? timed ? void 0 : dayIn("UTC", until) : until && !timed ? until.day : void 0;
|
|
425
|
+
const pastUntil = (day) => {
|
|
426
|
+
if (untilDay !== void 0) return day > untilDay;
|
|
427
|
+
if (untilAt !== void 0) return at(day).getTime() > untilAt.getTime();
|
|
428
|
+
return false;
|
|
429
|
+
};
|
|
430
|
+
const firstPeriod = rule.freq === "WEEKLY" ? startOfWeek(firstDay, rule.weekStart) : rule.freq === "DAILY" ? firstDay : rule.freq === "MONTHLY" ? startOfMonth(firstDay) : dayOf(first[0], 1, 1);
|
|
431
|
+
const out = [];
|
|
432
|
+
let truncated = false;
|
|
433
|
+
const done = () => ({ occurrences: out, truncated });
|
|
434
|
+
let counted = 0;
|
|
435
|
+
const take = (day) => {
|
|
436
|
+
counted++;
|
|
437
|
+
if (day >= window.start && day < window.end && !exDays.has(day)) {
|
|
438
|
+
const start = at(day);
|
|
439
|
+
if (!(start && exInstants.has(start.getTime()))) {
|
|
440
|
+
if (out.length >= limit) {
|
|
441
|
+
truncated = true;
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
out.push(start ? { day, start } : { day });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return rule.count !== void 0 && counted >= rule.count;
|
|
448
|
+
};
|
|
449
|
+
if (pastUntil(firstDay)) return done();
|
|
450
|
+
if (take(firstDay)) return done();
|
|
451
|
+
let k = 0;
|
|
452
|
+
if (rule.count === void 0 && window.start > firstDay) {
|
|
453
|
+
const behind = periodsTo(rule, firstPeriod, window.start) - 1;
|
|
454
|
+
k = Math.max(0, Math.floor(behind / rule.interval) * rule.interval);
|
|
455
|
+
}
|
|
456
|
+
let lastYear = first[0];
|
|
457
|
+
for (; ; k += rule.interval) {
|
|
458
|
+
const period = periodAt(rule, firstPeriod, k);
|
|
459
|
+
if (period.length > 10 || period >= window.end) break;
|
|
460
|
+
const periodYear = Number(period.slice(0, 4));
|
|
461
|
+
if (periodYear - lastYear > 400) break;
|
|
462
|
+
const days = periodCandidates(rule, period, first).filter((d) => d > firstDay);
|
|
463
|
+
if (days.length) lastYear = periodYear;
|
|
464
|
+
for (const day of days) {
|
|
465
|
+
if (pastUntil(day)) return done();
|
|
466
|
+
if (day >= window.end && rule.count === void 0) return done();
|
|
467
|
+
if (take(day)) return done();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return done();
|
|
471
|
+
}
|
|
472
|
+
var STORED = /^(\d{4}-\d{2}-\d{2})(?:[ T](\d{2}:\d{2}(?::\d{2})?)(\.\d+)?)?\s*(Z|[+-]\d{2}(?::?\d{2})?)?$/;
|
|
473
|
+
function readEventTime(value, options = {}) {
|
|
474
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? null : value;
|
|
475
|
+
if (typeof value !== "string") return null;
|
|
476
|
+
const m = STORED.exec(value.trim());
|
|
477
|
+
if (!m || !isDay(m[1])) return null;
|
|
478
|
+
if (options.dateOnly || !m[2]) return m[1];
|
|
479
|
+
const time = m[2].length === 5 ? `${m[2]}:00` : m[2];
|
|
480
|
+
if (!m[4]) {
|
|
481
|
+
const zone2 = options.timeZone && isTimeZone(options.timeZone) ? options.timeZone : deviceTimeZone();
|
|
482
|
+
const [h, mi, sec] = time.split(":").map(Number);
|
|
483
|
+
if (!WALL_TIME.test(time) || h > 23 || mi > 59 || sec > 59) return null;
|
|
484
|
+
const at = instantIn(m[1], time, zone2);
|
|
485
|
+
return m[3] ? new Date(at.getTime() + Math.round(Number(`0${m[3]}`) * 1e3)) : at;
|
|
486
|
+
}
|
|
487
|
+
let zone = m[4];
|
|
488
|
+
if (/^[+-]\d{2}$/.test(zone)) zone += ":00";
|
|
489
|
+
else if (/^[+-]\d{4}$/.test(zone)) zone = `${zone.slice(0, 3)}:${zone.slice(3)}`;
|
|
490
|
+
const ms = m[3] ? m[3].slice(0, 4).padEnd(4, "0") : "";
|
|
491
|
+
const date = /* @__PURE__ */ new Date(`${m[1]}T${time}${ms}${zone}`);
|
|
492
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export { addDays, addMonths, addWeeks, dayIn, dayRangeIn, daysBetween, daysInMonth, deviceTimeZone, eachDay, expandRecurrence, instantIn, isDay, isTimeZone, monthGridRange, monthRange, parseRRule, personDay, personTimeZone, quarterRange, rangeIn, readEventTime, startOfDayIn, startOfMonth, startOfQuarter, startOfWeek, wallClock, wallTimeIn, weekdayOf, weeksRange };
|
|
496
|
+
//# sourceMappingURL=chunk-227N654S.js.map
|
|
497
|
+
//# sourceMappingURL=chunk-227N654S.js.map
|