@thomasfosterau/effect-ical
v0.1.0
Published
iCalendar (RFC 5545) and vCard (RFC 6350) encoding and decoding for Effect: typed components, parsers, encoders and pipeable builders.
Maintainers
Readme
@thomasfosterau/effect-ical
iCalendar (RFC 5545) and vCard (RFC 6350) encoding and decoding for Effect: a typed object model for VCALENDAR data, a parser and an encoder (as pure Result functions and as Effects), fluent builders, pipeable Calendar / Event / Todo modules, a vCard 4.0 codec, and the content-line layer the two formats share.
- Components:
VEVENT,VTODO,VJOURNAL,VFREEBUSY,VTIMEZONE(withSTANDARD/DAYLIGHT) andVALARM. - The encoder escapes TEXT values, folds lines at 75 octets (UTF-8, never splitting a character) and writes CRLF line endings; the parser unfolds lines and collects repeatable properties (
ATTENDEE,CATEGORIES,RDATE,EXDATE) into arrays. - TEXT values are unescaped when parsed and escaped when encoded, so
parse(encode(calendar))round-trips them. Other values stay verbatim strings: dates, durations and recurrence rules are not decoded. - Failures are tagged errors (
ParseError,EncodeError,BuildError) you can handle withEffect.catchTag.
| Package | Purpose | Peers |
| --- | --- | --- |
| @thomasfosterau/effect-ical | iCalendar parse, encode and build | effect ^4.0.0, @thomasfosterau/effect-parse ^0.4.1 |
Install
pnpm add @thomasfosterau/effect-ical @thomasfosterau/effect-parse effectThe package is ESM only and has no runtime dependencies besides its peers: effect, and @thomasfosterau/effect-parse, whose compiled grammar scans the content lines.
Quick start
import { encodeSync, parse } from "@thomasfosterau/effect-ical";
import type { VCalendar } from "@thomasfosterau/effect-ical";
import * as Effect from "effect/Effect";
const calendar: VCalendar = {
component: "VCALENDAR",
version: "2.0",
prodid: "-//Example Corp//Example App//EN",
events: [
{
component: "VEVENT",
uid: "[email protected]",
dtstart: "20240115T100000Z",
dtend: "20240115T110000Z",
summary: "Team standup",
location: "Room A",
},
],
};
const text = encodeSync(calendar); // "BEGIN:VCALENDAR\r\nVERSION:2.0\r\n…"
const program = parse(text).pipe(
Effect.map((parsed) => parsed.events?.[0]?.summary), // "Team standup"
Effect.catchTag("ParseError", (error) => Effect.succeed(`invalid: ${error.message}`)),
);API
Everything is exported from the package root.
Parsing
| Export | Signature | Notes |
| --- | --- | --- |
| parse | (text: string) => Effect<VCalendar, ParseError> | Runs when the Effect runs. |
| parseResult | (text: string) => Result<VCalendar, ParseError> | The same parse, synchronously. |
| ParseError | tagged error, _tag: "ParseError", message, optional line / column | No VCALENDAR, an unclosed calendar, a line without :, a missing or non-2.0 VERSION, or a missing PRODID. For a line without : or an unclosed component, line and column (1-based) locate the offending line or the component's BEGIN line. |
Text before the first BEGIN:VCALENDAR and after its END:VCALENDAR is ignored. Nothing inside it is lost: every component keeps the properties it does not type (X- properties, SEQUENCE, CREATED, TRANSP, GEO, FREEBUSY, …) in extra, as ContentLine values in their original order with raw values and parameters, and the sub-components of a type it does not hold (VAVAILABILITY, an X- component, a VALARM in a VJOURNAL) in components, verbatim as UnknownComponents. Property names become lower-cased fields (PERCENT-COMPLETE → percent, LAST-MODIFIED → lastmod, X-WR-CALNAME → "x-wr-calname" on a component, kept as before alongside extra, which is the documented way to read them); PRIORITY, PERCENT-COMPLETE, REPEAT and SEQUENCE become numbers when they are integers. TEXT values (SUMMARY, DESCRIPTION, LOCATION, COMMENT, CONTACT, NAME, TZNAME, X-WR-CALNAME, X-WR-CALDESC) are unescaped (\\, \;, \,, \n / \N; other backslash sequences are kept), and each CATEGORIES line splits at its unescaped commas. The parameters of the typed fields (DTSTART;TZID=…, ATTENDEE;CN=…) are kept in the component's parameters, keyed by field name, with one parameter list per content line: event.parameters?.dtstart?.[0] is DTSTART's, and event.parameters?.attendee?.[i] is the i-th attendee's. A field is present only when one of its lines carried parameters. Lines are read with ContentLine.parse, so a quoted : in a parameter value stays in the parameter; a line it rejects (an unterminated quote) is still read, without parameters.
import { parseResult } from "@thomasfosterau/effect-ical";
import * as Result from "effect/Result";
const result = parseResult(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Example//EN\r\nEND:VCALENDAR\r\n",
);
if (Result.isSuccess(result)) {
console.log(result.success.prodid); // "-//Example//EN"
} else {
console.log(result.failure.message);
}Encoding
| Export | Signature | Notes |
| --- | --- | --- |
| encode | (calendar: VCalendar) => Effect<string, EncodeError> | Runs when the Effect runs. |
| encodeResult | (calendar: VCalendar) => Result<string, EncodeError> | Synchronous. |
| encodeSync | (calendar: VCalendar) => string | Throws the EncodeError. |
| EncodeError | tagged error, _tag: "EncodeError", message | version or prodid missing or empty (only possible for a value that bypassed the types). |
Components are written in a fixed order: timezones, events, to-dos, journals, free/busy. Optional string properties that are empty are omitted; numeric ones (priority, percent, repeat) are written whenever set, 0 included. SUMMARY, DESCRIPTION, LOCATION, NAME, TZNAME and each CATEGORIES item are escaped (\, ;, ,, newline); every other value is written as given. Each typed field's line carries the parameters its component's parameters keeps for it, so encode(parse(text)) keeps TZID, VALUE=DATE, CN, PARTSTAT and the rest. A component's extra lines follow its typed fields, and its unknown components follow its typed sub-components, so encode(parse(text)) writes back every line text held, possibly in another order. geo and freebusy are written only when they hold the structures the model types (the parser keeps those lines in extra).
The object model
VCalendar, VEvent, VTodo, VJournal, VFreeBusy, VTimezone and VAlarm are plain TypeScript interfaces, one per component, each with a component discriminant ("VEVENT", …). VCalendar holds the components in timezones, events, todos, journals and freebusys; events and to-dos hold their alarms in alarms. Every component (and each STANDARD / DAYLIGHT Observance) may also carry parameters (FieldParameters), the parameters of its typed fields, extra, its untyped property lines, and components, its unknown sub-components (Preserved).
import { encodeSync } from "@thomasfosterau/effect-ical";
import type { VCalendar } from "@thomasfosterau/effect-ical";
const calendar: VCalendar = {
component: "VCALENDAR",
version: "2.0",
prodid: "-//Example Corp//Example App//EN",
todos: [
{
component: "VTODO",
uid: "[email protected]",
due: "20240205T170000Z",
summary: "Write the proposal",
status: "IN-PROCESS",
percent: 50,
alarms: [
{ component: "VALARM", action: "DISPLAY", trigger: "-PT1H", description: "Due soon" },
],
},
],
};
const text = encodeSync(calendar);Fluent builders
createCalendar(prodid), createEvent() and createTodo() return mutable builders (CalendarBuilder, EventBuilder, TodoBuilder) whose setters return the builder, so calls chain. build() returns the object model value; it throws a BuildError when an event has no dtstart or a calendar no prodid. CalendarBuilder also has toICalString() (via encodeSync) and toICalEffect() (via encode).
import { createCalendar, createEvent } from "@thomasfosterau/effect-ical";
const text = createCalendar("-//Example Corp//Example App//EN")
.name("Work")
.event((e) =>
e
.uid("[email protected]")
.dtstart("20240115T140000Z")
.dtend("20240115T150000Z")
.summary("Code review")
.addAttendee("mailto:[email protected]")
.addDisplayAlarm("-PT15M", "Review in 15 minutes"),
)
.todo((t) => t.uid("[email protected]").summary("Send notes").priority(1))
.toICalString();
const event = createEvent()
.uid("[email protected]")
.dtstart("20240301T090000Z")
.summary("Focus time")
.build();Event setters: uid, dtstart, dtend, duration, summary, description, location, organizer, addAttendee, attendees, categories, status, rrule, url, geo(lat, lon), priority, classification, dtstamp, addAlarm, addDisplayAlarm(trigger, description?). To-do setters: uid, dtstart, due, duration, summary, description, location, status, percentComplete (clamped to 0–100), completed, priority, dtstamp, rrule, addAlarm. Calendar setters: prodid, calscale, method, name, description, addEvent, event(fn), addTodo, todo(fn), addJournal, addTimezone.
Pipeable modules: Calendar, Event, Todo
Each module defines an Effect Schema class of the same name plus a make constructor and pipeable with* setters that return a new instance of the class (built with its constructor, so instanceof holds and Schema encoding works; properties the class does not declare are not carried over).
import { Calendar, Event, Todo } from "@thomasfosterau/effect-ical";
import * as Effect from "effect/Effect";
import { pipe } from "effect/Function";
const standup = pipe(
Event.make("20240115T100000Z"),
Event.withUid("[email protected]"),
Event.withSummary("Team standup"),
Event.withStatus("CONFIRMED"),
);
const proposal = pipe(Todo.make(), Todo.withSummary("Write the proposal"), Todo.withPercent(60));
const calendar = pipe(
Calendar.builder(),
Calendar.withProdid("-//Example Corp//Example App//EN"),
Calendar.withName("Work"),
Calendar.withEvent(standup),
Calendar.withTodo(proposal),
Calendar.build,
);
const text = await Effect.runPromise(Calendar.encode(calendar));Event:make(dtstart),withUid,withDtend,withDuration,withSummary,withDescription,withLocation,withOrganizer,withAttendee,withCategories,withStatus,withRrule,withUrl,withPriority,withClassification, andencode(event): Effect<Array<string>>(the event's content lines, unescaped and unfolded).Todo:make(),withUid,withDtstart,withDue,withSummary,withDescription,withLocation,withStatus,withPercent(clamped to 0–100),withCompleted,withPriority,withRrule, andencode(todo): Effect<Array<string>>.Calendar:make(prodid),builder(),withProdid,withName,withDescription,withCalscale,withMethod,withEvent,withTodo,build, and:encode(calendar): Effect<string>: a compact rendering. Values are not escaped or folded, and only an event'sDTSTART,UID,SUMMARY,DTEND,DESCRIPTION,LOCATION,STATUSand a to-do'sUID,SUMMARY,DTSTART,DUE,STATUS,PERCENT-COMPLETEare written. For complete output, use the rootencodeon aVCalendar.decode(text): Effect<Calendar, ParseError>: a shallow reader of the top-levelPRODID,VERSION,NAME,DESCRIPTION,CALSCALEandMETHOD. It does not unfold lines or read components, and a matching line inside a component also counts (the last one wins). For a full parse, use the rootparse.
Content lines: ContentLine
ContentLine is the line syntax iCalendar shares with vCard (RFC 5545 §3.1, RFC 6350 §3.3): [group "."] name *(";" param) ":" value, folded at 75 octets. It knows nothing about components; the iCalendar parser and encoder are built on it, and so can other formats.
| Export | Signature | Notes |
| --- | --- | --- |
| ContentLine.unfold | (text) => ReadonlyArray<{ text, offset }> | Unfolds continuation lines; splits at \r\n or \n; drops empty lines. |
| ContentLine.parse | (line) => Result<ContentLine, ContentLineError> | One unfolded line. The value starts at the first : outside a quoted parameter value; names and parameter names are upper-cased; parameter values are unquoted and RFC 6868 caret-decoded (^n, ^', ^^); the value is verbatim. |
| ContentLine.parseAll | (text) => Result<ReadonlyArray<ContentLine>, ContentLineError> | Unfolds and parses every line; the error carries the failing line's 1-based line / column. |
| ContentLine.format | (line) => string | Writes and folds a line, quoting parameter values that contain :, ; or , and caret-encoding them. The value is written as given. |
| ContentLine.getParameter | (line, name) => Option<ReadonlyArray<string>> | The first parameter's values, by case-insensitive name. |
| ContentLine.escapeText / unescapeText | (value) => string | TEXT escaping (\\, \;, \,, \n); unescaping also reads \N and keeps unknown escapes. |
| ContentLine.splitValue | (value, ";" \| ",") => ReadonlyArray<string> | Splits at unescaped separators only, for structured values such as vCard N and ADR. |
| ContentLine.fold | (line) => string | Folds a formatted line at 75 octets (UTF-8) without splitting a code point. |
import { ContentLine } from "@thomasfosterau/effect-ical";
import * as Result from "effect/Result";
const line = Result.getOrThrow(
ContentLine.parse('ATTENDEE;CN="Doe, Jane";PARTSTAT=ACCEPTED:mailto:[email protected]'),
);
line.parameters; // [{ name: "CN", values: ["Doe, Jane"] }, { name: "PARTSTAT", values: ["ACCEPTED"] }]
line.value; // "mailto:[email protected]"
ContentLine.format(line); // 'ATTENDEE;CN="Doe, Jane";PARTSTAT=ACCEPTED:mailto:[email protected]'The root parse reads every line with ContentLine.parse, falling back to its earlier split at the first : (without parameters) for a line ContentLine.parse rejects.
vCard 4.0: VCard
VCard reads and writes vCard 4.0 (RFC 6350) contact cards on the same content-line layer. A .vcf input may hold any number of cards.
| Export | Signature | Notes |
| --- | --- | --- |
| VCard.parse | (text) => Effect<ReadonlyArray<VCard>, VCardParseError> | Runs when the Effect runs. |
| VCard.parseResult | (text) => Result<ReadonlyArray<VCard>, VCardParseError> | Synchronous. Lines outside cards are ignored. Fails on a malformed line inside a card (with line / column), an unclosed or nested card, and a card without VERSION:4.0 or FN. |
| VCard.encode / encodeResult | (card \| cards) => Effect<string, VCardEncodeError> / Result<…> | Writes BEGIN, VERSION:4.0, the typed properties in a fixed order, then extra in its order; folds at 75 octets; CRLF. |
| VCard.make | (fn) => VCard | A minimal card. |
| VCard.types | (property) => ReadonlyArray<string> | The TYPE values, lower-cased, with TYPE="work,voice" split. |
| VCard.preferred | (properties) => Option<Property> | The property with the lowest PREF. |
Every property instance is a Property<A>: { value, parameters?, group? }. Fields are named after their properties (fn, n, email, tel, adr, org, …); fn is a non-empty list, the at-most-once properties (n, bday, anniversary, gender, prodid, rev, uid, kind) are single, and the rest are lists.
- TEXT values (
FN,NOTE,EMAIL,TITLE,ROLE, …) are unescaped, unless the property saysVALUE=uri;NICKNAMEandCATEGORIESare comma lists. N(5 components),ADR(7) andGENDER(sex; identity) are structured, eachN/ADRcomponent a list;ORGis the organisation name then its units.- URIs, dates and other values (
TEL,URL,PHOTO,BDAY,TZ,UID, …) are verbatim strings. - Nothing is lost: properties the model does not type (
X-…,item1.X-ABLabel), a secondUID, and anNorADRwith too many components are kept inextrain their order and written back.
vCard 3.0 and 2.1 are not read yet (#27).
import { VCard } from "@thomasfosterau/effect-ical";
import * as Option from "effect/Option";
import * as Result from "effect/Result";
const [jane] = Result.getOrThrow(
VCard.parseResult(
"BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Jane Doe\r\nN:Doe;Jane;;;\r\n" +
"EMAIL;TYPE=work;PREF=1:[email protected]\r\nEND:VCARD\r\n",
),
);
jane?.n?.value.givenNames; // ["Jane"]
Option.map(VCard.preferred(jane?.email ?? []), (email) => email.value); // Some("[email protected]")
const text = Result.getOrThrow(VCard.encodeResult(VCard.make("John Doe")));
// "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:John Doe\r\nEND:VCARD\r\n"Examples
The repository's examples/ directory has four runnable programs, one per API style plus one for vCard; its test suite runs each of them.
Standards
Implements RFC 5545 content lines, components and escaping. ContentLine implements RFC 6868 parameter value encoding. Extensions from RFC 5546 (iTIP), RFC 7529 (non-Gregorian recurrence) and RFC 7986 (new properties) are not implemented, though their properties pass through the parser as plain fields.
Changelog
Release notes are in the package's CHANGELOG.md, written by Changesets.
License
MIT. See the LICENSE file in the package.
