Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

node-cron

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-cron - npm Package Compare versions

Comparing version 2.0.3 to 3.0.0

.eslintrc.yml

25

package.json
{
"name": "node-cron",
"version": "2.0.3",
"version": "3.0.0",
"description": "A simple cron-like task scheduler for Node.js",

@@ -12,4 +12,4 @@ "author": "Lucas Merencia",

"coverage": "nyc report --reporter=text-lcov | coveralls",
"check": "npm test && npm run coverage",
"postinstall": "opencollective-postinstall"
"lint": "./node_modules/.bin/eslint ./src ./test",
"check": "npm run lint && npm test && npm run coverage"
},

@@ -34,20 +34,13 @@ "engines": {

"devDependencies": {
"chai": "^4.2.0",
"coveralls": "^3.0.2",
"expect.js": "^0.3.1",
"eslint": "^5.7.0",
"istanbul": "^0.4.2",
"mocha": "^4.0.1",
"nyc": "^13.0.1",
"sinon": "^4.1.2"
"mocha": "^6.1.4",
"nyc": "^14.0.0",
"sinon": "^7.3.2"
},
"collective": {
"type": "opencollective",
"url": "https://opencollective.com/node-cron",
"mocha": "^5.2.0",
"nyc": "^13.0.1",
"sinon": "^6.2.0"
},
"dependencies": {
"opencollective-postinstall": "^2.0.0",
"tz-offset": "0.0.1"
"moment-timezone": "^0.5.31"
}
}

@@ -15,2 +15,4 @@ # Node Cron

**Need a job scheduler with support for worker threads and cron syntax?** Try out the [Bree](https://github.com/breejs/bree) job scheduler!
[![NPM](https://nodei.co/npm/node-cron.png?downloads=true&downloadRank=true&stars=false)](https://nodei.co/npm/node-cron/)

@@ -139,4 +141,4 @@

- **scheduled**: A `boolean` to set if the created task is schaduled. Default `true`;
- **timezone**: The timezone that is used for job scheduling;
- **scheduled**: A `boolean` to set if the created task is scheduled. Default `true`;
- **timezone**: The timezone that is used for job scheduling. See [moment-timezone](https://momentjs.com/timezone) for valid values.

@@ -149,3 +151,3 @@ **Example**:

cron.schedule('0 1 * * *', () => {
console.log('Runing a job at 01:00 at America/Sao_Paulo timezone');
console.log('Running a job at 01:00 at America/Sao_Paulo timezone');
}, {

@@ -167,3 +169,3 @@ scheduled: true,

var task = cron.schedule('* * * * *', () => {
console.log('stoped task');
console.log('stopped task');
}, {

@@ -219,3 +221,3 @@ scheduled: false

## Contributors
## Contributing

@@ -235,3 +237,3 @@ In general, we follow the "fork-and-pull" Git workflow.

This project exists thanks to all the people who contribute.
<a href="graphs/contributors"><img src="https://opencollective.com/node-cron/contributors.svg?width=890&button=false" /></a>
<a href="https://github.com/node-cron/node-cron/graphs/contributors"><img src="https://opencollective.com/node-cron/contributors.svg?width=890&button=false" /></a>

@@ -238,0 +240,0 @@

'use strict';
module.exports = (() => {
function convertAsterisk(expression, replecement){
if(expression.indexOf('*') !== -1){
return expression.replace('*', replecement);
function convertAsterisk(expression, replecement){
if(expression.indexOf('*') !== -1){
return expression.replace('*', replecement);
}
return expression;
}
return expression;
}
function convertAsterisksToRanges(expressions){
expressions[0] = convertAsterisk(expressions[0], '0-59');
expressions[1] = convertAsterisk(expressions[1], '0-59');
expressions[2] = convertAsterisk(expressions[2], '0-23');
expressions[3] = convertAsterisk(expressions[3], '1-31');
expressions[4] = convertAsterisk(expressions[4], '1-12');
expressions[5] = convertAsterisk(expressions[5], '0-6');
return expressions;
}
function convertAsterisksToRanges(expressions){
expressions[0] = convertAsterisk(expressions[0], '0-59');
expressions[1] = convertAsterisk(expressions[1], '0-59');
expressions[2] = convertAsterisk(expressions[2], '0-23');
expressions[3] = convertAsterisk(expressions[3], '1-31');
expressions[4] = convertAsterisk(expressions[4], '1-12');
expressions[5] = convertAsterisk(expressions[5], '0-6');
return expressions;
}
return convertAsterisksToRanges;
return convertAsterisksToRanges;
})();
'use strict';
var monthNamesConversion = require('./month-names-conversion');
var weekDayNamesConversion = require('./week-day-names-conversion');
var convertAsterisksToRanges = require('./asterisk-to-range-conversion');
var convertRanges = require('./range-conversion');
var convertSteps = require('./step-values-conversion');
const monthNamesConversion = require('./month-names-conversion');
const weekDayNamesConversion = require('./week-day-names-conversion');
const convertAsterisksToRanges = require('./asterisk-to-range-conversion');
const convertRanges = require('./range-conversion');
const convertSteps = require('./step-values-conversion');
module.exports = (() => {
function appendSeccondExpression(expressions){
if(expressions.length === 5){
return ['0'].concat(expressions);
function appendSeccondExpression(expressions){
if(expressions.length === 5){
return ['0'].concat(expressions);
}
return expressions;
}
return expressions;
}
function removeSpaces(str) {
return str.replace(/\s{2,}/g, ' ').trim();
}
function removeSpaces(str) {
return str.replace(/\s{2,}/g, ' ').trim();
}
// Function that takes care of normalization.
function normalizeIntegers(expressions) {
for (var i=0; i < expressions.length; i++){
var numbers = expressions[i].split(',');
for (var j=0; j<numbers.length; j++){
numbers[j] = parseInt(numbers[j]);
}
expressions[i] = numbers;
// Function that takes care of normalization.
function normalizeIntegers(expressions) {
for (let i=0; i < expressions.length; i++){
const numbers = expressions[i].split(',');
for (let j=0; j<numbers.length; j++){
numbers[j] = parseInt(numbers[j]);
}
expressions[i] = numbers;
}
return expressions;
}
return expressions;
}
/*
/*
* The node-cron core allows only numbers (including multiple numbers e.g 1,2).

@@ -51,17 +51,17 @@ * This module is going to translate the month names, week day names and ranges

*/
function interprete(expression){
var expressions = removeSpaces(expression).split(' ');
expressions = appendSeccondExpression(expressions);
expressions[4] = monthNamesConversion(expressions[4]);
expressions[5] = weekDayNamesConversion(expressions[5]);
expressions = convertAsterisksToRanges(expressions);
expressions = convertRanges(expressions);
expressions = convertSteps(expressions);
function interprete(expression){
let expressions = removeSpaces(expression).split(' ');
expressions = appendSeccondExpression(expressions);
expressions[4] = monthNamesConversion(expressions[4]);
expressions[5] = weekDayNamesConversion(expressions[5]);
expressions = convertAsterisksToRanges(expressions);
expressions = convertRanges(expressions);
expressions = convertSteps(expressions);
expressions = normalizeIntegers(expressions);
expressions = normalizeIntegers(expressions);
return expressions.join(' ');
}
return expressions.join(' ');
}
return interprete;
return interprete;
})();
'use strict';
module.exports = (() => {
var months = ['january','february','march','april','may','june','july',
'august','september','october','november','december'];
var shortMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'];
const months = ['january','february','march','april','may','june','july',
'august','september','october','november','december'];
const shortMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'];
function convertMonthName(expression, items){
for(var i = 0; i < items.length; i++){
expression = expression.replace(new RegExp(items[i], 'gi'), parseInt(i, 10) + 1);
function convertMonthName(expression, items){
for(let i = 0; i < items.length; i++){
expression = expression.replace(new RegExp(items[i], 'gi'), parseInt(i, 10) + 1);
}
return expression;
}
return expression;
}
function interprete(monthExpression){
monthExpression = convertMonthName(monthExpression, months);
monthExpression = convertMonthName(monthExpression, shortMonths);
return monthExpression;
}
function interprete(monthExpression){
monthExpression = convertMonthName(monthExpression, months);
monthExpression = convertMonthName(monthExpression, shortMonths);
return monthExpression;
}
return interprete;
return interprete;
})();
'use strict';
module.exports = ( () => {
function replaceWithRange(expression, text, init, end) {
function replaceWithRange(expression, text, init, end) {
var numbers = [];
var last = parseInt(end);
var first = parseInt(init);
const numbers = [];
let last = parseInt(end);
let first = parseInt(init);
if(first > last){
last = parseInt(init);
first = parseInt(end);
}
if(first > last){
last = parseInt(init);
first = parseInt(end);
}
for(var i = first; i <= last; i++) {
numbers.push(i);
for(let i = first; i <= last; i++) {
numbers.push(i);
}
return expression.replace(new RegExp(text, 'i'), numbers.join());
}
return expression.replace(new RegExp(text, 'gi'), numbers.join());
}
function convertRange(expression){
var rangeRegEx = /(\d+)\-(\d+)/;
var match = rangeRegEx.exec(expression);
while(match !== null && match.length > 0){
expression = replaceWithRange(expression, match[0], match[1], match[2]);
match = rangeRegEx.exec(expression);
function convertRange(expression){
const rangeRegEx = /(\d+)-(\d+)/;
let match = rangeRegEx.exec(expression);
while(match !== null && match.length > 0){
expression = replaceWithRange(expression, match[0], match[1], match[2]);
match = rangeRegEx.exec(expression);
}
return expression;
}
return expression;
}
function convertAllRanges(expressions){
for(var i = 0; i < expressions.length; i++){
expressions[i] = convertRange(expressions[i]);
function convertAllRanges(expressions){
for(let i = 0; i < expressions.length; i++){
expressions[i] = convertRange(expressions[i]);
}
return expressions;
}
return expressions;
}
return convertAllRanges;
return convertAllRanges;
})();
'use strict';
module.exports = (() => {
function convertSteps(expressions){
var stepValuePattern = /^(.+)\/(\d+)$/;
for(var i = 0; i < expressions.length; i++){
var match = stepValuePattern.exec(expressions[i]);
var isStepValue = match !== null && match.length > 0;
if(isStepValue){
var values = match[1].split(',');
var setpValues = [];
var divider = parseInt(match[2], 10);
for(var j = 0; j <= values.length; j++){
var value = parseInt(values[j], 10);
if(value % divider === 0){
setpValues.push(value);
}
function convertSteps(expressions){
var stepValuePattern = /^(.+)\/(\w+)$/;
for(var i = 0; i < expressions.length; i++){
var match = stepValuePattern.exec(expressions[i]);
var isStepValue = match !== null && match.length > 0;
if(isStepValue){
var baseDivider = match[2];
if(isNaN(baseDivider)){
throw baseDivider + ' is not a valid step value';
}
var values = match[1].split(',');
var stepValues = [];
var divider = parseInt(baseDivider, 10);
for(var j = 0; j <= values.length; j++){
var value = parseInt(values[j], 10);
if(value % divider === 0){
stepValues.push(value);
}
}
expressions[i] = stepValues.join(',');
}
}
expressions[i] = setpValues.join(',');
}
return expressions;
}
return expressions;
}
return convertSteps;
return convertSteps;
})();
'use strict';
module.exports = (() => {
var weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
'friday', 'saturday'];
var shortWeekDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
const weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
'friday', 'saturday'];
const shortWeekDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
function convertWeekDayName(expression, items){
for(var i = 0; i < items.length; i++){
expression = expression.replace(new RegExp(items[i], 'gi'), parseInt(i, 10));
function convertWeekDayName(expression, items){
for(let i = 0; i < items.length; i++){
expression = expression.replace(new RegExp(items[i], 'gi'), parseInt(i, 10));
}
return expression;
}
return expression;
}
function convertWeekDays(expression){
expression = expression.replace('7', '0');
expression = convertWeekDayName(expression, weekDays);
return convertWeekDayName(expression, shortWeekDays);
}
function convertWeekDays(expression){
expression = expression.replace('7', '0');
expression = convertWeekDayName(expression, weekDays);
return convertWeekDayName(expression, shortWeekDays);
}
return convertWeekDays;
return convertWeekDays;
})();
'use strict';
var Task = require('./task'),
ScheduledTask = require('./scheduled-task'),
validation = require('./pattern-validation');
const ScheduledTask = require('./scheduled-task');
const BackgroundScheduledTask = require('./background-scheduled-task');
const validation = require('./pattern-validation');
const storage = require('./storage');
module.exports = (() => {
/**
/**
* Creates a new task to execute given function when the cron

@@ -28,35 +29,40 @@ * expression ticks.

*/
function createTask(expression, func, options) {
// Added for immediateStart depreciation
if(typeof options === 'boolean'){
console.warn('DEPRECIATION: imediateStart is deprecated and will be removed soon in favor of the options param.');
options = {
scheduled: options
};
function schedule(expression, func, options) {
let task = createTask(expression, func, options);
storage.save(task);
return task;
}
if(!options){
options = {
scheduled: true
};
function createTask(expression, func, options){
if(typeof func === 'string'){
return new BackgroundScheduledTask(expression, func, options);
}
return new ScheduledTask(expression, func, options);
}
var task = new Task(expression, func);
return new ScheduledTask(task, options);
}
/**
* Check if a cron expression is valid
* @param {string} expression - cron expression.
*
* @returns {boolean} - returns true if expression is valid
*/
function validate(expression) {
try {
validation(expression);
} catch(e) {
return false;
}
function validate(expression) {
try {
validation(expression);
} catch(e) {
return false;
return true;
}
return true;
}
function getTasks() {
return storage.getTasks();
}
return {
schedule: createTask,
validate: validate
};
return {
schedule: schedule,
validate: validate,
getTasks: getTasks
};
})();
'use strict';
var convertExpression = require('./convert-expression');
const convertExpression = require('./convert-expression');
module.exports = ( () => {
function isValidExpression(expression, min, max){
var options = expression.split(',');
var regexValidation = /^\d+$|^\*$|^\*\/\d+$/;
for(var i = 0; i < options.length; i++){
var option = options[i];
var optionAsInt = parseInt(options[i], 10);
if(optionAsInt < min || optionAsInt > max || !regexValidation.test(option)) {
return false;
}
function isValidExpression(expression, min, max){
const options = expression.split(',');
const regexValidation = /^\d+$|^\*$|^\*\/\d+$/;
for(let i = 0; i < options.length; i++){
const option = options[i];
const optionAsInt = parseInt(options[i], 10);
if(optionAsInt < min || optionAsInt > max || !regexValidation.test(option)) {
return false;
}
}
return true;
}
return true;
}
function isInvalidSecond(expression){
return !isValidExpression(expression, 0, 59);
}
function isInvalidMinute(expression){
return !isValidExpression(expression, 0, 59);
}
function isInvalidHour(expression){
return !isValidExpression(expression, 0, 23);
}
function isInvalidDayOfMonth(expression){
return !isValidExpression(expression, 1, 31);
}
function isInvalidMonth(expression){
return !isValidExpression(expression, 1, 12);
}
function isInvalidWeekDay(expression){
return !isValidExpression(expression, 0, 7);
}
function validateFields(patterns, executablePatterns){
if (isInvalidSecond(executablePatterns[0])) {
throw patterns[0] + ' is a invalid expression for second';
function isInvalidSecond(expression){
return !isValidExpression(expression, 0, 59);
}
if (isInvalidMinute(executablePatterns[1])) {
throw patterns[1] + ' is a invalid expression for minute';
function isInvalidMinute(expression){
return !isValidExpression(expression, 0, 59);
}
if (isInvalidHour(executablePatterns[2])) {
throw patterns[2] + ' is a invalid expression for hour';
function isInvalidHour(expression){
return !isValidExpression(expression, 0, 23);
}
if (isInvalidDayOfMonth(executablePatterns[3])) {
throw patterns[3] + ' is a invalid expression for day of month';
function isInvalidDayOfMonth(expression){
return !isValidExpression(expression, 1, 31);
}
if (isInvalidMonth(executablePatterns[4])) {
throw patterns[4] + ' is a invalid expression for month';
function isInvalidMonth(expression){
return !isValidExpression(expression, 1, 12);
}
if (isInvalidWeekDay(executablePatterns[5])) {
throw patterns[5] + ' is a invalid expression for week day';
function isInvalidWeekDay(expression){
return !isValidExpression(expression, 0, 7);
}
}
function validate(pattern){
if (typeof pattern !== 'string'){
throw 'pattern must be a string!';
function validateFields(patterns, executablePatterns){
if (isInvalidSecond(executablePatterns[0])) {
throw patterns[0] + ' is a invalid expression for second';
}
if (isInvalidMinute(executablePatterns[1])) {
throw patterns[1] + ' is a invalid expression for minute';
}
if (isInvalidHour(executablePatterns[2])) {
throw patterns[2] + ' is a invalid expression for hour';
}
if (isInvalidDayOfMonth(executablePatterns[3])) {
throw patterns[3] + ' is a invalid expression for day of month';
}
if (isInvalidMonth(executablePatterns[4])) {
throw patterns[4] + ' is a invalid expression for month';
}
if (isInvalidWeekDay(executablePatterns[5])) {
throw patterns[5] + ' is a invalid expression for week day';
}
}
var patterns = pattern.split(' ');
var executablePattern = convertExpression(pattern);
var executablePatterns = executablePattern.split(' ');
function validate(pattern){
if (typeof pattern !== 'string'){
throw 'pattern must be a string!';
}
if(patterns.length === 5){
patterns = ['0'].concat(patterns);
let patterns = pattern.split(' ');
const executablePattern = convertExpression(pattern);
const executablePatterns = executablePattern.split(' ');
if(patterns.length === 5){
patterns = ['0'].concat(patterns);
}
validateFields(patterns, executablePatterns);
}
validateFields(patterns, executablePatterns);
}
return validate;
return validate;
})();
'use strict';
var tzOffset = require('tz-offset');
const EventEmitter = require('events');
const Task = require('./task');
const Scheduler = require('./scheduler');
/**
* Creates a new scheduled task.
*
* @param {Task} task - task to schedule.
* @param {*} options - task options.
*/
function ScheduledTask(task, options) {
var timezone = options.timezone;
/**
* Starts updating the task.
*
* @returns {ScheduledTask} instance of this task.
*/
this.start = () => {
this.status = 'scheduled';
if (this.task && !this.tick) {
this.tick = setTimeout(this.task, 1000 - new Date().getMilliseconds() + 1);
class ScheduledTask extends EventEmitter {
constructor(cronExpression, func, options) {
super();
if(!options){
options = {
scheduled: true,
recoverMissedExecutions: false
};
}
let task = new Task(func);
let scheduler = new Scheduler(cronExpression, options.timezone, options.recoverMissedExecutions);
scheduler.on('scheduled-time-matched', (now) => {
let result = task.execute(now);
this.emit('task-done', result);
});
if(options.scheduled !== false){
scheduler.start();
}
this.start = () => {
scheduler.start();
};
this.stop = () => {
scheduler.stop();
};
}
return this;
};
/**
* Stops updating the task.
*
* @returns {ScheduledTask} instance of this task.
*/
this.stop = () => {
this.status = 'stoped';
if (this.tick) {
clearTimeout(this.tick);
this.tick = null;
}
return this;
};
/**
* Returns the current task status.
*
* @returns {string} current task status.
* The return may be:
* - scheduled: when a task is scheduled and waiting to be executed.
* - running: the task status while the task is executing.
* - stoped: when the task is stoped.
* - destroyed: whe the task is destroyed, in that status the task cannot be re-started.
* - failed: a task is maker as failed when the previous execution fails.
*/
this.getStatus = () => {
return this.status;
};
/**
* Destroys the scheduled task.
*/
this.destroy = () => {
this.stop();
this.status = 'destroyed';
this.task = null;
};
task.on('started', () => {
this.status = 'running';
});
task.on('done', () => {
this.status = 'scheduled';
});
task.on('failed', () => {
this.status = 'failed';
});
this.task = () => {
var date = new Date();
if(timezone){
date = tzOffset.timeAt(date, timezone);
}
this.tick = setTimeout(this.task, 1000 - date.getMilliseconds() + 1);
task.update(date);
};
this.tick = null;
if (options.scheduled !== false) {
this.start();
}
}
module.exports = ScheduledTask;
'use strict';
var convertExpression = require('./convert-expression');
var validatePattern = require('./pattern-validation');
const EventEmitter = require('events');
var events = require('events');
class Task extends EventEmitter{
constructor(execution){
super();
if(typeof execution !== 'function') {
throw 'execution must be a function';
}
this._execution = execution;
}
function matchPattern(pattern, value){
if( pattern.indexOf(',') !== -1 ){
var patterns = pattern.split(',');
return patterns.indexOf(value.toString()) !== -1;
}
return pattern === value.toString();
}
function mustRun(task, date){
var runInSecond = matchPattern(task.expressions[0], date.getSeconds());
var runOnMinute = matchPattern(task.expressions[1], date.getMinutes());
var runOnHour = matchPattern(task.expressions[2], date.getHours());
var runOnDayOfMonth = matchPattern(task.expressions[3], date.getDate());
var runOnMonth = matchPattern(task.expressions[4], date.getMonth() + 1);
var runOnDayOfWeek = matchPattern(task.expressions[5], date.getDay());
var runOnDay = false;
var delta = task.initialPattern.length === 6 ? 0 : -1;
if (task.initialPattern[3 + delta] === '*') {
runOnDay = runOnDayOfWeek;
} else if (task.initialPattern[5 + delta] === '*') {
runOnDay = runOnDayOfMonth;
} else {
runOnDay = runOnDayOfMonth || runOnDayOfWeek;
}
return runInSecond && runOnMinute && runOnHour && runOnDay && runOnMonth;
}
function Task(pattern, execution){
validatePattern(pattern);
this.initialPattern = pattern.split(' ');
this.pattern = convertExpression(pattern);
this.execution = execution;
this.expressions = this.pattern.split(' ');
events.EventEmitter.call(this);
this.update = (date) => {
if(mustRun(this, date)){
new Promise((resolve, reject) => {
this.emit('started', this);
var ex = this.execution();
if(ex instanceof Promise){
ex.then(resolve).catch(reject);
execute(now) {
let exec;
try {
exec = this._execution(now);
} catch (error) {
return this.emit('task-failed', error);
}
if (exec instanceof Promise) {
return exec
.then(() => this.emit('task-finished'))
.catch((error) => this.emit('task-failed', error));
} else {
resolve();
this.emit('task-finished');
return exec;
}
}).then(() => {
this.emit('done', this);
}).catch((error) => {
console.error(error);
this.emit('failed', error);
});
}
};
}
Task.prototype = events.EventEmitter.prototype;
module.exports = Task;
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/asterisk-to-range-conversion');
const { expect } = require('chai');
const conversion = require('../../src/convert-expression/asterisk-to-range-conversion');
describe('asterisk-to-range-conversion.js', () => {
it('shuld convert * to ranges', () => {
var expressions = '* * * * * *'.split(' ');
var expression = conversion(expressions).join(' ');
expect(expression).to.equal('0-59 0-59 0-23 1-31 1-12 0-6');
});
it('shuld convert * to ranges', () => {
const expressions = '* * * * * *'.split(' ');
const expression = conversion(expressions).join(' ');
expect(expression).to.equal('0-59 0-59 0-23 1-31 1-12 0-6');
});
});
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression');
const { expect } = require('chai');
const conversion = require('../../src/convert-expression');
describe('month-names-conversion.js', () => {
it('shuld convert month names', () => {
var expression = conversion('* * * * January,February *');
var expressions = expression.split(' ');
expect(expressions[4]).to.equal('1,2');
});
it('shuld convert month names', () => {
const expression = conversion('* * * * January,February *');
const expressions = expression.split(' ');
expect(expressions[4]).to.equal('1,2');
});
it('shuld convert week day names', () => {
var expression = conversion('* * * * * Mon,Sun');
var expressions = expression.split(' ');
expect(expressions[5]).to.equal('1,0');
});
it('shuld convert week day names', () => {
const expression = conversion('* * * * * Mon,Sun');
const expressions = expression.split(' ');
expect(expressions[5]).to.equal('1,0');
});
});
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/month-names-conversion');
const { expect } = require('chai');
const conversion = require('../../src/convert-expression/month-names-conversion');
describe('month-names-conversion.js', () => {
it('shuld convert month names', () => {
var months = conversion('January,February,March,April,May,June,July,August,September,October,November,December');
expect(months).to.equal('1,2,3,4,5,6,7,8,9,10,11,12');
});
it('shuld convert month names', () => {
const months = conversion('January,February,March,April,May,June,July,August,September,October,November,December');
expect(months).to.equal('1,2,3,4,5,6,7,8,9,10,11,12');
});
it('shuld convert month names', () => {
var months = conversion('Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec');
expect(months).to.equal('1,2,3,4,5,6,7,8,9,10,11,12');
});
it('shuld convert month names', () => {
const months = conversion('Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec');
expect(months).to.equal('1,2,3,4,5,6,7,8,9,10,11,12');
});
});
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/range-conversion');
const { expect } = require('chai');
const conversion = require('../../src/convert-expression/range-conversion');
describe('range-conversion.js', () => {
it('shuld convert ranges to numbers', () => {
var expressions = '0-3 0-3 0-2 1-3 1-2 0-3'.split(' ');
var expression = conversion(expressions).join(' ');
expect(expression).to.equal('0,1,2,3 0,1,2,3 0,1,2 1,2,3 1,2 0,1,2,3');
});
it('shuld convert ranges to numbers', () => {
const expressions = '0-3 0-3 0-2 1-3 1-2 0-3'.split(' ');
const expression = conversion(expressions).join(' ');
expect(expression).to.equal('0,1,2,3 0,1,2,3 0,1,2 1,2,3 1,2 0,1,2,3');
});
it('shuld convert ranges to numbers', () => {
var expressions = '0-3 0-3 8-10 1-3 1-2 0-3'.split(' ');
var expression = conversion(expressions).join(' ');
expect(expression).to.equal('0,1,2,3 0,1,2,3 8,9,10 1,2,3 1,2 0,1,2,3');
});
it('shuld convert ranges to numbers', () => {
const expressions = '0-3 0-3 8-10 1-3 1-2 0-3'.split(' ');
const expression = conversion(expressions).join(' ');
expect(expression).to.equal('0,1,2,3 0,1,2,3 8,9,10 1,2,3 1,2 0,1,2,3');
});
it('should convert comma delimited ranges to numbers', () => {
var expressions = '0-2,10-23'.split(' ');
var expression = conversion(expressions).join(' ');
expect(expression).to.equal('0,1,2,10,11,12,13,14,15,16,17,18,19,20,21,22,23');
});
});
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/step-values-conversion');
const { expect } = require('chai');
const conversion = require('../../src/convert-expression/step-values-conversion');
describe('step-values-conversion.js', () => {
it('shuld convert step values', () => {
var expressions = '1,2,3,4,5,6,7,8,9,10/2 0,1,2,3,4,5,6,7,8,9/5 * * * *'.split(' ');
expressions = conversion(expressions);
console.log(expressions);
expect(expressions[0]).to.equal('2,4,6,8,10');
expect(expressions[1]).to.equal('0,5');
});
it('should convert step values', () => {
var expressions = '1,2,3,4,5,6,7,8,9,10/2 0,1,2,3,4,5,6,7,8,9/5 * * * *'.split(' ');
expressions = conversion(expressions);
expect(expressions[0]).to.equal('2,4,6,8,10');
expect(expressions[1]).to.equal('0,5');
});
it('should throw an error if step value is not a number', () => {
var expressions = '1,2,3,4,5,6,7,8,9,10/someString 0,1,2,3,4,5,6,7,8,9/5 * * * *'.split(' ');
expect(() => conversion(expressions)).to.throw('someString is not a valid step value');
});
});
'use strict';
var expect = require('expect.js');
var conversion = require('../../src/convert-expression/week-day-names-conversion');
const { expect } = require('chai');
const conversion = require('../../src/convert-expression/week-day-names-conversion');
describe('week-day-names-conversion.js', () => {
it('shuld convert week day names names', () => {
var weekDays = conversion('Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday');
expect(weekDays).to.equal('1,2,3,4,5,6,0');
});
it('shuld convert week day names names', () => {
const weekDays = conversion('Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday');
expect(weekDays).to.equal('1,2,3,4,5,6,0');
});
it('shuld convert short week day names names', () => {
var weekDays = conversion('Mon,Tue,Wed,Thu,Fri,Sat,Sun');
expect(weekDays).to.equal('1,2,3,4,5,6,0');
});
it('shuld convert short week day names names', () => {
const weekDays = conversion('Mon,Tue,Wed,Thu,Fri,Sat,Sun');
expect(weekDays).to.equal('1,2,3,4,5,6,0');
});
it('shuld convert 7 to 0', () => {
var weekDays = conversion('7');
expect(weekDays).to.equal('0');
});
it('shuld convert 7 to 0', () => {
const weekDays = conversion('7');
expect(weekDays).to.equal('0');
});
});
'use strict';
var expect = require('expect.js');
var validate = require('../../src/pattern-validation');
const { expect } = require('chai');
const validate = require('../../src/pattern-validation');
describe('pattern-validation.js', () => {
describe('validate day of month', () => {
it('should fail with invalid day of month', () => {
expect(() => {
validate('* * 32 * *');
}).to.throwException((e) =>{
expect('32 is a invalid expression for day of month').to.equal(e);
});
});
describe('validate day of month', () => {
it('should fail with invalid day of month', () => {
expect(() => {
validate('* * 32 * *');
}).to.throw('32 is a invalid expression for day of month');
});
it('should not fail with valid day of month', () => {
expect(() => {
validate('0 * * 15 * *');
}).to.not.throwException();
});
it('should not fail with valid day of month', () => {
expect(() => {
validate('0 * * 15 * *');
}).to.not.throw();
});
it('should not fail with * for day of month', () => {
expect(() => {
validate('* * * * * *');
}).to.not.throwException();
});
it('should not fail with * for day of month', () => {
expect(() => {
validate('* * * * * *');
}).to.not.throw();
});
it('should not fail with */2 for day of month', () => {
expect(() => {
validate('* * */2 * *');
}).to.not.throwException();
it('should not fail with */2 for day of month', () => {
expect(() => {
validate('* * */2 * *');
}).to.not.throw();
});
});
});
});
'use strict';
var expect = require('expect.js');
var validate = require('../../src/pattern-validation');
const { expect } = require('chai');
const validate = require('../../src/pattern-validation');
describe('pattern-validation.js', () => {
describe('validate hour', () => {
it('should fail with invalid hour', () => {
expect(() => {
validate('* 25 * * *');
}).to.throwException((e) => {
expect('25 is a invalid expression for hour').to.equal(e);
});
});
describe('validate hour', () => {
it('should fail with invalid hour', () => {
expect(() => {
validate('* 25 * * *');
}).to.throw('25 is a invalid expression for hour');
});
it('should not fail with valid hour', () => {
expect(() => {
validate('* 12 * * *');
}).to.not.throwException();
});
it('should not fail with valid hour', () => {
expect(() => {
validate('* 12 * * *');
}).to.not.throw();
});
it('should not fail with * for hour', () => {
expect(() => {
validate('* * * * * *');
}).to.not.throwException();
});
it('should not fail with * for hour', () => {
expect(() => {
validate('* * * * * *');
}).to.not.throw();
});
it('should not fail with */2 for hour', () => {
expect(() => {
validate('* */2 * * *');
}).to.not.throwException();
});
it('should not fail with */2 for hour', () => {
expect(() => {
validate('* */2 * * *');
}).to.not.throw();
});
it('should accept range for hours', () => {
expect(() => {
validate('* 3-20 * * *');
}).to.not.throwException();
it('should accept range for hours', () => {
expect(() => {
validate('* 3-20 * * *');
}).to.not.throw();
});
});
});
});
'use strict';
var expect = require('expect.js');
var validate = require('../../src/pattern-validation');
const { expect } = require('chai');
const validate = require('../../src/pattern-validation');
describe('pattern-validation.js', () => {
describe('validate minutes', () => {
it('should fail with invalid minute', () => {
expect(() => {
validate('63 * * * *');
}).to.throwException((e) => {
expect('63 is a invalid expression for minute').to.equal(e);
});
});
describe('validate minutes', () => {
it('should fail with invalid minute', () => {
expect(() => {
validate('63 * * * *');
}).to.throw('63 is a invalid expression for minute');
});
it('should not fail with valid minute', () => {
expect(() => {
validate('30 * * * *');
}).to.not.throwException();
});
it('should not fail with valid minute', () => {
expect(() => {
validate('30 * * * *');
}).to.not.throw();
});
it('should not fail with *', () => {
expect(() => {
validate('* * * * *');
}).to.not.throwException();
});
it('should not fail with *', () => {
expect(() => {
validate('* * * * *');
}).to.not.throw();
});
it('should not fail with */2', () => {
expect(() => {
validate('*/2 * * * *');
}).to.not.throwException();
it('should not fail with */2', () => {
expect(() => {
validate('*/2 * * * *');
}).to.not.throw();
});
});
});
});
'use strict';
var expect = require('expect.js');
var validate = require('../../src/pattern-validation');
const { expect } = require('chai');
const validate = require('../../src/pattern-validation');
describe('pattern-validation.js', () => {
describe('validate month', () => {
it('should fail with invalid month', () => {
expect( () => {
validate('* * * 13 *');
}).to.throwException((e) => {
expect('13 is a invalid expression for month').to.equal(e);
});
});
describe('validate month', () => {
it('should fail with invalid month', () => {
expect( () => {
validate('* * * 13 *');
}).to.throw('13 is a invalid expression for month');
});
it('should fail with invalid month name', () => {
expect( () => {
validate('* * * foo *');
}).to.throwException(function(e){
expect('foo is a invalid expression for month').to.equal(e);
});
});
it('should fail with invalid month name', () => {
expect( () => {
validate('* * * foo *');
}).to.throw('foo is a invalid expression for month');
});
it('should not fail with valid month', () => {
expect( () => {
validate('* * * 10 *');
}).to.not.throwException();
});
it('should not fail with valid month', () => {
expect( () => {
validate('* * * 10 *');
}).to.not.throw();
});
it('should not fail with valid month name', () => {
expect( () => {
validate('* * * September *');
}).to.not.throwException();
});
it('should not fail with valid month name', () => {
expect( () => {
validate('* * * September *');
}).to.not.throw();
});
it('should not fail with * for month', () => {
expect( () => {
validate('* * * * *');
}).to.not.throwException();
});
it('should not fail with * for month', () => {
expect( () => {
validate('* * * * *');
}).to.not.throw();
});
it('should not fail with */2 for month', () => {
expect( () => {
validate('* * * */2 *');
}).to.not.throwException();
it('should not fail with */2 for month', () => {
expect( () => {
validate('* * * */2 *');
}).to.not.throw();
});
});
});
});
'use strict';
var expect = require('expect.js');
var Task = require('../../src/task');
const { expect } = require('chai');
const Task = require('../../src/task');
describe('Task', () => {
it('should accept string for pattern', () => {
expect(() => {
new Task('* * * * *');
}).to.not.throwException();
});
it('should accept a function', () => {
expect(() => {
new Task(() => {});
}).to.not.throw();
});
it('should fail with a non string value for pattern', () => {
expect(() => {
new Task([]);
}).to.throwException((e) => {
expect('pattern must be a string!').to.equal(e);
});
});
it('should fail without a function', () => {
expect(() => {
new Task([]);
}).to.throw('execution must be a function');
});
});
'use strict';
var expect = require('expect.js');
var validate = require('../../src/pattern-validation');
const { expect } = require('chai');
const validate = require('../../src/pattern-validation');
describe('pattern-validation.js', () => {
describe('validate seconds', () => {
it('should fail with invalid second', () => {
expect(() => {
validate('63 * * * * *');
}).to.throwException((e) => {
expect('63 is a invalid expression for second').to.equal(e);
});
});
describe('validate seconds', () => {
it('should fail with invalid second', () => {
expect(() => {
validate('63 * * * * *');
}).to.throw('63 is a invalid expression for second');
});
it('should not fail with valid second', () => {
expect(() => {
validate('30 * * * * *');
}).to.not.throwException();
});
it('should not fail with valid second', () => {
expect(() => {
validate('30 * * * * *');
}).to.not.throw();
});
it('should not fail with * for second', () => {
expect(() => {
validate('* * * * * *');
}).to.not.throwException();
});
it('should not fail with * for second', () => {
expect(() => {
validate('* * * * * *');
}).to.not.throw();
});
it('should not fail with */2 for second', () => {
expect(() => {
validate('*/2 * * * * *');
}).to.not.throwException();
it('should not fail with */2 for second', () => {
expect(() => {
validate('*/2 * * * * *');
}).to.not.throw();
});
});
});
});
'use strict';
var expect = require('expect.js');
var cron = require('../../src/node-cron');
const { expect } = require('chai');
const validate = require('../../src/pattern-validation');
describe('public .validate() method', () => {
it('should succeed with a valid expression', () => {
var result = cron.validate('59 * * * *');
expect(result).to.equal(true);
});
describe('pattern-validation', () => {
it('should succeed with a valid expression', () => {
expect(() => {
validate('59 * * * *');
}).to.not.throw();
});
it('should fail with an invalid expression', () => {
var result = cron.validate('60 * * * *');
expect(result).to.equal(false);
});
it('should fail with an invalid expression', () => {
expect(() => {
validate('60 * * * *');
}).to.throw('60 is a invalid expression for minute');
});
it('should fail without a string', () => {
expect(() => {
validate(50);
}).to.throw('pattern must be a string!');
});
});
'use strict';
var expect = require('expect.js');
var validate = require('../../src/pattern-validation');
const { expect } = require('chai');
const validate = require('../../src/pattern-validation');
describe('pattern-validation.js', () => {
describe('validate week day', () => {
it('should fail with invalid week day', () => {
expect(() => {
validate('* * * * 9');
}).to.throwException((e) => {
expect('9 is a invalid expression for week day').to.equal(e);
});
});
describe('validate week day', () => {
it('should fail with invalid week day', () => {
expect(() => {
validate('* * * * 9');
}).to.throw('9 is a invalid expression for week day');
});
it('should fail with invalid week day name', () => {
expect(() => {
validate('* * * * foo');
}).to.throwException((e) => {
expect('foo is a invalid expression for week day').to.equal(e);
});
});
it('should fail with invalid week day name', () => {
expect(() => {
validate('* * * * foo');
}).to.throw('foo is a invalid expression for week day');
});
it('should not fail with valid week day', () => {
expect(() => {
validate('* * * * 5');
}).to.not.throwException();
});
it('should not fail with valid week day', () => {
expect(() => {
validate('* * * * 5');
}).to.not.throw();
});
it('should not fail with valid week day name', () => {
expect(() => {
validate('* * * * Friday');
}).to.not.throwException();
});
it('should not fail with valid week day name', () => {
expect(() => {
validate('* * * * Friday');
}).to.not.throw();
});
it('should not fail with * for week day', () => {
expect(() => {
validate('* * * * *');
}).to.not.throwException();
});
it('should not fail with * for week day', () => {
expect(() => {
validate('* * * * *');
}).to.not.throw();
});
it('should not fail with */2 for week day', () => {
expect(() => {
validate('* * * */2 *');
}).to.not.throwException();
});
it('should not fail with */2 for week day', () => {
expect(() => {
validate('* * * */2 *');
}).to.not.throw();
});
it('should not fail with Monday-Sunday for week day', () => {
expect(() => {
validate('* * * * Monday-Sunday');
}).to.not.throwException();
});
it('should not fail with Monday-Sunday for week day', () => {
expect(() => {
validate('* * * * Monday-Sunday');
}).to.not.throw();
});
it('should not fail with 1-7 for week day', () => {
expect(() => {
validate('0 0 1 1 1-7');
}).to.not.throwException();
it('should not fail with 1-7 for week day', () => {
expect(() => {
validate('0 0 1 1 1-7');
}).to.not.throw();
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc