An Package to manipulate, build and validate Cronjob Timings.
Using npm
# npm install crontools
Using yarn
# yarn add crontools
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 |
// If you are using ES6
import CronBuilder from 'crontools';
// If you are using ES5
var CronBuilder = require('crontools');
// ...
var cronTiming = new CronBuilder();
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');
cronTiming.removeValue('month', 'SEP,NOV');
cronTiming.get('month');
// returns: "1,JAN-APR"
cronTiming.getAll();
/* returns:
{
minute: ['5'],
hour: ['3'],
dayOfTheMonth: ['15'],
month: ['1', 'JAN-APR'],
dayOfTheWeek: ['4'],
}
*/
cronTiming.set('minute', ['6', '30']);
// returns: "6,30"
cronTiming.setAll({
minute: ['45'],
hour: ['6'],
dayOfTheMonth: ['12'],
month: ['JAN-DEC'],
dayOfTheWeek: ['*'],
});
cronTiming.build();
// returns: "45 6 12 JAN-DEC *"
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');
// ...
CronValidator.validateExpression('* * 1 * *');
// or the object from earlier...
CronValidator.validateExpression({
minute: ['45'],
hour: ['6'],
dayOfTheMonth: ['12'],
month: ['JAN-DEC'],
dayOfTheWeek: ['*'],
});
CronValidator.validateString('* * 1 * *');
CronValidator.validateValue('minute', 1);