Skip to content

An JS Package to manipulate, build and validate Cronjob Timings.

License

Notifications You must be signed in to change notification settings

l-mbert/crontools

Repository files navigation

Crontools

An Package to manipulate, build and validate Cronjob Timings.


Adding this Package

Using npm

# npm install crontools

Using yarn

# yarn add crontools

Pattern and Naming

I'm using this Wikipedia Article for reference.

Pattern

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute

Naming of the measurements of time

It's just using CamelCase for the name, but for clarification:

Wikipedia Package
minute minute
hour hour
day of the month dayOfTheMonth
month month
day of the week dayOfTheWeek

Using the Cronjob Timing Builder

// If you are using ES6
import CronBuilder from 'crontools';
// If you are using ES5
var CronBuilder = require('crontools');
// ...

var cronTiming = new CronBuilder();

Adding an Value

cronTiming.addValue('minute', 5);
cronTiming.addValue('hour', 3);
cronTiming.addValue('dayOfTheMonth', 15);
cronTiming.addValue('month', 1);
cronTiming.addValue('month', 'JAN-APR');
cronTiming.addValue('month', 'SEP,NOV');
cronTiming.addValue('dayOfTheWeek', '4');

Removing an Value

cronTiming.removeValue('month', 'SEP,NOV');

Getting an Value

cronTiming.get('month');
// returns: "1,JAN-APR"

Getting all Values as an object

cronTiming.getAll();
/* returns:
{
  minute: ['5'],
  hour: ['3'],
  dayOfTheMonth: ['15'],
  month: ['1', 'JAN-APR'],
  dayOfTheWeek: ['4'],
}
*/

Setting an Value

cronTiming.set('minute', ['6', '30']);
// returns: "6,30"

Setting all Values

cronTiming.setAll({
  minute: ['45'],
  hour: ['6'],
  dayOfTheMonth: ['12'],
  month: ['JAN-DEC'],
  dayOfTheWeek: ['*'],
});

Building the current state

cronTiming.build();
// returns: "45 6 12 JAN-DEC *"

Using the Cronjob Timing Validator

If the validation fails, the Validator will throw an Error.

// If you are using ES6
import { CronValidator } from 'crontools';
// If you are using ES5
var { CronValidator } = require('crontools');
// ...

Validating an Timing Expression

CronValidator.validateExpression('* * 1 * *');
// or the object from earlier...
CronValidator.validateExpression({
  minute: ['45'],
  hour: ['6'],
  dayOfTheMonth: ['12'],
  month: ['JAN-DEC'],
  dayOfTheWeek: ['*'],
});

Validating an Timing String explicitly

CronValidator.validateString('* * 1 * *');

Validating a single Timing Value

CronValidator.validateValue('minute', 1);