1.0.1 • Published 3 years ago

pretty-jcal v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

pretty-jcal

Introduction

RFC7265 JSON Format for iCalendar (jCal) is a great to handle iCalendar content in Javascript.

This repo provide a JSON format converter to allow 2-way-convert between the prettyJCAL and jCal.

Installation

yarn add pretty-jcal

or

npm i pretty-jcal

Reason

When dealing with iCalendar(ics) files, we often want a js object representation of the ical object. It's natural for us to use jCal. However, there's a problem.

Example jCal JSON:

[
  "vcalendar",
  [
    ["calscale", {}, "text", "GREGORIAN"],
    ["prodid", {}, "text", "-//Example Inc.//Example Calendar//EN"],
    ["version", {}, "text", "2.0"]
  ],
  [
    [
      "vevent",
      [
        ["dtstamp", {}, "date-time", "2008-02-05T19:12:24Z"],
        ["dtstart", {}, "date", "2008-10-06"],
        ["summary", {}, "text", "Planning meeting"],
        ["uid", {}, "text", "4088E990AD89CB3DBB484909"]
      ],
      []
    ]
  ]
]

While it's compact and efficient, you cannot figure out what is what and you would end up writing code like: calendar[1][2][3] + component[2][3] which is borderline unreadable and unmaintainable.

Solution

We can transform the jCal JSON to make it more like a normal JSON, while it's more verbose, it's much easier to write readable and maintainable code.

Example PrettyJCAL JSON:

{
  "name": "vcalendar",
  "properties": [
    {
      "name": "calscale",
      "type": "text",
      "value": "GREGORIAN"
    },
    {
      "name": "prodid",
      "type": "text",
      "value": "-//Example Inc.//Example Calendar//EN"
    },
    {
      "name": "version",
      "type": "text",
      "value": "2.0"
    }
  ],
  "components": [
    {
      "name": "vevent",
      "properties": [
        {
          "name": "dtstamp",
          "type": "date-time",
          "value": "2008-02-05T19:12:24Z"
        },
        {
          "name": "dtstart",
          "type": "date",
          "value": "2008-10-06"
        },
        {
          "name": "summary",
          "type": "text",
          "value": "Planning meeting"
        },
        {
          "name": "uid",
          "type": "text",
          "value": "4088E990AD89CB3DBB484909"
        }
      ],
      "components": []
    }
  ]
}

Features

  • Readable JSON format with full information about each node, follows JSON convention which only use array when there's multiple objects of same type.
  • jCal PrettyJCAL 2-way-convert, with no loss in information.
  • Built-in Typescript typings, make sure your code strongly typed and safe.

Usage

Caveat

The typescript typing does not verify against jCal standard, it's up to user to make sure only valid properties and components were used.

Function nameParameter typeReturn value typeDescription
jcal2prettyJCALJCALComponentNodePrettyJCALComponentNodeConvert standard jCal to prettyJCAL JSON format
prettyJCAL2jcalPrettyJCALComponentNodeJCALComponentNodeConvert prettyJCAL JSON format to standard jCal

JCALComponentNode (Array type)

PropertyDescription
0Component name
1Component properties
2Children components

JCALPropertyNode (Array type)

PropertyDescription
0name of the property
1parameters of this property
2type of the property value
3property value

PrettyJCALComponentNode (Object type)

PropertyDescription
NameComponent name
propertiesComponent properties
componentsChildren components

PrettyJCALPropertyNode (Object type)

PropertyDescription
namename of the property
parametersparameters of this property
typetype of the property value
valueproperty value

License

MIT