cron-string-generator

1.0.11 • Public • Published

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

Package Sidebar

Install

npm i cron-string-generator

Weekly Downloads

42

Version

1.0.11

License

ISC

Unpacked Size

14 kB

Total Files

3

Last publish

Collaborators

  • slartibartfast