@moltenship/calendar 1.0.0-rc.2 → 2.0.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.
Files changed (78) hide show
  1. package/README.md +27 -6
  2. package/dist/index.d.mts +3 -3
  3. package/dist/index.mjs +2 -2
  4. package/docs/accessibility.md +305 -0
  5. package/docs/agents/changes.md +449 -0
  6. package/docs/agents/coding-agents.md +116 -0
  7. package/docs/agents/connect.md +444 -0
  8. package/docs/agents/deleting.md +296 -0
  9. package/docs/agents/reference.md +511 -0
  10. package/docs/agents/server.md +317 -0
  11. package/docs/agents/webmcp.md +153 -0
  12. package/docs/agents.md +355 -0
  13. package/docs/annotations.md +198 -0
  14. package/docs/api/changes.md +264 -0
  15. package/docs/api/components.md +245 -0
  16. package/docs/api/dnd-kit.md +265 -0
  17. package/docs/api/events.md +153 -0
  18. package/docs/api/primitives.md +218 -0
  19. package/docs/api/store.md +92 -0
  20. package/docs/api.md +57 -0
  21. package/docs/availability.md +576 -0
  22. package/docs/comparison.md +278 -0
  23. package/docs/concepts/anatomy.md +73 -0
  24. package/docs/concepts/events.md +163 -0
  25. package/docs/concepts/how-it-works.md +112 -0
  26. package/docs/concepts/state.md +244 -0
  27. package/docs/concepts/time.md +168 -0
  28. package/docs/customization.md +271 -0
  29. package/docs/frameworks.md +116 -0
  30. package/docs/guides/dnd-kit.md +333 -0
  31. package/docs/guides/google-calendar-sync.md +1246 -0
  32. package/docs/guides/google-calendar.md +367 -0
  33. package/docs/guides/motion.md +414 -0
  34. package/docs/guides/nextjs-app-router.md +630 -0
  35. package/docs/guides/nextjs.md +149 -0
  36. package/docs/guides/performance.md +103 -0
  37. package/docs/guides/tanstack-query.md +485 -0
  38. package/docs/ics/feed.md +484 -0
  39. package/docs/ics/import.md +183 -0
  40. package/docs/ics/reference.md +482 -0
  41. package/docs/ics.md +142 -0
  42. package/docs/index.md +149 -0
  43. package/docs/installation.md +209 -0
  44. package/docs/interactions.md +441 -0
  45. package/docs/llms.txt +99 -0
  46. package/docs/migrating-to-2.md +497 -0
  47. package/docs/quick-start.md +163 -0
  48. package/docs/recurrence/calendar.md +598 -0
  49. package/docs/recurrence/model.md +128 -0
  50. package/docs/recurrence/reference.md +547 -0
  51. package/docs/recurrence.md +122 -0
  52. package/docs/stylex.md +290 -0
  53. package/docs/temporal/daylight-saving-time.md +172 -0
  54. package/docs/temporal/month-grid.md +119 -0
  55. package/docs/temporal/reference.md +167 -0
  56. package/docs/temporal.md +105 -0
  57. package/docs/testing.md +600 -0
  58. package/docs/theme-builder.md +82 -0
  59. package/docs/theming.md +275 -0
  60. package/docs/undo-redo.md +347 -0
  61. package/docs/views/agenda.md +222 -0
  62. package/docs/views/custom-views.md +195 -0
  63. package/docs/views/month.md +110 -0
  64. package/docs/views/resource-columns.md +265 -0
  65. package/docs/views/resource-timeline.md +226 -0
  66. package/docs/views/scrolling.md +193 -0
  67. package/docs/views/week-and-day.md +258 -0
  68. package/docs/views/year.md +138 -0
  69. package/docs/views.md +69 -0
  70. package/package.json +42 -14
  71. package/skills/moltenship-time/SKILL.md +77 -0
  72. package/skills/moltenship-time/references/agent-ready.md +272 -0
  73. package/skills/moltenship-time/references/changes.md +188 -0
  74. package/skills/moltenship-time/references/compose.md +175 -0
  75. package/skills/moltenship-time/references/mistakes.md +100 -0
  76. package/skills/moltenship-time/references/solid.md +213 -0
  77. package/skills/moltenship-time/references/state.md +129 -0
  78. package/skills/moltenship-time/references/testing.md +176 -0
@@ -0,0 +1,547 @@
1
+ # API reference
2
+
3
+ Everything @moltenship/calendar-recurrence exports. Series and adapters, expansion, occurrence IDs, rules, the ICS bridge and edits.
4
+
5
+ Source: https://time.molten.sh/docs/react/recurrence/reference
6
+
7
+ Everything on this page is exported from `@moltenship/calendar-recurrence`. The
8
+ [Overview](../recurrence.md) has the install command and a first example, and
9
+ [Series and occurrences](./model.md) explains the model.
10
+
11
+ [Show and edit recurring events](./calendar.md) wires the package into a calendar.
12
+
13
+ ## Series
14
+
15
+ A series is a master event, which sits at the series' first occurrence, and any number of override
16
+ events. The package reads both through a [`RecurrenceAdapter`](#adapters). Events with neither are
17
+ one-off events, and pass through.
18
+
19
+ Every date of a series has the kind of its first occurrence. That's a `RecurrenceDate`:
20
+
21
+ | The series is | Its dates are | Its occurrence IDs end in |
22
+ | ------------- | ------------------------ | ------------------------- |
23
+ | Timed | `Temporal.Instant` | `2026-10-28T08:00:00Z` |
24
+ | All-day | `Temporal.PlainDate` | `2026-10-28` |
25
+ | Floating | `Temporal.PlainDateTime` | `2026-10-28T09:00:00` |
26
+
27
+ A date of another kind is converted with [`coerceRecurrenceDate`](#dates-of-another-kind), or
28
+ dropped with an issue when it can't be.
29
+
30
+ ### EventRecurrence
31
+
32
+ An `EventRecurrence` is what makes an event a series' master: its RRULE, EXDATE, RDATE and
33
+ DURATION.
34
+
35
+ | Name | Type | Description |
36
+ | ----------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
37
+ | `rule` | `RecurrenceRule` | RRULE. A series without one has only its first occurrence and its `recurrenceDates`. |
38
+ | `exceptionDates` | `readonly RecurrenceDate[]` | EXDATE: original starts of occurrences that don't happen, the first one included. A `PlainDate` on a timed or floating series excludes every occurrence that starts that day. Excluded occurrences still count toward COUNT. |
39
+ | `recurrenceDates` | `readonly (RecurrenceDate \| RecurrencePeriod)[]` | RDATE: extra occurrences. A date starts one that lasts as long as the first occurrence. A `RecurrencePeriod`, with a `start` and an `end`, keeps its own length. |
40
+ | `duration` | `Temporal.Duration` | DURATION, such as `P1D`, which keeps each occurrence's wall-clock length across DST. Without it, occurrences last exactly as long as the first. |
41
+ | `wallStart` | `Temporal.PlainDateTime` | A timed series' first start as written, when it fell in a DST gap and so resolved later. Later occurrences repeat at this wall time. Ignored unless it resolves to the first start. ICS gives it as `wallTime`. |
42
+
43
+ A `RecurrencePeriod` is an extra occurrence with its own `start` and `end`, both in the series'
44
+ kind.
45
+
46
+ `RecurrenceRule` is `@moltenship/ics`'s [`IcsRecurrenceRule`](../ics/reference.md#recurrence), with
47
+ `until` as a `RecurrenceDate`: `frequency`, `interval`, `count`, `until`, `byDay`, `byMonth`,
48
+ `byMonthDay`, `bySetPosition`, `weekStart` and the rest. UNTIL is inclusive. With COUNT too, the
49
+ series ends at whichever comes first.
50
+
51
+ ### RecurrenceOverride
52
+
53
+ A `RecurrenceOverride` is what makes an event replace one occurrence of a series, as RECURRENCE-ID
54
+ does.
55
+
56
+ | Name | Type | Default | Description |
57
+ | -------------- | ---------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
58
+ | `seriesId` | `TEventId` | | Required. The master's ID. |
59
+ | `recurrenceId` | `RecurrenceDate` | | Required. The original start of the occurrence it replaces, in the series' kind. The override keeps it when it moves, and so keeps the occurrence's ID. |
60
+ | `cancelled` | `boolean` | `false` | The occurrence doesn't happen, as Google's cancelled instances and ICS overrides with STATUS:CANCELLED say. It hides the occurrence and doesn't show itself. |
61
+
62
+ An event that is both a master and an override is read as the override, with a
63
+ `conflicting-recurrence` issue.
64
+
65
+ ### RecurringCalendarEvent
66
+
67
+ `RecurringCalendarEvent<TData, TEventId, TResourceId>` is core's [`CalendarEvent`](../concepts/events.md#records-in-times-own-shape)
68
+ with an optional `recurrence: EventRecurrence` and `override: RecurrenceOverride`. Use it when your
69
+ app has no event type of its own, with [`createCanonicalRecurrenceAdapter`](#adapters). It's also
70
+ what [`recurringEventsFromIcs`](#ics-bridge) returns and
71
+ [`applyRecurrenceEdit`](#applyrecurrenceedit) changes.
72
+
73
+ ## Adapters
74
+
75
+ A `RecurrenceAdapter` reads recurrence from your events, beside the `EventAdapter` that reads their
76
+ IDs, titles and intervals. A master's `getInterval` is its first occurrence, as DTSTART and DTEND
77
+ are. Timed series repeat on the wall clock of the interval's `sourceTimeZone`.
78
+
79
+ | Name | Type | Description |
80
+ | --------------- | -------------------------------------------- | ---------------------------------------------------------------------------- |
81
+ | `getRecurrence` | `(event) => EventRecurrence \| undefined` | Required. The recurrence of a series' master; undefined for any other event. |
82
+ | `getOverride` | `(event) => RecurrenceOverride \| undefined` | Which occurrence an override replaces; undefined for any other event. |
83
+
84
+ `RecurrenceAdapters<TEvent, TEventId, TResourceId>` holds both: `{ event, recurrence }`. The
85
+ expander and the edit functions take it.
86
+
87
+ - `createCanonicalRecurrenceAdapter<TData, TEventId, TResourceId>()` returns the adapters for
88
+ `RecurringCalendarEvent`: core's `createCanonicalEventAdapter`, and a recurrence adapter that reads
89
+ `recurrence` and `override`.
90
+ - `createOccurrenceAdapter(eventAdapter)` returns the `EventAdapter` you give the calendar, whose
91
+ events are occurrences. IDs and intervals come from the occurrence. Titles, resources, read-only
92
+ and busy state come from the event it shows, through `eventAdapter`. `isBusy` and `isReadOnly`
93
+ exist only when `eventAdapter` has them, so core's defaults still apply.
94
+
95
+ ```ts
96
+ import {
97
+ createCanonicalRecurrenceAdapter,
98
+ createOccurrenceAdapter,
99
+ type RecurrenceOccurrence,
100
+ type RecurringCalendarEvent,
101
+ } from "@moltenship/calendar-recurrence";
102
+
103
+ type Event = RecurringCalendarEvent<{ color: string }>;
104
+ type Occurrence = RecurrenceOccurrence<Event, string>;
105
+ const adapters = createCanonicalRecurrenceAdapter<{ color: string }>();
106
+
107
+ // `createCalendar<Occurrence>()` takes this as its `eventAdapter`.
108
+ const eventAdapter = createOccurrenceAdapter(adapters.event);
109
+ ```
110
+
111
+ An occurrence's revision combines the revisions of its event and its series' master with its
112
+ interval. Without `getRevision` on your adapter, an event's revision is its object identity, so
113
+ replace events rather than changing them.
114
+
115
+ ## Expansion
116
+
117
+ `createOccurrenceExpander(adapters, options)` returns an `OccurrenceExpander`.
118
+ `expander.expand(events, range)` returns the occurrences of `events` that overlap `range`:
119
+
120
+ ```ts
121
+ import { createOccurrenceExpander } from "@moltenship/calendar-recurrence";
122
+
123
+ const expander = createOccurrenceExpander(adapters, { defaultTimeZone: "Europe/Berlin" });
124
+
125
+ const { occurrences, issues } = expander.expand(events, {
126
+ range: visibleRange.loadRange,
127
+ timeZone: "Europe/Berlin",
128
+ });
129
+ ```
130
+
131
+ Create one expander and keep it, in `useState` or at module scope. It reuses its work for events
132
+ that are the same objects as last time: each series keeps its last four ranges and eight rule
133
+ windows, and an occurrence that comes out the same keeps its object, so memoized views don't
134
+ render again. Treat events as immutable.
135
+
136
+ `options` is an `OccurrenceExpanderOptions`:
137
+
138
+ | Name | Type | Default | Description |
139
+ | ----------------- | -------- | ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
140
+ | `defaultTimeZone` | `string` | `"UTC"` | Where timed series without `sourceTimeZone` repeat, and the zone instants are read in for all-day and floating series. Weekdays and DST follow it. A zone that isn't one reports `invalid-time-zone` for each date that needs it. |
141
+ | `maxIterations` | `number` | `DEFAULT_MAX_ITERATIONS (100,000)` | Most loop steps one series may take per expansion. A series that runs out reports `iteration-limit`. Must be a positive integer, or `createOccurrenceExpander` throws a `RangeError`. |
142
+
143
+ ### OccurrenceRange
144
+
145
+ `expand` takes the dates as an `OccurrenceRange`:
146
+
147
+ | Name | Type | Description |
148
+ | ---------- | -------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
149
+ | `range` | `{ start: Temporal.PlainDate; end: Temporal.PlainDate }` | Required. Half-open. Pass the `loadRange` that `onVisibleRangeChange` reports, not its `range`, so the pages a scrolling view keeps ready have their occurrences. |
150
+ | `timeZone` | `string` | Required. The calendar's time zone. It places the range's dates for timed series; all-day series compare dates and floating ones wall times. |
151
+
152
+ An occurrence overlapping the range is included, even one that starts before it. Overrides and
153
+ one-off events pass through whatever the range, since your app chose to load them.
154
+
155
+ ### OccurrenceExpansion
156
+
157
+ `expand` returns an `OccurrenceExpansion`: `{ occurrences, issues }`. `occurrences` holds a `RecurrenceOccurrence` for each
158
+ occurrence in the range, each override that isn't cancelled, and each one-off event. They come in
159
+ the input order of their events, and each series' in time order. IDs are unique. Each one has:
160
+
161
+ | Name | Type | Description |
162
+ | ------------ | ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
163
+ | `id` | `TEventId \| string` | Required. `occurrenceId(seriesId, recurrenceId)` for an occurrence or an override, and the event's own ID for a one-off event. See [Occurrence IDs](#occurrence-ids). |
164
+ | `interval` | `CalendarEventInterval` | Required. Where it is: the generated start and the series' length, or the override's own interval. |
165
+ | `event` | `TEvent` | Required. The event it shows: the override, the series' master, or the one-off event. |
166
+ | `recurrence` | `OccurrenceRecurrence` | Its place in its series. Absent on one-off events. |
167
+
168
+ `OccurrenceRecurrence` has the `seriesId`, the `recurrenceId` (the original start, which names the
169
+ occurrence and stays put when it moves), the `series` master and the `override` that replaces the
170
+ occurrence, when there is one. `series` is absent for an override whose master isn't among the
171
+ events, such as an invitation to one occurrence. It keeps its ID, so it matches once the master
172
+ loads.
173
+
174
+ ### Issues
175
+
176
+ Bad data becomes an issue, with the event and its position in the input, rather than an
177
+ exception. A problem with one date drops only that date; the series stays.
178
+
179
+ | `code` | Meaning |
180
+ | ------------------------ | ------------------------------------------------------------------------------------------------------------------ |
181
+ | `adapter-error` | An adapter function threw. The event is left out, and `message` names the function. |
182
+ | `date-kind-mismatch` | A date that `coerceRecurrenceDate` can't bring into the series' kind, such as a date-only RDATE on a timed series. |
183
+ | `conflicting-recurrence` | An event is both a master and an override. It's read as the override. |
184
+ | `duplicate-event-id` | Two items have the same ID, such as series `42` and `"42"`. The later one is left out. |
185
+ | `duplicate-override` | Two overrides replace the same occurrence. The later one in the input wins. |
186
+ | `invalid-rule` | The rule can't be expanded. The series still shows its first occurrence and its RDATEs. |
187
+ | `invalid-time-zone` | `sourceTimeZone` or `defaultTimeZone` isn't a time zone. Dates that need it are dropped. |
188
+ | `iteration-limit` | A series ran out of `maxIterations` before it finished the range. |
189
+ | `missing-series` | An override's master isn't among the events. It shows on its own, under its occurrence ID. |
190
+ | `unsupported-rule` | A valid RFC 5545 construct the engine doesn't handle, such as a sub-daily FREQ on an all-day series. |
191
+
192
+ Each `RecurrenceIssue` has its `code`, the `event`, its `eventId` when the adapter could read it, its
193
+ `index` in the input, and a `message`.
194
+
195
+ ## Occurrence IDs
196
+
197
+ ```ts
198
+ import { occurrenceId, parseOccurrenceId, recurrenceKey } from "@moltenship/calendar-recurrence";
199
+
200
+ occurrenceId("standup", Temporal.Instant.from("2026-10-28T08:00:00Z"));
201
+ // "standup::2026-10-28T08:00:00Z"
202
+ occurrenceId(42, Temporal.PlainDate.from("2026-10-28")); // "42::2026-10-28"
203
+
204
+ recurrenceKey(Temporal.Instant.from("2026-10-28T09:00:00+01:00")); // "2026-10-28T08:00:00Z"
205
+
206
+ parseOccurrenceId("standup::2026-10-28T08:00:00Z");
207
+ // { seriesId: "standup", recurrenceKey: "2026-10-28T08:00:00Z" }
208
+ parseOccurrenceId("standup"); // undefined
209
+ ```
210
+
211
+ - `occurrenceId(seriesId, recurrenceId)` is `<seriesId>::<recurrenceKey>`: the series and the
212
+ occurrence's original start. It stays the same across views, time zones and reloads, and when
213
+ an override moves the occurrence. Edits to the following occurrences or to all of them change
214
+ starts, and so IDs.
215
+ - `recurrenceKey(recurrenceId)` is the original start in ISO form: instants in UTC, dates and wall
216
+ times in the ISO calendar, without a calendar annotation. The format is stable, since apps store
217
+ IDs.
218
+ - `parseOccurrenceId(id)` splits an ID into the series ID, as a string, and the key, or returns
219
+ `undefined` for an ID that doesn't end in a key. Use it only for IDs you stored; an occurrence's
220
+ `recurrence` has the series ID with its type.
221
+
222
+ ## Rules
223
+
224
+ ### RRULE text
225
+
226
+ `parseRecurrenceRule(text)` reads an RRULE as a RECUR value (`FREQ=WEEKLY;BYDAY=MO`) or a content
227
+ line (`RRULE:FREQ=WEEKLY;BYDAY=MO`), as the Google Calendar API stores it. It never throws:
228
+
229
+ ```ts
230
+ import { formatRecurrenceRule, parseRecurrenceRule } from "@moltenship/calendar-recurrence";
231
+
232
+ parseRecurrenceRule("RRULE:FREQ=WEEKLY;BYDAY=MO,WE;UNTIL=20261231T225959Z");
233
+ // { rule: { frequency: "WEEKLY", byDay: [{ weekday: "MO" }, { weekday: "WE" }],
234
+ // until: Temporal.Instant 2026-12-31T22:59:59Z }, issues: [] }
235
+
236
+ parseRecurrenceRule("INTERVAL=2");
237
+ // { issues: [{ severity: "error", code: "invalid-value",
238
+ // message: "\"INTERVAL=2\" has no FREQ, which a recurrence rule requires" }] }
239
+
240
+ formatRecurrenceRule({ frequency: "MONTHLY", byDay: [{ weekday: "TU", ordinal: 2 }], count: 6 });
241
+ // "FREQ=MONTHLY;COUNT=6;BYDAY=2TU"
242
+ ```
243
+
244
+ - `parseRecurrenceRule` returns a `RecurrenceRuleTextResult`: `rule`, absent when the text holds
245
+ no usable rule, and `issues`. Each `RecurrenceRuleTextIssue` has a `severity`, the `code`
246
+ `@moltenship/ics` reports it under, and a `message`. Errors leave no rule. Warnings keep a
247
+ best-effort one, as when an invalid BYDAY is dropped.
248
+ - UNTIL becomes a `PlainDate` for a DATE, an `Instant` for a UTC or zoned DATE-TIME, and a
249
+ `PlainDateTime` for a floating one.
250
+ - `formatRecurrenceRule(rule)` writes the RECUR value, without `RRULE:`, its parts in RFC 5545's
251
+ order. An `Instant` UNTIL is written in UTC and rounded down to the second. It throws a
252
+ `RangeError` for an UNTIL outside the years 0 to 9999.
253
+ - `recurrenceRuleFromIcs(icsRule)` converts `IcsEvent.recurrenceRule` from `@moltenship/ics`, and
254
+ returns a `RecurrenceRuleTextResult`. An UNTIL whose TZID resolved to no time zone is an error,
255
+ since the series' end would be unknown. `recurrenceRuleToIcs(rule)` converts back.
256
+
257
+ ### The engine
258
+
259
+ `expandRecurrenceRule(rule, first, window)` lists a rule's starts. The expander calls it for each
260
+ series; call it yourself to list starts without events:
261
+
262
+ ```ts
263
+ import { expandRecurrenceRule } from "@moltenship/calendar-recurrence";
264
+
265
+ const start = Temporal.ZonedDateTime.from("2026-10-19T09:00[Europe/Berlin]");
266
+ const { dates, truncated } = expandRecurrenceRule(
267
+ { frequency: "WEEKLY", byDay: [{ weekday: "MO" }] },
268
+ {
269
+ kind: "timed",
270
+ start: start.toInstant(),
271
+ end: start.add({ minutes: 30 }).toInstant(),
272
+ sourceTimeZone: "Europe/Berlin",
273
+ },
274
+ {
275
+ from: Temporal.Instant.from("2026-10-20T00:00Z"),
276
+ before: Temporal.Instant.from("2026-11-10T00:00Z"),
277
+ },
278
+ );
279
+ // dates: 2026-10-26T08:00:00Z, 2026-11-02T08:00:00Z, 2026-11-09T08:00:00Z
280
+ // (09:00 in Berlin each Monday, after the switch to winter time); truncated: false
281
+ ```
282
+
283
+ It covers RFC 5545: every FREQ, every BY part, BYSETPOS, WKST, COUNT and UNTIL. `first` is a
284
+ `RecurrenceRuleStart`: the first occurrence's interval, with an optional `wallStart`. The first
285
+ occurrence is always one and counts toward COUNT. Dates that don't exist, such as 30 February,
286
+ are skipped and don't count. `window` is a `RecurrenceRuleWindow`:
287
+
288
+ | Name | Type | Default | Description |
289
+ | --------------- | ---------------- | ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------- |
290
+ | `from` | `RecurrenceDate` | | Leaves out starts before this. COUNT still counts them; without COUNT, the engine skips ahead instead of stepping through every earlier period. |
291
+ | `before` | `RecurrenceDate` | | Stops before starts at or after this. |
292
+ | `maxIterations` | `number` | `DEFAULT_MAX_ITERATIONS` | Most steps to take. A step is one period the engine visits, whether or not it gives a start; periods skipped to reach from don't count. |
293
+
294
+ It returns a `RecurrenceRuleExpansion`: `dates`, strictly increasing in the kind of the first
295
+ occurrence and the ISO calendar, and `truncated`, which is `true` when `maxIterations` ran out while
296
+ the rule may still have had starts to give. `DEFAULT_MAX_ITERATIONS` is 100,000.
297
+
298
+ A rule that can never match again ends after its first occurrence, without `truncated`, when the
299
+ engine can tell up front: day parts that meet on no date, such as `BYMONTH=2;BYMONTHDAY=30`, and an
300
+ INTERVAL whose steps never reach the BY values of a fixed cycle, such as
301
+ `FREQ=MINUTELY;INTERVAL=2;BYMINUTE=1` from an even minute. The cycles are the second, minute and
302
+ hour of the day, the weekday of a DAILY rule and the month of a MONTHLY one. Other impossible
303
+ rules, such as `FREQ=MONTHLY;BYDAY=MO;BYSETPOS=6`, show only over time: they run to
304
+ `maxIterations` and report `truncated`.
305
+
306
+ For a rule it can't expand, or a `sourceTimeZone` that isn't a time zone, it throws a
307
+ `RecurrenceRuleError`, a `RangeError` whose `code` is a `RecurrenceRuleErrorCode`:
308
+ `invalid-rule`, `invalid-time-zone` or `unsupported-rule`. The expander reports the same code as an
309
+ issue. Window dates of another kind than `first`, and a `maxIterations` that isn't a positive
310
+ integer, throw a plain `RangeError`.
311
+
312
+ ### Dates of another kind
313
+
314
+ Real feeds mix kinds: a UTC UNTIL on an all-day series, a date-only EXDATE on a timed one.
315
+ `coerceRecurrenceDate(date, first, role, options)` brings a date into the kind of the series' first
316
+ occurrence. It's the one place dates convert, and the expander and the ICS bridge use it.
317
+
318
+ | On a series that is | A `PlainDate` | A `PlainDateTime` | An `Instant` |
319
+ | ------------------- | ------------------------------------------------------------------------------------------- | ------------------------------- | ---------------------------------- |
320
+ | Timed | `"exception"`: stays, the whole day. `"until"`: the end of that day. `"start"`: `undefined` | its instant in the series' zone | itself |
321
+ | All-day | itself | its date | its date in `defaultTimeZone` |
322
+ | Floating | `"exception"`: stays. `"until"`: the end of that day. `"start"`: `undefined` | itself | its wall time in `defaultTimeZone` |
323
+
324
+ `role` is a `RecurrenceDateRole`: `"exception"` for EXDATE, `"start"` for RDATE and
325
+ RECURRENCE-ID, `"until"` for UNTIL. Wall times resolve with `compatible`, as RFC 5545 §3.3.5 says.
326
+ `options` is a `CoerceRecurrenceDateOptions` with a `defaultTimeZone`, UTC by default, for timed
327
+ series without `sourceTimeZone` and for instants on the other kinds. `undefined` means the date
328
+ can't stand for its role; report it and drop only that date.
329
+
330
+ ## ICS bridge
331
+
332
+ `recurringEventsFromIcs(events, options)` groups the VEVENTs that `@moltenship/ics` parsed into
333
+ masters, overrides and one-off events, as `RecurringCalendarEvent`s whose `data` is the `IcsEvent`:
334
+
335
+ ```ts
336
+ import {
337
+ createCanonicalRecurrenceAdapter,
338
+ createOccurrenceExpander,
339
+ recurringEventsFromIcs,
340
+ } from "@moltenship/calendar-recurrence";
341
+ import { parseIcs, type IcsEvent } from "@moltenship/ics";
342
+
343
+ const calendar = parseIcs(text).calendars[0]!;
344
+ const { events, issues } = recurringEventsFromIcs(calendar.events, {
345
+ timeZone: calendar.timeZone,
346
+ });
347
+ const expander = createOccurrenceExpander(createCanonicalRecurrenceAdapter<IcsEvent>());
348
+ ```
349
+
350
+ - Masters and one-off events keep their UID as their ID. An override gets the ID of the occurrence
351
+ it replaces, `occurrenceId(uid, recurrenceId)`, with or without its master.
352
+ - A UTC DTSTART repeats in UTC, and a zoned one in its zone. A DTSTART in a DST gap keeps its
353
+ written time as `recurrence.wallStart`. It moves forward by the gap, as RFC 5545 says; a series
354
+ with DTEND in the same zone keeps the length written between them, and a single event its exact
355
+ length, unless the gap leaves it none. DURATION without DTEND becomes `recurrence.duration`.
356
+ - With `X-MICROSOFT-CDO-ALLDAYEVENT:TRUE`, an event written from midnight to a later midnight, or
357
+ from midnight without an end, as older Outlook and Exchange exports write all-day events, is
358
+ all-day on the dates written, even when its TZID resolves to no zone. Its series' midnight
359
+ EXDATE, UNTIL and RECURRENCE-ID values are those dates. UTC ones read in its DTSTART's zone (UTC
360
+ for a UTC one), else in `timeZone`.
361
+ - EXDATE, RDATE, UNTIL and RECURRENCE-ID take the series' kind through `coerceRecurrenceDate`.
362
+ - A cancelled master or one-off event is left out, with the master's overrides. A cancelled
363
+ override comes out with `override.cancelled`, which hides its occurrence.
364
+ - Of events with the same UID, or UID and RECURRENCE-ID, the highest SEQUENCE wins, then the later
365
+ one.
366
+ - RANGE=THISANDFUTURE, as Apple's "this and future" changes write it, applies to the occurrence it
367
+ names and every later one (RFC 5545 §3.8.4.4). The series splits as a "this and following" edit
368
+ splits it: it ends before the occurrence, and a new series, with the override's ID, VEVENT and
369
+ title, starts where the override is, the rest of the rule moved by the same difference. Later
370
+ EXDATEs, RDATEs and overrides move to it, and overrides take the IDs of the occurrences they then
371
+ replace. Later occurrences are keyed by their new starts, so their `recurrenceId`s aren't the
372
+ feed's RECURRENCE-IDs. On the first occurrence the whole series changes instead. A cancelled one
373
+ ends the series there, and later overrides stay. When the rule can't follow, as a BYSETPOS rule
374
+ moved to another day can't, the override replaces only the occurrence it names, with an
375
+ `unsupported-rule` issue.
376
+
377
+ `options` is an `IcsRecurringEventsOptions`:
378
+
379
+ | Name | Type | Default | Description |
380
+ | ----------------- | ----------------------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
381
+ | `timeZone` | `string` | `the feed's most used TZID, else UTC` | The feed's time zone: `calendar.timeZone` (X-WR-TIMEZONE), or one you choose. UTC times on all-day and floating series, such as an UNTIL, are read in it, so they keep the feed's dates. Floating DTSTARTs stay floating. Without it, the zone most of the events' TZIDs resolve to, as feeds without X-WR-TIMEZONE, such as Exchange's, need. Pass `"UTC"` for the reading before that default. |
382
+ | `minimumDuration` | `Temporal.DurationLike` | `{ minutes: 30 }` | How long a timed or floating event lasts when DTEND or DURATION make it zero-length or negative. Such all-day events last a day. |
383
+
384
+ It returns an `IcsRecurringEvents`: `events`, in input order, and `issues`. Bad data becomes an
385
+ issue; only invalid options throw, as a `RangeError`. Each `IcsRecurrenceIssue` has its `code`, the
386
+ `event`, its `index`, a `message` and the `property` it concerns, such as `EXDATE`:
387
+
388
+ | `code` (`IcsRecurrenceIssueCode`) | Meaning |
389
+ | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
390
+ | `date-kind-mismatch` | EXDATE, RDATE, UNTIL or RECURRENCE-ID has another kind than the series, or an unresolved TZID. It was converted, or dropped. |
391
+ | `duplicate-event` | Another VEVENT has the same UID, or the same UID and RECURRENCE-ID. The higher SEQUENCE wins, then the later one. |
392
+ | `invalid-period` | An RDATE PERIOD that doesn't end after it starts. Only its start is kept. |
393
+ | `unresolved-time-zone` | DTSTART, or the RECURRENCE-ID of an override without its master, has a TZID that resolved to no time zone. The event is skipped. |
394
+ | `unsupported-rule` | RANGE=THISANDFUTURE the series can't take, or whose series or occurrence isn't in the feed. The override replaces only that one. |
395
+
396
+ The expander reports its own issues on top, such as a rule it can't expand.
397
+
398
+ ## Edits
399
+
400
+ When someone moves, resizes or deletes an occurrence, `onEventChange` receives a change whose
401
+ `event` is the occurrence: a `RecurrenceOccurrenceChange<TEvent, TEventId, TResourceId>`. The edit
402
+ functions turn it into what to save.
403
+
404
+ ```ts
405
+ // `ask` comes from the registry's useRecurrenceScopePrompt.
406
+ const scope = isRecurringChange(change)
407
+ ? await ask({ scopes: recurrenceEditScopes(change, adapters, { timeZone }) })
408
+ : "this";
409
+ // Rolls the change back, and is announced as a cancel.
410
+ if (scope === null) throw new DOMException("Cancelled", "AbortError");
411
+
412
+ const edit = resolveRecurrenceEdit(change, scope, adapters, { newSeriesId, occurrences, timeZone });
413
+ if (edit.type === "rejected") throw new Error(edit.message);
414
+ setEvents(
415
+ (events) =>
416
+ edit.type === "series"
417
+ ? applyRecurrenceEdit(events, edit, { createId })
418
+ : saveOneOff(events, edit.change), // a one-off event, unwrapped: save it as without recurrence
419
+ );
420
+ ```
421
+
422
+ - `isRecurringChange(change)` is `true` for a change to an occurrence of a series, which needs a
423
+ scope. It narrows `change` to a `RecurringOccurrenceChange`. A create, or a change to a one-off
424
+ event, needs none.
425
+ - `recurrenceEditScopes(change, adapters, options)` lists the `RecurrenceEditScope`s worth
426
+ offering, in order: `"this"`, `"following"`, `"all"`. It leaves out those
427
+ `resolveRecurrenceEdit` would reject, and `"following"` where it means `"all"`, as on the first
428
+ occurrence. It's empty for a change that isn't recurring. Hand it to the dialog that asks for
429
+ the scope. [Ask which events a change applies
430
+ to](./calendar.md#ask-which-events-a-change-applies-to) builds one.
431
+ - `resolveRecurrenceEdit(change, scope, adapters, options)` returns a `RecurrenceEdit`. It reads its
432
+ arguments only: it never changes them or calls your code.
433
+
434
+ Both take a `ResolveRecurrenceEditOptions`:
435
+
436
+ | Name | Type | Default | Description |
437
+ | ----------------- | --------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
438
+ | `defaultTimeZone` | `string` | `"UTC"` | Where timed series without `sourceTimeZone` repeat. Pass the expander's `defaultTimeZone`. |
439
+ | `newSeriesId` | `TEventId` | | The ID the series a `"following"` edit starts will have, when you pick IDs yourself. With it, `newSeries.id` is set and `occurrenceIds` maps into the new series; without it, those occurrences are left out of the map. |
440
+ | `occurrences` | `readonly RecurrenceOccurrence[]` | | The occurrences you show, such as the expansion you give the calendar, so `occurrenceIds` maps their IDs too. |
441
+ | `overrides` | `readonly TEvent[]` | | The series' overrides, or all your events: only cancelled overrides of the edited series are read. They then hide occurrences as EXDATEs do, so `"following"` from the first occurrence left to show is `"all"`. Pass them when you keep deleted occurrences as cancelled overrides, as Google's API does. |
442
+ | `maxIterations` | `number` | | Most loop steps a `"following"` edit's search for the previous occurrence may take. |
443
+ | `timeZone` | `string` | | The calendar's time zone, which occurrences are moved and resized on and the editor shows times in. `"all"` and `"following"` replay the change, calendar days and then exact time, on the series' own clock: a drag by a week keeps its time of day, a drag an hour later moves it an hour, and a resize by days stretches it by days, even in a week when the calendar's zone has changed DST and the series' hasn't. Without it, the calendar is taken to show the series' own zone, and the series takes the instants it proposes, which are an hour off then. An override in another zone replays on its own clock. |
444
+
445
+ ### What each scope does
446
+
447
+ - **`"this"`.** A move, resize or update creates the occurrence's override, or updates it. Its
448
+ `recurrenceId` stays, so the occurrence keeps its ID. A delete adds the original start to the
449
+ master's `exceptionDates` and deletes an existing override. An override whose master isn't
450
+ loaded is marked `cancelled` instead. Google's API records a deleted instance as a cancelled one;
451
+ this package reads those, but writes EXDATE, which every iCalendar store understands.
452
+ - **`"following"`.** The master ends before the occurrence: UNTIL becomes the previous
453
+ occurrence's start, or COUNT the number of occurrences before it. A new series starts where the
454
+ change puts the occurrence, with the rest of the rule moved as `"all"` moves it, and later
455
+ EXDATEs, RDATEs and overrides move to it. A delete only ends the series. Google Calendar and
456
+ Outlook on the web split a series the same way. When nothing before the occurrence is left to
457
+ show, it's `"all"`: EXDATEs count for that, and cancelled overrides when you pass `overrides`.
458
+ - **`"all"`.** The change from where the occurrence showed to where it goes, measured on the
459
+ series' wall clock, applies to every occurrence. The rule moves with it, such as BYDAY when the
460
+ day changes, and so do EXDATEs, RDATEs, UNTIL and every override's `recurrenceId`, so deleted
461
+ occurrences stay deleted and moved ones don't appear twice. Overrides keep their own placements.
462
+ A move keeps the series' length; a resize changes it. A delete removes the master and its
463
+ overrides. An occurrence dropped on the later of two equal wall times, as DST's end repeats an
464
+ hour, gets an override there, since a rule can only say the earlier one, where the rest of the
465
+ series goes.
466
+
467
+ A timed occurrence dropped on a day, or an all-day one dropped on a time, works in every scope.
468
+ `"following"` and `"all"` change the series' kind and convert its dates; a rule with several times
469
+ a day can't become all-day.
470
+
471
+ ### RecurrenceEdit
472
+
473
+ A `RecurrenceEdit` is one of three types:
474
+
475
+ - `{ type: "event", change }`, a `RecurrenceEventEdit`: a create, or a change to a one-off event,
476
+ with the event unwrapped. Apply `change` as you would without recurrence.
477
+ - `{ type: "series", … }`, a `RecurrenceSeriesEdit`, below.
478
+ - `{ type: "rejected", reason, message }`, a `RecurrenceEditRejection`: the scope can't apply.
479
+
480
+ A `RecurrenceSeriesEdit` describes the writes as plain data. Only placement and recurrence are
481
+ described; copy anything else an `"update"` changed, such as the title, yourself.
482
+
483
+ | Name | Type | Description |
484
+ | ---------------- | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
485
+ | `scope` | `RecurrenceEditScope` | Required. The scope applied. `"following"` on the first occurrence is `"all"`. |
486
+ | `cause` | `"move" \| "resize-start" \| "resize-end" \| "update" \| "delete"` | Required. The change's cause. |
487
+ | `seriesId` | `TEventId` | Required. The master's ID. |
488
+ | `recurrenceId` | `RecurrenceDate` | Required. The edited occurrence's original start. |
489
+ | `series` | `RecurrenceSeriesWrite` | Required. The master: `keep`, `update` with its new `placement` (its first occurrence) and whole `recurrence`, or `delete`. |
490
+ | `newSeries` | `RecurrenceNewSeries` | The series a `"following"` edit starts: copy `from`, the old master, except its ID, placement and recurrence; give it `placement`, `recurrence`, and `id` when you passed `newSeriesId`. |
491
+ | `override` | `RecurrenceOverrideWrite` | The edited occurrence's override: `create` one from the master at `placement`, `update` the existing one (its placement, `recurrenceId`, `cancelled`, and its series when `series` is `"new"`), or `delete` it. |
492
+ | `otherOverrides` | `RecurrenceOverridesRule` | Required. What happens to the series' other overrides, as a rule, since you may not have them all loaded. Apply `recurrenceOverrideEdit` to each one you store. |
493
+ | `occurrenceIds` | `ReadonlyMap<EventId, string>` | Required. Old occurrence IDs to new ones, for your selection: the edited occurrence's and those in `options.occurrences` whose IDs change. Empty for `"this"`. |
494
+
495
+ `RecurrenceSeriesWrite`, `RecurrenceNewSeries` and `RecurrenceOverrideWrite` are the types of
496
+ `series`, `newSeries` and `override`. A placement is a `CalendarEventPlacement`: an interval and its
497
+ `resourceIds`.
498
+
499
+ ### recurrenceOverrideEdit
500
+
501
+ `recurrenceOverrideEdit(edit, recurrenceId)` says what an edit does to one stored override of
502
+ `edit.seriesId`, by its `recurrenceId`. It returns a `RecurrenceOverrideRekey`:
503
+
504
+ - `{ action: "keep" }`, as for the edited occurrence's own override, which `edit.override`
505
+ covers;
506
+ - `{ action: "delete" }`;
507
+ - `{ action: "update", recurrenceId, series }`: its new original start, and `"new"` when it moves
508
+ to the series a `"following"` edit starts. Its own placement stays.
509
+
510
+ `otherOverrides` is a `RecurrenceOverridesRule`: an `action` (`"keep"`, `"rekey"` or `"delete"`),
511
+ `after`, the original start after which it applies, `series`, and `shift`, a `RecurrenceKeyShift`
512
+ that says how original starts move. Read them through `recurrenceOverrideEdit`; the fields of
513
+ `RecurrenceKeyShift` and of its `RecurrenceOrdinalShift`, which moves ordinal weekdays such as the
514
+ 2nd Tuesday within their month or year, may change.
515
+
516
+ ### Rejections
517
+
518
+ `RecurrenceEditRejection.reason` is a `RecurrenceEditRejectionReason`:
519
+
520
+ | `reason` | Meaning |
521
+ | -------------------- | -------------------------------------------------------------------------------------------------------- |
522
+ | `date-kind-mismatch` | The change's dates don't fit the series' kind. |
523
+ | `extra-occurrence` | `"following"` from an occurrence only an RDATE adds, which no rule can continue from. |
524
+ | `invalid-rule` | The engine can't expand the rule, or the one the edit would write. |
525
+ | `invalid-time-zone` | The series' or the placement's zone isn't one Temporal knows. |
526
+ | `iteration-limit` | The search for the previous occurrence ran past `maxIterations`. |
527
+ | `missing-series` | `"following"` or `"all"` on an override whose master isn't loaded. |
528
+ | `rule-cannot-move` | The rule can't follow the change, such as a BYSETPOS rule moved to another day, or the 28th to the 29th. |
529
+ | `unknown-occurrence` | The rule doesn't produce the occurrence any more. |
530
+ | `unsupported-rule` | The engine doesn't handle the rule, or the one the edit would write. |
531
+
532
+ `recurrenceEditScopes` leaves out the scopes that would be rejected, so the dialog offers only
533
+ scopes that apply.
534
+
535
+ ### applyRecurrenceEdit
536
+
537
+ `applyRecurrenceEdit(events, edit, { createId })` applies a series edit to an array of
538
+ `RecurringCalendarEvent`s and returns a new array. It updates or deletes the master, adds the
539
+ series a `"following"` edit starts, writes the edited occurrence's override, and applies
540
+ `recurrenceOverrideEdit` to every other override of the series. Events it doesn't touch stay the
541
+ same objects.
542
+
543
+ `createId(event)`, the one field of `ApplyRecurrenceEditOptions`, returns the ID of an event the
544
+ edit creates: an override, or the new series when `newSeries.id` isn't set. It gets the event
545
+ without its ID. Apps with their own event type or store write the same steps themselves.
546
+
547
+ See [Save the edit](./calendar.md#save-the-edit).