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

cron-string-generator

v1.0.11

Published

Create cron expressions by building them via functions: cronGenerator().every(5).minutes().onDaysOfWeek([1,2]) gives "*/5 * * * 1,2"

Downloads

221

Readme

Cron String Generator

Cron String Generator is a simple, lightweight function which allows you to construct a valid cron string by chaining together functions.

Installation

You can install the package via npm

npm install cron-string-generator

Usage

const cronGenerator = require('cron-string-generator');

// create a cron string which runs every 20 minutes
const cron = new cronGenerator()
  .every(20).minutes();

console.log(cron.toString());
// outputs: */20 * * * *

// create a cron which runs every two hours on the first day of the month
const cron2 = new cronGenerator();
  .atMinute(0)
  .every(2).hours()
  .onDaysOfMonth(1);

console.log(cron2.toString());
// outputs: 0 */2 1 * *

// create a cron which runs monday to friday at 11 o'clock pm
const cron3 = new cronGenerator();
  .atMinute(0)
  .atHour(23)
  .onDaysOfWeek([1,2,3,4,5])

console.log(cron3.toString());
// outputs: 0 23 * * 1,2,3,4,5

Built-in Functions

every(int x)

Takes an integer as its only argument. Sets the frequency of the cron job. This call should be followed by either .minutes(), .hours() or .months() to set the correct unit of time Example: cronGenerator().every(5).minutes() creates a cron every 5 minutes (ie: */5 * * * *), whereas cronGenerator().every(2).months() creates a cron string for every 2 months (ie: * * * */2 *)

betweenMinute(array[2](int from, int to))

Takes an array of exactly two numbers. Sets the range of minutes in which the cron will be active

betweenHours(array[2](int from, int to))

Takes an array of exactly two numbers. Sets the range of hours in which the cron will be active

betweenMonths(array[2](int from, int to))

Takes an array of exactly two numbers. Sets the range of months in which the cron will be active

betweenDaysOfMonth(array[2](int from, int to))

Takes an array of exactly two numbers. Sets the range of days of the month in which the cron will be active

Note:

The "between*" methods will try to automatically sanitise the cases where the start time is greater than the end time. For example: betweenHours([22,2]) will create a string with 22-23,0-2 in the hours part.

atMinute(mixed x)

Can take either an integer between 0 and 59, or an array of integers between 0 and 59. Set the minute(s) in which the cron runs.

atHour(mixed x)

Can take either an integer between 0 and 24, or an array of integers between 0 and 24. Set the hour(s) in which the cron runs.

atMonth(mixed x)

Can take either an integer between 1 and 12, or an array of integers between 1 and 12. Set the months(s) in which the cron runs.

onDaysOfMonth(mixed x)

Can take either an integer between 1 and 31, or an array of integers between 1 and 31. Set the day(s) of the month in which the cron runs.

onDaysOfWeek(mixed x)

Can take either an integer between 0 (sunday) and 6 (saturday), or an array of integers between 0 and 6. Set the day(s) of the week in which the cron runs.

toString()

Returns the value of the cron string as a string - necessary to use it.

Examples

Cron every 2 hours

const cron = new cronGenerator()
  .atMinute(0)
  .every(2).hours()

creates a cron string like 0 */2 * * *

Cron every 5 minutes, only on monday and tuesday

const cron = new cronGenerator()
   .every(5).minutes()
   .onDaysOfWeek([1,2])

creates a cron string like */5 * * * 1,2

Cron every 2 minutes between 23:00 and 01:00

const cron = new cronGenerator()
   .every(2).minutes()
   .betweenHours([23, 1])

creates a cron string like */2 23,0-1 * * *

General Notes

This package is managed by me in my spare time. If you like it, feel free to use and modify as you see fit.

License

ISC