Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.
/ lunartick Public archive

RFC5545 / iCal - RRule (recurrence rules) Parser for Node.js

License

Notifications You must be signed in to change notification settings

ordermentum/lunartick

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lunartick

npm version Build Status npm npm

Description

Based on the RFC5545 spec for Internet Calendaring and Scheduling Core Object Specification.

Lunartick!! ... get it!?

It's a pun on lunar and a crazy person, because that's what I feel like after working on this. It's a joke.

Anyway...

Lunartick allows you to parse an RRULE string to return an object of the properties in JS format. The returned object has an iterator() to either get the next run time based on a start time passed in (.getNext())or over the next X instances based on the RRULE COUNT or defaults to 1000 if no COUNT is present. If no start date is given, it will fetch the next run time based on the current time.

Please note that this package only supports ES6 friendly codebases.

Usage

NPM users

npm i lunartick

Yarn users

yarn add lunartick

Import the library and pass in an RRULE string to the .parse() method. The resulting instance supports the following properties: frequency, interval, count, bySetPos, byYearDay, byMonth, byMonthDay, byWeekNo, byEaster, byDay, byHour, byMinute, bySecond, tzId and dtStart.

const Rule = require('lunartick');
const rruleString = 'FREQ=DAILY;INTERVAL=1;BYHOUR=14;BYMINUTE=3;BYSECOND=0;DTSTART=19701101T020000';
const rule = Rule.parse(rruleString);
 /*{
   frequency: 2,
   interval: 1,
   byHour: [14],
   byMinute: [3],
   bySecond: [0],
   dtStart: '19701101T020000'
}*/

Once a string has been parsed, it can be converted back to an RRULE string using the .toString() method. This method does not take any parameters.

const string = rule.toString();
// 'FREQ=DAILY;INTERVAL=1;BYHOUR=14;BYMINUTE=3;BYSECOND=0;DTSTART=19701101T020000'

To fetch the next run time for this schedule call the .getNext() method with an optional from date, you can pass in the .dtStart property if you want to use it. If no from date is passed in, it will fetch the next run time base on the current time.

const nextRun = rule.getNext(rule.dtStart);
// Sun Nov 01 1970 14:03:00 GMT+1000

The rule instance will also have an iterator to fetch the next X runtimes for the rule. You can use a for-of loop on rule.iterator() which takes two optional parameters. The first one is the from date (default is also the current time). The second parameter is how many iterations should be fetched. If a number is passed in, it will take precedence, if a .count property exists in the rule, it will be used next and if neither are available it will default to 52 iterations.

const iterator = rule.iterator(rule.dtStart, 5);

for (const nextDate of iterator) {
  nextDate.toString();
}
// OR
Array.from(iterator);
/*
  Sun Nov 01 1970 14:03:00 GMT+1000
  Mon Nov 02 1970 14:03:00 GMT+1000
  Tue Nov 03 1970 14:03:00 GMT+1000
  Wed Nov 04 1970 14:03:00 GMT+1000
  Thu Nov 05 1970 14:03:00 GMT+1000
*/

Limitations

  • Currently does not support multiple values for byDay, byHour, byMinute and bySecond.

Licensing

Lunartick is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Originally developed by Michael Cooper - Development sponsored by Ordermentum.