npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sfn-schedule-parser

v0.4.0

Published

A simple friendly Node.js tool for parsing schedule string patterns.

Downloads

17

Readme

SFN-Schedule-Parser

A simple friendly Node.js tool for parsing schedule string patterns in human language.

Notice: this is not a ready-to-use scheduler tool, it just exposes a simple, friendly and common API, so that developers can build schedulers according to the API.

Install

npm i sfn-schedule-parser

Example

const { parse } = require("sfn-schedule-parser");

console.log(parse('2018-2-14 20:00')); // a certain time.
console.log(parse('2018-*-14')); // 14th day of every month in 2018.
console.log(parse('*:30')); // 30 minutes of every hour in today.
console.log(parse('Sat 20:00')); // 20:00 on Saturday in this week.
// every minutes of 20 hours in the 14th day of every month.
console.log(parse('2018-*-14 20:*'));
// February 14th in 2018.
console.log(parse('Dec 25th 2018'));

console.log(parse('every 2 hours'));
console.log(parse('in 2 hours'));
console.log(parse('after 2 hours'));
console.log(parse('every day'));
console.log(parse('every Monday')); // Monday, not Mon or monday.
console.log(parse('tomorrow'));
console.log(parse('the day after tomorrow'));
console.log(parse('the day after 2 days'));

console.log(parse('20:00 every day'));
// 0 minutes of every hour on every Monday.
console.log(parse('*:00 every Monday'));

// the date-time can be specified with an `*/<num>` value, to set an every...
// phrase.
console.log(parse('*/2:00')); // every 2 hours at 0 minutes in today.
console.log(parse('2018-*/2-*/5')) // every 2 months and every 5 days.

Returning Values

The parse() function returns a ScheduleInfo that carries information of:

  • year?: number | string 2018+.
  • day?: number | string Day of week, 0 - 6, 0 represents Sunday.
  • month?: number | string 1 - 12.
  • date?: number | string Day of month, 1 - 31.
  • hours?: number | string 0 - 23.
  • minutes?: number | string 0 - 59.
  • seconds?: number | string 0 - 59.
  • once: bool Whether the schedule should run only once.

Warning: if the schedule pattern is already expired, an RangeError will be thrown, the scheduler may or may not catch this error, but it must not start any job in this situation.

The state of a schedule

The method getState() returns the state of the schedule, possible values are:

  • -1 expired, the schedule should stop now.
  • 0 in position, the schedule should run now;
  • 1 waiting, the schedule should wait for the next tick.

The methods getBestInterval() and getBestTimeout() returns the best interval/timeout value calculated according to the schedule information, so that the scheduler doesn't need to check the state every second when not necessary.

Whether the schedule should run only once or more than once?

You can check this condition by accessing to the read-only property once, if it returns true, that means the callback function of the schedule should run only once, and after that, the scheduler must turn off.

How to build a scheduler?

In JavaScript, you could use setInterval(), setTimeout(), or process.nextTick() in Node.js to build a scheduler, checking the date-time within proper period, and run any callbacks when the time is ripe.

const { parse } = require("sfn-schedule-parser");

var schedule = parse("20:00 every day");
var interval = schedule.getBestInterval(); // returns the best interval value.

let timer = setInterval(() => {
    let state = schedule.getState();

    if (state === 0) {
        // your task...
    } else if (state === -1) {
        clearInterval(timer);
    }
    // normally you don't need to do anything when the state is 1.
}, interval);

Warning: if you're going to use setTimeout() or process.nextTick(), make sure that your program will hang until the schedule is called at least once, unless you stop it manually.

Tip: it's always better to use setTimeout() and getBestTimeout() in a recursive function to form a scheduler, which will protect memory leak and run fewer times of the timer callback function, in which case, more efficient. Look this example:

var schedule = parse("20:00 every day");
var timer = null;
var scheduler = () => {
    let state = schedule.getState();

    if (state === 0) {
        // your task...

        if (schedule.once == false)
            start(); // continue the scheduler and waiting for the next timeout.
    } else if (state === -1) {
        clearTimeout(timer);
    }
};
var start = () => {
    timer = setTimeout(scheduler, schedule.getBestTimeout());
};

start(); // start the scheduler