A nodejs iCalendar file parser

A nodejs iCalendar file parser

UNDER ACTIVE DEVELOPMENT, MAY HAVE BREAKING CHANGES A nodejs iCalendar file parser

@ymchun/ics

UNDER ACTIVE DEVELOPMENT, MAY HAVE BREAKING CHANGES

Install

$ npm i @ymchun/ics

Basic Usage

import { Parser, ParserOptions } from '@ymchun/ics';

const icsStr = '......'; // your ics file content
const opts: ParserOptions = {
    // whether the parser should emit warning message
    quiet: true,
};

// 'calendars' is array of v-calendar components
const calendars = new Parser(opts).parse(icsStr);

Extending components and properties

More components and properties can be supported by extending Component and Property classes.

Also, your custom components and properties should implements ComponentImpl and PropertyImpl<T>.

For PropertyImpl<T>, the generic type refer to the type of your property value.

import { Component, COMPONENT, ComponentImpl, Parser, ParserOptions, Property, PropertyImpl, VCalendar } from '@ymchun/ics';

// define your custom ics property
class MyCustomPropertyA extends Property implements PropertyImpl<string> {
    // type should match your property key when feeding into the parser options
    public type = 'MyCustomPropertyA'; // <-- property key, mandatory
    public value!: string; // <-- property value, mandatory
    public parameters = { // property parameters
        Encoding: null as string | null,
        MyCustomParam: null as string | null,
    };

    // this method should be implemented
    public evaluate(calendar: VCalendar): void {
        // set parameters
        if (this.token.parameters) {
            this.token.parameters.map((param) => {
                switch (param.name) {
                case PARAMETER.Encoding: this.parameters.Encoding = param.value; break;
                case PARAMETER.MyCustomParam: this.parameters.MyCustomParam = param.value; break;
                }
            });
        }
        // your property should use the 'token' property from parent class to set the value
        // since properties may have different value type, so this method is where you parse your own value
        // 'token' will become null after this method is called
        this.value = this.token.value;
    }
}

class MyCustomPropertyB extends Property implements PropertyImpl<Date> {
    // type should match your property key when feeding into the parser options
    public type = 'MyCustomPropertyB'; // <-- property key, mandatory
    public value!: Date; // <-- property value, mandatory

    // this method should be implemented
    public evaluate(calendar: VCalendar): void {
        // or if your property value is a Date object
        this.value = new Date(this.token.value);
    }
}

// define your custom ics component
class MyCustomComponent extends Component implements ComponentImpl {
    // type should match your component key when feeding into the parser options
    public type = 'MyCustomComponent'; // <-- component key, mandatory

    public myCustomPropertyA: MyCustomPropertyA; // <-- use 'MyCustomPropertyA' here
    public myCustomPropertyB: MyCustomPropertyB; // <-- use 'MyCustomPropertyB' here

    public childComponentTypeA: ComponentTypeA;
    public childComponentTypeB: ComponentTypeB;

    public propertyTypeA: PropertyTypeA;
    public propertyTypeB: PropertyTypeB;
    public propertyTypeC: PropertyTypeC;

    // this method should be implemented when your component contains other components
    public setComponent(component: Component): void {
        switch (component.type) {
        case 'TypeA': this.daylight = component as ComponentTypeA; break;
        case 'TypeB': this.daylight = component as ComponentTypeB; break;
        }
    }

    // this method should be implemented when your component has properties
    public setProperty(property: Property): void {
        switch (property.type) {
        case 'TypeA': this.propertyTypeA = property as PropertyTypeA; break;
        case 'TypeB': this.propertyTypeB = property as PropertyTypeB; break;
        case 'TypeC': this.propertyTypeC = property as PropertyTypeC; break;
        //    v-- property key
        case 'MyCustomPropertyA': this.myCustomPropertyA = property as MyCustomPropertyA; break;
        case 'MyCustomPropertyB': this.myCustomPropertyB = property as MyCustomPropertyB; break;
        }
    }
}

// your can also extends existing component
class MyExtendedVCalendar extends VCalendar implements ComponentImpl {
    public myCustomProperty: MyCustomProperty; // <-- use 'MyCustomProperty' here

    // this method should be implemented when your component has properties
    public setProperty(property: Property): void {
        switch (property.type) {
        case 'MyCustomProperty': this.myCustomProperty = property as MyCustomProperty; break;
        default:
            // you should call parent class 'setProperty' method to avoid missing parent properties
            super.setProperty(property);
        }
    }
}

const icsStr = '......'; // your ics file content
const opts: ParserOptions = {
    components: {
        // the key should match the ics 'BEGIN:xxx' and 'END:xxx'
        // i.e. when parser meet 'BEGIN:MyCustomComponent', it will use this component.
        // v-- component key
        'MyCustomComponent': MyCustomComponent,
        // overriding the default vCalendar component
        [COMPONENT.Calendar]: MyExtendedVCalendar,
    },
    properties: {
        // the key should match the ics 'MyCustomPropertyA:xxx' component property
        // v-- property key
        'MyCustomPropertyA': MyCustomPropertyA,
        'MyCustomPropertyB': MyCustomPropertyB,
    },
};

// 'calendars' is array of v-calendar components
const calendars = new Parser(opts).parse(icsStr);

Download Details:

Author: ymchun

Source Code: https://github.com/ymchun/ics

node nodejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Node canvas is a Cairo backed Canvas implementation for NodeJS.

node-canvas is a Cairo-backed Canvas implementation for Node.js.

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

Decoding Nodejs

The main goal of this blog is to explain the “Architecture of Nodejs” and to know how the Nodejs works behind the scenes. Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, and including Nodejs follows multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

Node.js for Beginners - Learn Node.js from Scratch (Step by Step)

Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.