@moltenship/calendar 1.0.0-rc.2 → 2.0.0-beta.1

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 (70) hide show
  1. package/README.md +24 -4
  2. package/dist/index.d.mts +2 -2
  3. package/docs/accessibility.md +304 -0
  4. package/docs/agents/changes.md +449 -0
  5. package/docs/agents/coding-agents.md +117 -0
  6. package/docs/agents/connect.md +435 -0
  7. package/docs/agents/deleting.md +296 -0
  8. package/docs/agents/reference.md +512 -0
  9. package/docs/agents/server.md +317 -0
  10. package/docs/agents/webmcp.md +153 -0
  11. package/docs/agents.md +353 -0
  12. package/docs/annotations.md +196 -0
  13. package/docs/api/changes.md +248 -0
  14. package/docs/api/dnd-kit.md +267 -0
  15. package/docs/api/events.md +152 -0
  16. package/docs/api/ics.md +524 -0
  17. package/docs/api/primitives.md +168 -0
  18. package/docs/api/react.md +236 -0
  19. package/docs/api/recurrence.md +586 -0
  20. package/docs/api/solid.md +460 -0
  21. package/docs/api/store.md +92 -0
  22. package/docs/api/temporal.md +172 -0
  23. package/docs/api.md +57 -0
  24. package/docs/availability.md +573 -0
  25. package/docs/comparison.md +279 -0
  26. package/docs/concepts/anatomy.md +73 -0
  27. package/docs/concepts/events.md +164 -0
  28. package/docs/concepts/how-it-works.md +95 -0
  29. package/docs/concepts/recurrence.md +127 -0
  30. package/docs/concepts/state.md +244 -0
  31. package/docs/concepts/time.md +170 -0
  32. package/docs/guides/dnd-kit.md +333 -0
  33. package/docs/guides/google-calendar-sync.md +1246 -0
  34. package/docs/guides/google-calendar.md +367 -0
  35. package/docs/guides/ics.md +486 -0
  36. package/docs/guides/motion.md +414 -0
  37. package/docs/guides/nextjs-app-router.md +630 -0
  38. package/docs/guides/nextjs.md +149 -0
  39. package/docs/guides/performance.md +103 -0
  40. package/docs/guides/recurring-events.md +583 -0
  41. package/docs/guides/tanstack-query.md +485 -0
  42. package/docs/index.md +118 -0
  43. package/docs/installation.md +194 -0
  44. package/docs/interactions.md +408 -0
  45. package/docs/llms.txt +86 -0
  46. package/docs/migrating-to-2.md +322 -0
  47. package/docs/quick-start.md +159 -0
  48. package/docs/stylex.md +265 -0
  49. package/docs/testing.md +581 -0
  50. package/docs/theme-builder.md +81 -0
  51. package/docs/theming.md +169 -0
  52. package/docs/undo-redo.md +339 -0
  53. package/docs/views/agenda.md +221 -0
  54. package/docs/views/custom-views.md +192 -0
  55. package/docs/views/month.md +109 -0
  56. package/docs/views/resource-columns.md +261 -0
  57. package/docs/views/resource-timeline.md +225 -0
  58. package/docs/views/scrolling.md +192 -0
  59. package/docs/views/week-and-day.md +257 -0
  60. package/docs/views/year.md +138 -0
  61. package/docs/views.md +69 -0
  62. package/package.json +41 -14
  63. package/skills/moltenship-time/SKILL.md +75 -0
  64. package/skills/moltenship-time/references/agent-ready.md +272 -0
  65. package/skills/moltenship-time/references/changes.md +188 -0
  66. package/skills/moltenship-time/references/compose.md +168 -0
  67. package/skills/moltenship-time/references/mistakes.md +98 -0
  68. package/skills/moltenship-time/references/solid.md +202 -0
  69. package/skills/moltenship-time/references/state.md +129 -0
  70. package/skills/moltenship-time/references/testing.md +176 -0
@@ -0,0 +1,524 @@
1
+ # iCalendar (ICS)
2
+
3
+ Parse ICS feeds and files into typed Temporal values with @moltenship/ics.
4
+
5
+ Source: https://time.molten.sh/docs/api/ics
6
+
7
+ `@moltenship/ics` reads iCalendar (RFC 5545), the text format of calendar feeds, invitations and
8
+ exports from Google Calendar, Outlook, iCloud and most other calendar apps. ICS text goes in.
9
+ Calendars, events, to-dos, journals, free/busy and time zones come out, with dates and times as
10
+ Temporal values and every problem in the text listed with its line.
11
+
12
+ It's a separate package. `@moltenship/calendar` doesn't re-export it, and it imports no other
13
+ Moltenship package, so you can use it without the calendar: on a server, in a worker, or in a
14
+ script. It has no dependencies.
15
+
16
+ To show a feed's events in the calendar, follow [Show an ICS feed](../guides/ics.md).
17
+
18
+ ## Install
19
+
20
+ Like the calendar, `@moltenship/ics` is on npm as a release candidate for 1.0.0.
21
+
22
+ **pnpm**
23
+
24
+ ```bash
25
+ pnpm add @moltenship/ics
26
+ ```
27
+
28
+ **npm**
29
+
30
+ ```bash
31
+ npm install @moltenship/ics
32
+ ```
33
+
34
+ **yarn**
35
+
36
+ ```bash
37
+ yarn add @moltenship/ics
38
+ ```
39
+
40
+ **bun**
41
+
42
+ ```bash
43
+ bun add @moltenship/ics
44
+ ```
45
+
46
+ It needs the same setup as the calendar: a runtime with `Temporal`, or a polyfill loaded first,
47
+ and TypeScript's `ESNext` library. See [Installation](../installation.md#temporal).
48
+
49
+ ## Parse
50
+
51
+ ```ts
52
+ import { parseIcs } from "@moltenship/ics";
53
+
54
+ const response = await fetch("https://example.com/team.ics");
55
+ const { calendars, issues } = parseIcs(await response.text());
56
+
57
+ for (const event of calendars[0]?.events ?? []) {
58
+ console.log(event.summary, event.start);
59
+ }
60
+ ```
61
+
62
+ Edit the text below to see what `parseIcs` returns. The sample is a Google Calendar feed with a
63
+ recurring meeting, an all-day event, an event imported from Outlook, and one date written the way
64
+ a careless exporter writes it.
65
+
66
+ > Live example: see it at https://time.molten.sh/docs/api/ics
67
+
68
+ `parseIcs(text, options)` returns two lists:
69
+
70
+ - `calendars`: every VCALENDAR in the text, usually one. Their components keep the order of the
71
+ text.
72
+ - `issues`: every deviation from RFC 5545 the parser found, with its line. See
73
+ [Issues and strict mode](#issues-and-strict-mode).
74
+
75
+ Parsing is lenient. The parser keeps what it can read, skips components it can't type, such as an
76
+ event without `UID` or `DTSTART`, and reports each problem as an issue. Bad data never throws.
77
+ Text without a VCALENDAR, empty text included, gives no calendars and an error.
78
+
79
+ To parse many texts with the same options, create a parser once:
80
+
81
+ ```ts
82
+ import { createIcsParser } from "@moltenship/ics";
83
+
84
+ const parser = createIcsParser({ strict: true });
85
+ const { calendars } = parser.parse(text);
86
+ ```
87
+
88
+ `parseIcs(text, options)` is the same as `createIcsParser(options).parse(text)`.
89
+
90
+ ### Calendars
91
+
92
+ Each calendar is an `IcsCalendar`. Optional fields are absent when the file doesn't set them, and
93
+ lists are empty.
94
+
95
+ | Name | Type | Description |
96
+ | ----------------- | ------------------------ | ----------------------------------------------------------------------------------------------- |
97
+ | `events` | `readonly IcsEvent[]` | Required. VEVENTs, in the order of the text. |
98
+ | `todos` | `readonly IcsTodo[]` | Required. VTODOs. |
99
+ | `journals` | `readonly IcsJournal[]` | Required. VJOURNALs. |
100
+ | `freeBusy` | `readonly IcsFreeBusy[]` | Required. VFREEBUSYs. |
101
+ | `timeZones` | `readonly IcsTimeZone[]` | Required. VTIMEZONEs, each with its `tzid` as written and the `timeZone` it resolved to. |
102
+ | `name` | `string` | NAME (RFC 7986), else X-WR-CALNAME. |
103
+ | `description` | `string` | DESCRIPTION (RFC 7986), else X-WR-CALDESC. |
104
+ | `timeZone` | `string` | X-WR-TIMEZONE, resolved to a time zone id. See [Dates and times](#dates-and-times). |
105
+ | `refreshInterval` | `Temporal.Duration` | REFRESH-INTERVAL (RFC 7986), else X-PUBLISHED-TTL. |
106
+ | `method` | `string` | METHOD, such as PUBLISH, REQUEST or CANCEL. |
107
+ | `productId` | `string` | PRODID: the app that wrote the file. |
108
+ | `color` | `string` | COLOR (RFC 7986). |
109
+ | `extensions` | `object` | Required. The `X-` properties you declared. See [Extensions](#extensions-with-standard-schema). |
110
+ | `otherProperties` | `readonly IcsProperty[]` | Required. Properties no field maps, such as other X- properties. |
111
+
112
+ It also has `calendarScale`, `lastModified`, `source`, `uid`, `url`, `version` and
113
+ `otherComponents`.
114
+
115
+ ### Events
116
+
117
+ Each event is an `IcsEvent`:
118
+
119
+ | Name | Type | Description |
120
+ | ----------------- | ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
121
+ | `uid` | `string` | Required. UID. An event without one is reported and skipped. |
122
+ | `start` | `IcsDateValue` | Required. DTSTART: a date or a date-time. See [Dates and times](#dates-and-times). An event without one is reported and skipped. |
123
+ | `end` | `IcsDateValue` | DTEND as written. Use [getEventEnd](#ends-of-events) for the effective end. |
124
+ | `duration` | `Temporal.Duration` | DURATION as written. |
125
+ | `summary` | `string` | SUMMARY, with escapes such as \, and \n decoded. |
126
+ | `description` | `string` | DESCRIPTION. |
127
+ | `location` | `string` | LOCATION. |
128
+ | `status` | `string` | STATUS: CONFIRMED, TENTATIVE or CANCELLED. |
129
+ | `transparency` | `string` | TRANSP: OPAQUE (busy) or TRANSPARENT (free). |
130
+ | `organizer` | `IcsCalendarUser` | ORGANIZER. |
131
+ | `attendees` | `readonly IcsAttendee[]` | Required. ATTENDEE, every occurrence. |
132
+ | `recurrenceRule` | `IcsRecurrenceRule` | RRULE. See [Recurrence](#recurrence). |
133
+ | `exceptionDates` | `readonly IcsDateValue[]` | Required. EXDATE. |
134
+ | `recurrenceDates` | `readonly (IcsDateValue \| IcsPeriod)[]` | Required. RDATE. |
135
+ | `recurrenceId` | `IcsDateValue` | RECURRENCE-ID: the occurrence of a series this event replaces. |
136
+ | `alarms` | `readonly IcsAlarm[]` | Required. VALARMs. |
137
+ | `categories` | `readonly string[]` | Required. CATEGORIES, every value. |
138
+ | `stamp` | `Temporal.Instant` | DTSTAMP. Required by RFC 5545, but often missing from exports. |
139
+ | `extensions` | `object` | Required. The `X-` properties you declared. See [Extensions](#extensions-with-standard-schema). |
140
+ | `otherProperties` | `readonly IcsProperty[]` | Required. Properties no field maps, such as X-MICROSOFT-CDO-BUSYSTATUS. |
141
+
142
+ Events also have `attachments`, `classification`, `color`, `comments`, `conferences`, `contacts`,
143
+ `created`, `geo`, `lastModified`, `priority`, `recurrenceRange`, `relatedTo`, `resources`,
144
+ `sequence`, `url` and `otherComponents`.
145
+
146
+ The organizer and attendees are calendar users. `address` is the value as written, such as
147
+ `mailto:ann@example.com`, `email` comes from the `EMAIL` parameter or the address without
148
+ `mailto:`, and `name` comes from the `CN` parameter. Attendees add `participationStatus`, `role`, `rsvp` and the other `ATTENDEE`
149
+ parameters.
150
+
151
+ To-dos (`IcsTodo`) and journals (`IcsJournal`) share most of these fields. Every type is
152
+ exported, and each field has a doc comment that your editor shows.
153
+
154
+ ## Dates and times
155
+
156
+ A `DATE` value is a `Temporal.PlainDate`. A `DATE-TIME` value can mean several things, so it's a
157
+ union, and `kind` says what the file wrote:
158
+
159
+ | `kind` | Written as | Fields |
160
+ | ------------ | --------------------------------------- | --------------------------------------------------------------------- |
161
+ | `date` | `DTSTART;VALUE=DATE:20261016` | `date: Temporal.PlainDate` |
162
+ | `utc` | `DTSTART:20261014T080000Z` | `instant: Temporal.Instant` |
163
+ | `zoned` | `DTSTART;TZID=Europe/Berlin:20261014T…` | `zoned: Temporal.ZonedDateTime`, `wallTime`, `tzid` |
164
+ | `unresolved` | `DTSTART;TZID=Head office:20261014T…` | `wallTime: Temporal.PlainDateTime`, `tzid` |
165
+ | `floating` | `DTSTART:20261014T100000` | `wallTime: Temporal.PlainDateTime`: the same clock time in every zone |
166
+
167
+ `zoned` is a TZID that resolved to a time zone, and `unresolved` one that didn't. See
168
+ [Time zones](#time-zones). Switch on `kind` to get what you need, such as an instant:
169
+
170
+ ```ts
171
+ import type { IcsDateValue } from "@moltenship/ics";
172
+
173
+ function startInstant(start: IcsDateValue, timeZone: string): Temporal.Instant {
174
+ switch (start.kind) {
175
+ case "utc":
176
+ return start.instant;
177
+ case "zoned":
178
+ return start.zoned.toInstant();
179
+ case "date":
180
+ return start.date.toZonedDateTime(timeZone).toInstant();
181
+ case "floating":
182
+ case "unresolved":
183
+ return start.wallTime.toZonedDateTime(timeZone).toInstant();
184
+ }
185
+ }
186
+ ```
187
+
188
+ The same union holds `DTEND`, `DUE`, `RECURRENCE-ID`, `EXDATE`, `RDATE` and a rule's `UNTIL`.
189
+
190
+ A wall time that falls in a daylight saving gap or fold resolves the way RFC 5545 §3.3.5 says,
191
+ which is Temporal's `compatible` disambiguation: a time in a gap moves forward by the gap's length,
192
+ and a time in a fold takes the earlier of the two. `wallTime` keeps what the file wrote:
193
+
194
+ ```ts
195
+ // DTSTART;TZID=Europe/Berlin:20260329T023000, in the hour Berlin skips
196
+ const { start } = parseIcs(text).calendars[0]!.events[0]!;
197
+
198
+ if (start.kind === "zoned") {
199
+ start.wallTime.toString(); // "2026-03-29T02:30:00"
200
+ start.zoned.toString(); // "2026-03-29T03:30:00+02:00[Europe/Berlin]"
201
+ }
202
+ ```
203
+
204
+ Nothing falls back to the host's time zone, so the result is the same on every machine. A floating
205
+ time stays floating. If the calendar has an `X-WR-TIMEZONE`, as Google's feeds do, its resolved id
206
+ is `calendar.timeZone`: the zone the publisher meant for floating times. Apply it yourself if you
207
+ want it.
208
+
209
+ ## Time zones
210
+
211
+ Temporal has no custom time zones, so it can't use the definitions inside a file. Instead, the
212
+ parser maps each TZID to a time zone Temporal knows. It tries, in order:
213
+
214
+ 1. your `resolveTimeZone(tzid, vtimezone)` option, if it returns an id Temporal knows;
215
+ 2. IANA ids, such as `Europe/Berlin`;
216
+ 3. vendor paths, such as `/mozilla.org/20050126_1/Europe/Berlin`;
217
+ 4. Windows ids and Outlook display names, such as `W. Europe Standard Time` or
218
+ `(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna`, through the Unicode CLDR
219
+ mapping;
220
+ 5. the file's VTIMEZONE: its `X-LIC-LOCATION`, or a fixed offset when it has no daylight saving
221
+ time;
222
+ 6. offset names, such as `GMT+0100` or `UTC`.
223
+
224
+ A TZID can name a zone that never changes offset while the file's VTIMEZONE has daylight saving
225
+ rules. `TZID=EST` on its own is Temporal's fixed −05:00 zone, but a VTIMEZONE called `EST` with
226
+ summer rules usually means New York. When the VTIMEZONE observes daylight saving time, steps 2–4
227
+ and 6 skip zones that don't, so the wrong fixed zone is never picked.
228
+
229
+ A TZID that matches none of these stays `unresolved`, with an `unknown-time-zone` warning. The
230
+ parser doesn't guess:
231
+
232
+ ```ts
233
+ // DTSTART;TZID=Head office:20261014T100000
234
+ const { calendars, issues } = parseIcs(text);
235
+
236
+ calendars[0]!.events[0]!.start;
237
+ // { kind: "unresolved", tzid: "Head office", wallTime: 2026-10-14T10:00:00 }
238
+ issues[0]!.code; // "unknown-time-zone"
239
+ ```
240
+
241
+ If you know what your feeds mean, say so with `resolveTimeZone`. The parser asks it before its own
242
+ rules:
243
+
244
+ ```ts
245
+ const { calendars } = parseIcs(text, {
246
+ resolveTimeZone: (tzid) => (tzid === "Head office" ? "Europe/Lisbon" : undefined),
247
+ });
248
+ ```
249
+
250
+ The second argument is the file's VTIMEZONE with that TZID, as a raw component, if there is one.
251
+ Return an IANA id or a `±HH:MM` offset, or `undefined` to fall through to the rules above. An id
252
+ Temporal doesn't know falls through too.
253
+
254
+ ## Ends of events
255
+
256
+ An event's end can be written as `DTEND`, as `DURATION`, or not at all. `event.end` and
257
+ `event.duration` keep what the file says, and `getEventEnd` works out the end:
258
+
259
+ ```ts
260
+ import { getEventEnd } from "@moltenship/ics";
261
+
262
+ const end = getEventEnd(event); // an IcsDateValue of the same kind as event.start
263
+ ```
264
+
265
+ - With `DTEND`, it's `DTEND`, converted to the start's kind if it differs. A `DTEND` in another
266
+ zone keeps its instant; one without an instant keeps its wall time.
267
+ - With `DURATION`, it's the start plus the duration. On a zoned start, days are calendar days and
268
+ hours are exact hours, as RFC 5545 §3.3.6 says, so `P1D` across a daylight saving change keeps
269
+ the wall time. On a date start, it adds whole days, and a time part rounds up to a day.
270
+ - With neither, an all-day event ends the next day and a timed event ends when it starts.
271
+
272
+ An event with both `DTEND` and `DURATION` keeps `DTEND`, and the parser drops `DURATION` with a
273
+ `conflicting-properties` warning.
274
+
275
+ ## Recurrence
276
+
277
+ A recurring event is one `IcsEvent` with a rule, such as the design review in the playground:
278
+
279
+ ```ts
280
+ // RRULE:FREQ=WEEKLY;BYDAY=WE;COUNT=6
281
+ // EXDATE;TZID=Europe/Berlin:20261028T100000
282
+ event.recurrenceRule;
283
+ // { frequency: "WEEKLY", byDay: [{ weekday: "WE" }], count: 6 }
284
+ event.exceptionDates;
285
+ // [{ kind: "zoned", zoned: 2026-10-28T10:00:00+01:00[Europe/Berlin], … }]
286
+ ```
287
+
288
+ - `recurrenceRule` is the `RRULE` as an `IcsRecurrenceRule`, with named parts: `frequency`,
289
+ `interval`, `count`, `until`, `byDay`, `byMonth`, `byMonthDay`, `weekStart` and the rest. Parts
290
+ the rule doesn't state are absent, and defaults such as an `interval` of 1 aren't filled in.
291
+ - `exceptionDates` lists the `EXDATE` values: occurrences that don't happen.
292
+ - `recurrenceDates` lists the `RDATE` values: extra occurrences, as dates, date-times or periods.
293
+ - `recurrenceId` is set on an override: a separate event with the series' `UID` that replaces one
294
+ occurrence, such as a meeting moved to another time. `recurrenceRange` is `"THISANDFUTURE"` when
295
+ the override also applies to the occurrences after it.
296
+
297
+ This package parses rules but doesn't list a series' occurrences.
298
+ [`@moltenship/calendar-recurrence`](./recurrence.md) does: `recurringEventsFromIcs` groups
299
+ parsed events into series masters, overrides and one-off events, and its expander lists the
300
+ occurrences for a range of dates. It applies an override with `RANGE=THISANDFUTURE` to the
301
+ occurrences after it too, by splitting the series. See
302
+ [Recurring events](../guides/ics.md#recurring-events) in the ICS guide.
303
+
304
+ ## Issues and strict mode
305
+
306
+ Each issue says what's wrong and where:
307
+
308
+ ```ts
309
+ {
310
+ severity: "warning",
311
+ code: "invalid-value",
312
+ message: "DTSTART: a DATE where DATE-TIME is declared; read as DATE",
313
+ line: 62,
314
+ path: ["calendars", 0, "events", 3, "start"],
315
+ component: "VEVENT",
316
+ property: "DTSTART",
317
+ }
318
+ ```
319
+
320
+ - `severity` is `"error"` or `"warning"`. An error drops the value or component it concerns. A
321
+ warning keeps a best-effort value, such as the date above.
322
+ - `line` is the 1-based line in the text, when the issue comes from one. A component skipped for a
323
+ missing property reports its `BEGIN` line; one skipped for an invalid value reports that
324
+ property's line.
325
+ - `path` is where the issue lands in the result. Indexes count the components that were kept, so
326
+ a skipped event's path ends at `events`. It's empty for problems with the text's structure.
327
+ - `component` and `property` name the component and property, when there is one.
328
+
329
+ `code` says what kind of problem it is:
330
+
331
+ | Code | Severity | Meaning |
332
+ | ------------------------ | ---------------- | -------------------------------------------------------------------------------------------------------- |
333
+ | `missing-property` | error | A required property, such as `UID` or `DTSTART`, is missing. The component is skipped. |
334
+ | `invalid-value` | error or warning | A value doesn't parse as its type, or parses only leniently. |
335
+ | `invalid-content-line` | warning | A line isn't `NAME;PARAM=…:value`, or has an invalid parameter. |
336
+ | `invalid-structure` | error or warning | An error when there's no VCALENDAR; a warning for an `END` without its `BEGIN` or a component left open. |
337
+ | `duplicate-property` | warning | A property that may occur once occurs again. The first one wins. |
338
+ | `conflicting-properties` | warning | Properties that exclude each other, such as `DTEND` and `DURATION`. |
339
+ | `unknown-time-zone` | warning | A TZID that resolves to no time zone. The value stays `unresolved`. |
340
+ | `unsupported` | warning | Something the parser recognizes but doesn't interpret, such as `RSCALE`. |
341
+ | `schema` | error | An extension's schema rejected a value. See [Extensions](#extensions-with-standard-schema). |
342
+
343
+ Pass `strict: true` to throw instead when there's an error. The parser throws an `IcsParseError`
344
+ whose `issues` lists the error-severity issues. Warnings never throw.
345
+
346
+ ```ts
347
+ import { IcsParseError, parseIcs } from "@moltenship/ics";
348
+
349
+ function readFeed(text: string) {
350
+ try {
351
+ return parseIcs(text, { strict: true }).calendars;
352
+ } catch (error) {
353
+ if (error instanceof IcsParseError) {
354
+ console.error(error.message); // "VEVENT has no DTSTART; skipped (and 2 more)"
355
+ console.error(error.issues); // every error-severity issue
356
+ }
357
+ throw error;
358
+ }
359
+ }
360
+ ```
361
+
362
+ The error's `message` is the first issue's message, with a count of the others.
363
+
364
+ An exception thrown by a function you pass in, `resolveTimeZone` or an extension schema, comes out
365
+ of `parse` unchanged, strict or not. That's a bug in your code, not a problem with the text.
366
+
367
+ ## Extensions with Standard Schema
368
+
369
+ Calendar apps add their own `X-` properties, such as Outlook's busy status. Undeclared, they land
370
+ in `otherProperties` as raw text. Declare the ones you rely on with `icsExtension`, and they're
371
+ decoded, validated and typed in `extensions`:
372
+
373
+ ```ts
374
+ import { createIcsParser, icsExtension } from "@moltenship/ics";
375
+ import { z } from "zod";
376
+
377
+ const extensions = {
378
+ event: {
379
+ busyStatus: icsExtension("X-MICROSOFT-CDO-BUSYSTATUS", {
380
+ schema: z.enum(["FREE", "TENTATIVE", "BUSY", "OOF", "WORKINGELSEWHERE"]),
381
+ }),
382
+ resourceIds: icsExtension("X-RESOURCE-ID", { multiple: true }),
383
+ guests: icsExtension("X-EXAMPLE-GUESTS", { type: "INTEGER" }),
384
+ },
385
+ };
386
+
387
+ const parser = createIcsParser({ extensions });
388
+ const { calendars } = parser.parse(text);
389
+ const event = calendars[0]!.events[0]!;
390
+
391
+ event.extensions.busyStatus; // "FREE" | "TENTATIVE" | … | undefined
392
+ event.extensions.resourceIds; // readonly string[]
393
+ event.extensions.guests; // number | undefined
394
+ ```
395
+
396
+ `extensions` has a map for each component kind: `calendar`, `event`, `todo` and `journal`. Each
397
+ key is the field name in `extensions`, and `icsExtension(name, options)` takes the property's name
398
+ and these options:
399
+
400
+ | Name | Type | Default | Description |
401
+ | ---------- | ------------------ | -------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
402
+ | `type` | `IcsValueType` | `"TEXT"` | The RFC 5545 value type to decode the raw value as, such as INTEGER, DATE-TIME or DURATION, whatever the property's VALUE parameter says. |
403
+ | `multiple` | `boolean` | `false` | Collect every occurrence and every comma-separated value into an array, which is empty when there's none. |
404
+ | `schema` | `StandardSchemaV1` | | Validates or transforms each decoded value: Zod, Valibot, ArkType or any other Standard Schema. Its input type must accept the decoded value. |
405
+
406
+ The types are inferred: a schema's output type if it has one, else the value type's decoded type,
407
+ such as `number` for `INTEGER` or `IcsDateValue` for `DATE-TIME`. Without `multiple`, a property
408
+ that's missing gives `undefined`.
409
+
410
+ A value the schema rejects is dropped with a `schema` issue. That's an error, so strict parsing
411
+ throws and [validation](#as-a-standard-schema) fails. Schemas must validate synchronously: a schema
412
+ that returns a promise, such as one with an async refinement, drops the value with a `schema` error
413
+ too.
414
+
415
+ To name the result's types in your own code, keep the `extensions` object in a constant, as above.
416
+ Every type takes it the same way: `IcsCalendar<typeof extensions>`, `IcsEvent<typeof extensions>`.
417
+
418
+ ## As a Standard Schema
419
+
420
+ A parser is itself a [Standard Schema](https://standardschema.dev) that validates ICS text, so it
421
+ plugs into anything that takes one, such as a form library or a router's body validation. It fails
422
+ with the error-severity issues, each with its `message` and `path`, or succeeds with the parse
423
+ result. Warnings don't fail it.
424
+
425
+ ```ts
426
+ import { createIcsParser } from "@moltenship/ics";
427
+
428
+ const parser = createIcsParser();
429
+
430
+ async function readUpload(body: unknown) {
431
+ const result = await parser["~standard"].validate(body);
432
+ if (result.issues) {
433
+ return { status: 400, errors: result.issues.map((issue) => issue.message) };
434
+ }
435
+ return { status: 200, calendars: result.value.calendars };
436
+ }
437
+ ```
438
+
439
+ A value that isn't a string fails too.
440
+
441
+ ## Lower layers
442
+
443
+ `parseIcs` is the top of three layers, and each is exported. When the typed model doesn't cover
444
+ your data, drop down a layer.
445
+
446
+ ### Syntax
447
+
448
+ `parseIcsComponents(text)` splits the text into a lossless tree of components and properties, with
449
+ values exactly as written, and reports only syntax issues:
450
+
451
+ ```ts
452
+ import { parseIcsComponents, stringifyIcsComponents } from "@moltenship/ics";
453
+
454
+ const { components, issues } = parseIcsComponents(text);
455
+ const event = components[0]!.components[0]!;
456
+ const start = event.properties.find((property) => property.name === "DTSTART")!;
457
+ start.parameters.TZID; // ["Europe/Berlin"]
458
+ start.value; // "20261014T100000", as written
459
+
460
+ const ics = stringifyIcsComponents(components);
461
+ ```
462
+
463
+ A component has its `name`, `properties`, nested `components` and `line`. A property has its
464
+ `name`, `parameters` (each a list of values), its raw `value` and its `line`. Names are
465
+ upper-cased.
466
+
467
+ `stringifyIcsComponents(tree)` writes a tree back as text, with CRLF line ends and lines folded at
468
+ 75 octets. It throws a `RangeError` for a name or value that contains a line break, which iCalendar
469
+ can't hold; escape line breaks in TEXT values as `\n` first.
470
+
471
+ ### Values
472
+
473
+ These turn a raw value into a typed one and back, for any RFC 5545 value type:
474
+
475
+ ```ts
476
+ import { decodeIcsDateValue, decodeIcsValue, encodeIcsValue } from "@moltenship/ics";
477
+
478
+ decodeIcsValue("DURATION", "PT1H30M");
479
+ // { ok: true, value: Temporal.Duration PT1H30M }
480
+ decodeIcsValue("INTEGER", "ten");
481
+ // { ok: false, message: "\"ten\" isn't an INTEGER" }
482
+ decodeIcsValue("DATE-TIME", "20261014T100000", { tzid: "Europe/Berlin" });
483
+ // { ok: true, value: { kind: "zoned", … } }
484
+ decodeIcsDateValue("20261019", "DATE-TIME");
485
+ // { ok: true, value: { kind: "date", … }, warnings: [{ code: "invalid-value" }] }
486
+
487
+ encodeIcsValue("TEXT", "Room 4.01, Berlin office"); // "Room 4.01\, Berlin office"
488
+ ```
489
+
490
+ - `decodeIcsValue(type, raw, context)` decodes one value. It never throws: it returns
491
+ `{ ok: false, message }`, or `{ ok: true, value }` with `warnings` when it read the value
492
+ leniently. Each warning has the `code` that `parseIcs` would report it under.
493
+ - `context` gives a DATE-TIME its `tzid`, and a `resolveTimeZone(tzid)` function for TZIDs that
494
+ aren't IANA ids. Without one, only ids Temporal knows resolve; Windows names stay `unresolved`.
495
+ - `decodeIcsDateValue(raw, declared, context)` reads a DATE or DATE-TIME by its shape, as the
496
+ typed model does for `DTSTART`, `DTEND` and `EXDATE`, so a date written without `VALUE=DATE`
497
+ still reads as a date.
498
+ - `splitIcsValues(type, raw)` splits a list value, such as `CATEGORIES` or `EXDATE`, on its
499
+ separating commas, leaving escaped commas alone. `decodeIcsValues(type, raw, context)` splits and
500
+ decodes each item, so one bad item doesn't lose the others.
501
+ - `icsValueTypeOf(property)` says which type a property's value is: its `VALUE` parameter, else
502
+ the property's default type in RFC 5545, else TEXT.
503
+ - `encodeIcsValue(type, value)` writes a value as raw text. It throws a `RangeError` for values
504
+ iCalendar can't write, such as a duration in months. A zoned date-time is written as its wall
505
+ time; adding the `TZID` parameter is up to you.
506
+
507
+ ### What the typed model doesn't map
508
+
509
+ Typed components keep what they don't understand. `otherProperties` holds every property no field
510
+ or extension claims, in the order of the text, and `otherComponents` holds unknown subcomponents.
511
+ On attendees and the organizer, `otherParameters` holds parameters such as Google's
512
+ `X-NUM-GUESTS`. All three are in the raw syntax layer's shapes, so the value functions above decode
513
+ them.
514
+
515
+ ## Not included yet
516
+
517
+ - **Writing ICS from typed objects.** `stringifyIcsComponents` writes a syntax tree, which you
518
+ build yourself.
519
+ - **Recurrence expansion.** Rules are parsed into `IcsRecurrenceRule`, not expanded into
520
+ occurrences. [`@moltenship/calendar-recurrence`](./recurrence.md) expands them, so this
521
+ package stays free of dependencies.
522
+ - **Byte input.** `parseIcs` takes a string. Decode bytes as UTF-8 first, for example with
523
+ `response.text()` or `new TextDecoder().decode(bytes)`. A line fold that splits a multi-byte
524
+ character can't be repaired after decoding.