Core parsing functionality for converting between iCalendar/vCard strings and JavaScript objects. ICAL.js supports both the raw string formats defined in RFC 5545 (iCalendar) and RFC 6350 (vCard), as well as their JSON representations (jCal/jCard).
Parses iCalendar or vCard text into jCal/jCard array format.
/**
* Parse iCalendar/vCard string into jCal/jCard array format
* @param {string} input - Raw iCalendar or vCard string
* @returns {Array} jCal/jCard array representation
* @throws {ICAL.parse.ParserError} When input contains syntax errors
*/
ICAL.parse(input);Usage Examples:
const ICAL = require('ical.js');
// Parse a simple iCalendar string
const icalString = `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Test//Test//EN
BEGIN:VEVENT
UID:test@example.com
DTSTART:20230615T100000Z
SUMMARY:Test Event
END:VEVENT
END:VCALENDAR`;
const jcalData = ICAL.parse(icalString);
// Result: ['vcalendar', [['version', {}, 'text', '2.0'], ...], [['vevent', [...], [...]]]]
// Parse a vCard string
const vcardString = `BEGIN:VCARD
VERSION:3.0
FN:John Doe
EMAIL:john@example.com
END:VCARD`;
const jcardData = ICAL.parse(vcardString);
// Result: ['vcard', [['version', {}, 'text', '3.0'], ...], []]Converts jCal/jCard array format back to iCalendar/vCard string.
/**
* Convert jCal/jCard array into iCalendar/vCard string
* @param {Array} jCal - jCal/jCard array data
* @returns {string} Formatted iCalendar/vCard string
*/
ICAL.stringify(jCal);Usage Examples:
const ICAL = require('ical.js');
// Create a simple jCal structure
const jcalData = [
'vcalendar',
[
['version', {}, 'text', '2.0'],
['prodid', {}, 'text', '-//Test//Test//EN']
],
[
['vevent',
[
['uid', {}, 'text', 'test@example.com'],
['dtstart', {}, 'date-time', '20230615T100000Z'],
['summary', {}, 'text', 'Test Event']
],
[]
]
]
];
const icalString = ICAL.stringify(jcalData);
console.log(icalString);
// Output:
// BEGIN:VCALENDAR
// VERSION:2.0
// PRODID:-//Test//Test//EN
// BEGIN:VEVENT
// UID:test@example.com
// DTSTART:20230615T100000Z
// SUMMARY:Test Event
// END:VEVENT
// END:VCALENDARLow-level parsing functions for specific components and properties.
/**
* Parse a single component string
* @param {string} input - Component string to parse
* @param {Object} designSet - Design set for parsing rules (optional)
* @returns {Array} Parsed component in jCal format
*/
ICAL.parse.component(input, designSet);
/**
* Parse a single property string
* @param {string} input - Property string to parse
* @param {Object} designSet - Design set for parsing rules (optional)
* @returns {Array} Parsed property in jCal format
*/
ICAL.parse.property(input, designSet);
/**
* Parse a parameter string
* @param {string} input - Parameter string to parse
* @param {Object} designSet - Design set for parsing rules (optional)
* @returns {Array} Parsed parameter data
*/
ICAL.parse.parameter(input, designSet);
/**
* Parse a parameter value
* @param {string} input - Parameter value to parse
* @param {Object} designSet - Design set for parsing rules (optional)
* @param {string} structuredEscape - Escape character for structured values (optional)
* @returns {string} Parsed parameter value
*/
ICAL.parse.parameterValue(input, designSet, structuredEscape);
/**
* Parse a property value
* @param {string} input - Property value to parse
* @param {Object} designSet - Design set for parsing rules (optional)
* @returns {any} Parsed value in appropriate JavaScript type
*/
ICAL.parse.value(input, designSet);Low-level serialization functions for components and properties.
/**
* Convert a component to iCalendar string
* @param {Array} component - Component in jCal format
* @param {Object} designSet - Design set for serialization rules (optional)
* @returns {string} Component as iCalendar string
*/
ICAL.stringify.component(component, designSet);
/**
* Convert a property to iCalendar string
* @param {Array} property - Property in jCal format
* @param {Object} designSet - Design set for serialization rules (optional)
* @param {boolean} noFold - Disable line folding (optional)
* @returns {string} Property as iCalendar string
*/
ICAL.stringify.property(property, designSet, noFold);
/**
* Convert a property value to string
* @param {any} value - Value to stringify
* @returns {string} Stringified value
*/
ICAL.stringify.propertyValue(value);
/**
* Convert multiple values to delimited string
* @param {Array} values - Array of values to stringify
* @param {string} delim - Delimiter character
* @param {string} type - Value type
* @param {Object} designSet - Design set (optional)
* @param {string} structuredEscape - Escape character (optional)
* @returns {string} Multi-value string
*/
ICAL.stringify.multiValue(values, delim, type, designSet, structuredEscape);
/**
* Convert parameter value to string
* @param {any} value - Parameter value to stringify
* @param {string} structuredEscape - Escape character (optional)
* @param {Object} designSet - Design set (optional)
* @returns {string} Stringified parameter value
*/
ICAL.stringify.parameterValue(value, structuredEscape, designSet);/**
* Parser-specific error class
* @extends Error
*/
class ICAL.parse.ParserError extends Error {
constructor(message);
// Properties
message: string;
name: string;
stack: string;
}Error Handling Examples:
const ICAL = require('ical.js');
try {
const result = ICAL.parse('INVALID:CALENDAR:DATA');
} catch (error) {
if (error instanceof ICAL.parse.ParserError) {
console.error('Parse error:', error.message);
} else {
console.error('Unexpected error:', error);
}
}ICAL.js uses design sets to define parsing and serialization rules for different formats.
/**
* Get design set for component type
* @param {string} componentName - Name of component (e.g., 'vcalendar', 'vcard')
* @returns {Object} Design set object with parsing/serialization rules
*/
ICAL.design.getDesignSet(componentName);
// Available design sets
ICAL.design.icalendar; // iCalendar (RFC 5545) design set
ICAL.design.vcard3; // vCard 3.0 design set
ICAL.design.vcard4; // vCard 4.0 design setDesign Set Examples:
const ICAL = require('ical.js');
// Parse with specific design set
const vcardDesign = ICAL.design.vcard4;
const property = ICAL.parse.property('FN:John Doe', vcardDesign);
// Use design set in component parsing
const designSet = ICAL.design.getDesignSet('vcard');
const component = ICAL.parse.component(vcardString, designSet);jCal represents iCalendar data as nested arrays:
[
"component-name",
[
["property-name", {parameters}, "value-type", "value"],
// ... more properties
],
[
// ... nested subcomponents in same format
]
]jCard represents vCard data similarly:
[
"vcard",
[
["version", {}, "text", "4.0"],
["fn", {}, "text", "John Doe"],
["email", {"type": "work"}, "text", "john@example.com"]
],
[] // vCard typically has no subcomponents
]Common value types supported by the parser:
textdatedate-timedurationperiodrecurintegerfloatbooleanuribinaryutc-offset