Comparing version 1.0.12 to 1.0.13
@@ -1,3 +0,3 @@ | ||
var croner = require('./lib/croner.js'); | ||
var croner = require("./lib/croner.js"); | ||
module.exports = croner; |
@@ -1,2 +0,1 @@ | ||
/* ------------------------------------------------------------------------------------ | ||
@@ -11,3 +10,3 @@ | ||
Pattern: | ||
``` | ||
``` | ||
┌──────────────── sec (0 - 59) | ||
@@ -27,3 +26,3 @@ | ┌────────────── min (0 - 59) | ||
MIT: | ||
MIT: | ||
@@ -49,48 +48,7 @@ Copyright (c) 2015 Hexagon <github.com/Hexagon> | ||
------------------------------------------------------------------------------------ */ | ||
(function () { | ||
// Array.fill polyfill | ||
if (!Array.prototype.fill) { | ||
Array.prototype.fill = function(value) { | ||
// Steps 1-2. | ||
if (this === null) { | ||
throw new TypeError('this is null or not defined'); | ||
} | ||
var O = Object(this); | ||
// Steps 3-5. | ||
var len = O.length >>> 0; | ||
// Steps 6-7. | ||
var start = arguments[1]; | ||
var relativeStart = start >> 0; | ||
// Step 8. | ||
var k = relativeStart < 0 ? | ||
Math.max(len + relativeStart, 0) : | ||
Math.min(relativeStart, len); | ||
// Steps 9-10. | ||
var end = arguments[2]; | ||
var relativeEnd = end === undefined ? | ||
len : end >> 0; | ||
// Step 11. | ||
var final = relativeEnd < 0 ? | ||
Math.max(len + relativeEnd, 0) : | ||
Math.min(relativeEnd, len); | ||
// Step 12. | ||
while (k < final) { | ||
O[k] = value; | ||
k++; | ||
} | ||
// Step 13. | ||
return O; | ||
}; | ||
} | ||
"use strict"; | ||
var root = this, | ||
@@ -105,88 +63,97 @@ | ||
function raise (err) { | ||
throw new TypeError('Cron parser: ' + err); | ||
throw new TypeError("Cron parser: " + err); | ||
} | ||
function partToArray (type, arr, conf, valueIndexOffset) { | ||
var i,x, | ||
confParts, | ||
split, | ||
index, | ||
lower, | ||
upper; | ||
// First off, handle wildcard | ||
if (conf === '*' ) { | ||
for (i = 0; i < arr.length; i++) { | ||
arr[i] = 1; | ||
} | ||
function safeDate() { | ||
return new Date(new Date().setMilliseconds(0)); | ||
} | ||
return; | ||
function fill(arr, val) { | ||
for(var i = 0; i < arr.length; i++) { | ||
arr[i] = val; | ||
} | ||
// Check if we need to split | ||
confParts = conf.split(','); | ||
// Recurse into comma separated entries | ||
if (confParts.length > 1) { | ||
for (i = 0; i < confParts.length; i++) { | ||
partToArray(type, arr, confParts[i], valueIndexOffset); | ||
} | ||
return arr; | ||
} | ||
function CronDate (date) { | ||
this.seconds = date.getSeconds() + 1; | ||
this.minutes = date.getMinutes(); | ||
this.hours = date.getHours(); | ||
this.days = date.getDate(); | ||
this.months = date.getMonth(); | ||
this.years = date.getFullYear(); | ||
} | ||
CronDate.prototype.findNext = function (target, pattern, offset, override) { | ||
return; | ||
} | ||
// Didn't need to recurse, determine if this is a range or a number | ||
if (conf.indexOf('-') === -1) { | ||
// Got a number | ||
index = (parseInt(conf, 10) + valueIndexOffset); | ||
var startPos = (override === void 0) ? this[target] + offset : 0 + offset, result = false; | ||
if (index < 0 || index >= arr.length) { | ||
raise(type + ' value out of range: "' + conf + '"'); | ||
for (var i = startPos; i < pattern[target].length; i++) { | ||
if (pattern[target][i]) { | ||
this[target] = i-offset; | ||
result = true; | ||
break; | ||
} | ||
} | ||
arr[index] = 1; | ||
} else { | ||
return result; | ||
// Got a range | ||
split = conf.split('-'); | ||
}; | ||
if (split.length !== 2) { | ||
raise('syntax error, illegal range: "' + conf + '"'); | ||
} | ||
CronDate.prototype.increment = function (pattern) { | ||
lower = parseInt(split[0], 10) + valueIndexOffset; | ||
upper = parseInt(split[1], 10) + valueIndexOffset; | ||
var toDo = [ | ||
["seconds", "minutes", 0], | ||
["minutes", "hours", 0], | ||
["hours", "days", 0], | ||
["days", "months", -1], | ||
["months", "years", 0] | ||
], | ||
doing = 0; | ||
if (isNaN(lower)) { | ||
raise('syntax error, illegal lower range (NaN)'); | ||
} else if (isNaN(upper)) { | ||
raise('syntax error, illegal upper range (NaN)'); | ||
while(doing < 5) { | ||
if(!this.findNext(toDo[doing][0], pattern, toDo[doing][2])) { | ||
this[toDo[doing][1]]++; | ||
while(doing >= 0) { | ||
this.findNext(toDo[doing][0], pattern, toDo[doing][2], 0); | ||
doing--; | ||
} | ||
} | ||
doing++; | ||
} | ||
// | ||
if (lower < 0 || upper >= arr.length) { | ||
raise('value out of range: "' + conf + '"'); | ||
} | ||
while (!pattern.daysOfWeek[this.getDate().getDay()]) { | ||
this.days += 1; | ||
} | ||
// | ||
if (lower > upper) { | ||
raise('from value is larger than to value: "' + conf + '"'); | ||
} | ||
}; | ||
for (x = lower; x <= upper; x++) { | ||
arr[(x + valueIndexOffset)] = 1; | ||
} | ||
} | ||
CronDate.prototype.getDate = function () { | ||
return new Date(this.years, this.months, this.days, this.hours, this.minutes, this.seconds, 0); | ||
}; | ||
function CronPattern (pattern) { | ||
this.pattern = pattern; | ||
this.seconds = fill(Array(60),0); // 0-59 | ||
this.minutes = fill(Array(60),0); // 0-59 | ||
this.hours = fill(Array(24),0); // 0-23 | ||
this.days = fill(Array(31),0); // 0-30 in array, 1-31 in config | ||
this.months = fill(Array(12),0); // 0-11 in array, 1-12 in config | ||
this.daysOfWeek = fill(Array(8),0); // 0-7 Where 0 = Sunday and 7=Sunday; | ||
this.parse(); | ||
} | ||
function parsePattern(pattern, target) { | ||
CronPattern.prototype.parse = function () { | ||
// Sanity check | ||
if (typeof pattern !== 'string') { | ||
raise('invalid configuration string ("' + pattern + '").'); | ||
if (typeof this.pattern !== "string") { | ||
raise("invalid configuration string ('" + this.pattern + "')."); | ||
} | ||
// Split configuration on whitespace | ||
var parts = pattern.trim().replace(/\s+/g, ' ').split(' '), | ||
var parts = this.pattern.trim().replace(/\s+/g, " ").split(" "), | ||
part, | ||
@@ -201,3 +168,3 @@ i, | ||
if (parts.length !== 6) { | ||
raise('invalid configuration format ("' + pattern + '"), exacly five space separated parts required.'); | ||
raise("invalid configuration format ('" + this.pattern + "'), exacly five space separated parts required."); | ||
} | ||
@@ -210,4 +177,4 @@ | ||
// Check that part only contain legal characters ^[0-9-,]+$ | ||
if (part !== '*' && reValidCron.test(part)) { | ||
raise('configuration entry ' + (i + 1) + ' (' + part + ') contains illegal characters.'); | ||
if (part !== "*" && reValidCron.test(part)) { | ||
raise("configuration entry " + (i + 1) + " (" + part + ") contains illegal characters."); | ||
} | ||
@@ -217,152 +184,135 @@ } | ||
// Check that we dont have both months and daysofweek | ||
hasMonths = (parts[4] !== '*'); | ||
hasDaysOfWeek = (parts[5] !== '*'); | ||
hasDates = (parts[3] !== '*'); | ||
hasMonths = (parts[4] !== "*"); | ||
hasDaysOfWeek = (parts[5] !== "*"); | ||
hasDates = (parts[3] !== "*"); | ||
// Month/Date and dayofweek is incompatible | ||
if (hasDaysOfWeek && (hasMonths || hasDates)) { | ||
raise('configuration invalid, you can not combine month/date with day of week.'); | ||
raise("configuration invalid, you can not combine month/date with day of week."); | ||
} | ||
// Parse parts into arrays, validates as we go | ||
partToArray('seconds', target.seconds, parts[0], 0); | ||
partToArray('minutes', target.minutes, parts[1], 0); | ||
partToArray('hours', target.hours, parts[2], 0); | ||
partToArray('days', target.days, parts[3], -1); | ||
partToArray('months', target.months, parts[4], -1); | ||
partToArray('daysOfWeek', target.daysOfWeek, parts[5], 0); | ||
this.partToArray("seconds", this.seconds, parts[0], 0); | ||
this.partToArray("minutes", this.minutes, parts[1], 0); | ||
this.partToArray("hours", this.hours, parts[2], 0); | ||
this.partToArray("days", this.days, parts[3], -1); | ||
this.partToArray("months", this.months, parts[4], -1); | ||
this.partToArray("daysOfWeek", this.daysOfWeek, parts[5], 0); | ||
// 0 = Sunday, 7 = Sunday | ||
if (target.daysOfWeek[0]) { | ||
target.daysOfWeek[7] = 1; | ||
if (this.daysOfWeek[0]) { | ||
this.daysOfWeek[7] = 1; | ||
} | ||
if (target.daysOfWeek[7]) { | ||
target.daysOfWeek[0] = 1; | ||
if (this.daysOfWeek[7]) { | ||
this.daysOfWeek[0] = 1; | ||
} | ||
} | ||
function safeDate() { | ||
return new Date(new Date().setMilliseconds(0)); | ||
} | ||
function Cron (pattern) { | ||
var self = this; | ||
// Optional 'new' keyword | ||
if (!(this instanceof Cron)) { | ||
return new Cron(pattern); | ||
} | ||
}; | ||
self.pattern = pattern; | ||
CronPattern.prototype.partToArray = function (type, arr, conf, valueIndexOffset) { | ||
self.seconds = Array(60).fill(0); // 0-59 | ||
self.minutes = Array(60).fill(0); // 0-59 | ||
self.hours = Array(24).fill(0); // 0-23 | ||
self.days = Array(31).fill(0); // 0-30 in array, 1-31 in config | ||
self.months = Array(12).fill(0); // 0-11 in array, 1-12 in config | ||
self.daysOfWeek = Array(8).fill(0); // 0-7 Where 0 = Sunday and 7=Sunday; | ||
var i,x, | ||
confParts, | ||
split, | ||
index, | ||
lower, | ||
upper; | ||
self.schedulerDefaults = { | ||
stopAt: Infinity, | ||
maxRuns: Infinity, | ||
kill: false | ||
}; | ||
// First off, handle wildcard | ||
if (conf === "*" ) { | ||
for (i = 0; i < arr.length; i++) { | ||
arr[i] = 1; | ||
} | ||
parsePattern(pattern, self); | ||
return; | ||
} | ||
return this; | ||
} | ||
Cron.prototype.next = function (date) { | ||
// Check if we need to split | ||
confParts = conf.split(","); | ||
date = date || safeDate(); | ||
// Recurse into comma separated entries | ||
if (confParts.length > 1) { | ||
for (i = 0; i < confParts.length; i++) { | ||
this.partToArray(type, arr, confParts[i], valueIndexOffset); | ||
} | ||
return; | ||
} | ||
var self = this, | ||
// Didn"t need to recurse, determine if this is a range or a number | ||
if (conf.indexOf("-") === -1) { | ||
// Got a number | ||
index = (parseInt(conf, 10) + valueIndexOffset); | ||
collection = { | ||
cSecs: date.getSeconds() + 1, | ||
cMins: date.getMinutes(), | ||
cHour: date.getHours(), | ||
cDate: date.getDate(), | ||
cMon: date.getMonth(), | ||
cYear: date.getFullYear(), | ||
}, | ||
if (index < 0 || index >= arr.length) { | ||
raise(type + " value out of range: '" + conf + "'"); | ||
} | ||
secs = self.seconds, | ||
mins = self.minutes, | ||
hours = self.hours, | ||
days = self.days, | ||
months = self.months, | ||
arr[index] = 1; | ||
} else { | ||
hasDays = days.filter(Boolean).length!==31, | ||
hasMonths = months.filter(Boolean).length!==12, | ||
// Got a range | ||
split = conf.split("-"); | ||
dayChanged; | ||
function goUp (what, who, current, increment, valueIndexOffset) { | ||
if (split.length !== 2) { | ||
raise("syntax error, illegal range: '" + conf + "'"); | ||
} | ||
var i, found = false; | ||
lower = parseInt(split[0], 10) + valueIndexOffset; | ||
upper = parseInt(split[1], 10) + valueIndexOffset; | ||
if (what[who[current] + valueIndexOffset]) return true; | ||
if (isNaN(lower)) { | ||
raise("syntax error, illegal lower range (NaN)"); | ||
} else if (isNaN(upper)) { | ||
raise("syntax error, illegal upper range (NaN)"); | ||
} | ||
for (i = (who[current] + valueIndexOffset); i < mins.length; i++) { | ||
if (what[i]) { | ||
who[current] = i-valueIndexOffset; | ||
found = true; | ||
break; | ||
} | ||
// | ||
if (lower < 0 || upper >= arr.length) { | ||
raise("value out of range: '" + conf + "'"); | ||
} | ||
if (!found) { | ||
who[increment] += 1; | ||
// | ||
if (lower > upper) { | ||
raise("from value is larger than to value: '" + conf + "'"); | ||
} | ||
for (i = 0; i < who[current] + valueIndexOffset; i++) { | ||
if (what[i]) { | ||
who[current] = i - valueIndexOffset; | ||
break; | ||
} | ||
} | ||
for (x = lower; x <= upper; x++) { | ||
arr[(x + valueIndexOffset)] = 1; | ||
} | ||
} | ||
}; | ||
return found; | ||
function Cron (pattern) { | ||
var self = this; | ||
// Optional "new" keyword | ||
if (!(this instanceof Cron)) { | ||
return new Cron(pattern); | ||
} | ||
// Count up to minute and hour | ||
dayChanged = false; | ||
var upMinHour = function (collection) { | ||
goUp(secs, collection, 'cSecs','cMins', 0); | ||
goUp(mins, collection, 'cMins','cHour', 0); | ||
return !(goUp(hours, collection, 'cHour','cDate', 0)); | ||
}; | ||
dayChanged = upMinHour(collection); | ||
if (hasDays || hasMonths) { | ||
// Count up to date and month | ||
dayChanged = goUp(days, collection, 'cDate', 'cMon', -1); | ||
goUp(months, collection, 'cMon', 'cYear', 0); // No need to compensate here as javascript count months 0-11 | ||
self.pattern = new CronPattern(pattern); | ||
return new Date(collection.cYear, collection.cMon, collection.cDate, collection.cHour, collection.cMins, collection.cSecs, 0); | ||
} | ||
self.schedulerDefaults = { | ||
stopAt: Infinity, | ||
maxRuns: Infinity, | ||
kill: false | ||
}; | ||
while (!self.daysOfWeek[new Date(collection.cYear, collection.cMon, collection.cDate, collection.cHour, collection.cMins, collection.cSecs, 0).getDay()]) { | ||
collection.cDate += 1; | ||
dayChanged = true; | ||
} | ||
return this; | ||
} | ||
// If day changed, we need to re-run hours and minutes | ||
if (dayChanged) { | ||
collection.cSecs = collection.cMins = collection.cHour = 0; | ||
upMinHour(collection); | ||
} | ||
return new Date(collection.cYear, collection.cMon, collection.cDate, collection.cHour, collection.cMins, collection.cSecs, 0); | ||
Cron.prototype.next = function (date) { | ||
var cronDate = new CronDate(date || safeDate()); | ||
cronDate.increment(this.pattern); | ||
return cronDate.getDate(); | ||
}; | ||
Cron.prototype.msToNext = function (prev) { | ||
return (this.next(prev) - safeDate().getTime()); | ||
return (this.next(prev) - prev.getTime()); | ||
}; | ||
Cron.prototype.schedule = function (opts, func, recurse) { | ||
var self = this, | ||
@@ -382,3 +332,3 @@ waitMs, | ||
// Keep options, or set defaults | ||
opts.paused = (opts.paused === undefined) ? false : opts.paused; | ||
opts.paused = (typeof opts.paused === "undefined") ? false : opts.paused; | ||
opts.previous = (recurse === false) ? safeDate() : opts.startAt || opts.previous; | ||
@@ -393,3 +343,3 @@ opts.stopAt = opts.stopAt || this.schedulerDefaults.stopAt; | ||
// One-timer | ||
opts.startAt = undefined; | ||
opts.startAt = void 0; | ||
@@ -400,5 +350,9 @@ // Get ms to next run | ||
// Check for stop conditions | ||
if (opts.maxRuns <= 0) return; | ||
if (opts.stopAt !== Infinity && opts.previous.getTime() + waitMs/1000 > opts.stopAt.getTime() ) return; | ||
if (opts.kill) return; | ||
if ( | ||
(opts.maxRuns <= 0) || | ||
(opts.stopAt !== Infinity && opts.previous.getTime() + waitMs/1000 > opts.stopAt.getTime() ) || | ||
(opts.kill) | ||
) { | ||
return; | ||
} | ||
@@ -429,33 +383,31 @@ // setTimeout cant handle more than Math.pow(2, 32 - 1) - 1 ms | ||
// First run? Return killer | ||
if ( !recurse ) { | ||
return { | ||
// Return control functions | ||
return { | ||
// Return undefined | ||
stop: function() { | ||
opts.kill = true; | ||
// Stop any awaiting call | ||
if ( opts.currentTimeout ) { | ||
clearTimeout( opts.currentTimeout ); | ||
} | ||
}, | ||
// Return undefined | ||
stop: function() { | ||
opts.kill = true; | ||
// Stop any awaiting call | ||
if ( opts.currentTimeout ) { | ||
clearTimeout( opts.currentTimeout ); | ||
} | ||
}, | ||
// Return if pause were successful | ||
pause: function() { | ||
return (opts.paused = true) && !opts.kill; | ||
}, | ||
// Return if pause were successful | ||
pause: function() { | ||
return (opts.paused = true) && !opts.kill; | ||
}, | ||
// Return if resume were successful | ||
resume: function () { | ||
return !(opts.paused = false) && !opts.kill; | ||
} | ||
// Return if resume were successful | ||
resume: function () { | ||
return !(opts.paused = false) && !opts.kill; | ||
} | ||
}; | ||
} | ||
}; | ||
}; | ||
// Expose | ||
if (typeof module != 'undefined' && typeof module.exports === 'object') { | ||
if (typeof module != "undefined" && typeof module.exports === "object") { | ||
module.exports = Cron; | ||
} else if (typeof define === 'function' && define.amd) { | ||
} else if (typeof define === "function" && define.amd) { | ||
define([], function () { | ||
@@ -468,2 +420,2 @@ return Cron; | ||
}).call(this); | ||
}).call(this); |
{ | ||
"name": "croner", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"description": "Isomorphic JavaScript cron parser and scheduler.", | ||
@@ -5,0 +5,0 @@ "author": "Hexagon <github.com/hexagon>", |
205
test/test.js
@@ -25,20 +25,22 @@ /* | ||
'use strict'; | ||
/* eslint no-unused-vars: 0 */ | ||
var should = require('should'), | ||
cron = require('../index.js'); | ||
"use strict"; | ||
describe('Module', function () { | ||
var should = require("should"), | ||
Cron = require("../index.js"); | ||
it('new cron(...) should not throw', function () { | ||
describe("Module", function () { | ||
it("new Cron(...) should not throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * * *'), | ||
nextRun = scheduler.next(); | ||
var scheduler = new Cron("* * * * * *"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it('cron(...) without `new` should not throw', function () { | ||
it("cron(...) without `new` should not throw", function () { | ||
(function(){ | ||
var scheduler = cron('* * * * * *'), | ||
nextRun = scheduler.next(); | ||
var scheduler = Cron("* * * * * *"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
@@ -49,133 +51,155 @@ }); | ||
describe('Parser', function () { | ||
describe("Parser", function () { | ||
it('Clean pattern should not throw', function () { | ||
it("Clean pattern should not throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * * *'); | ||
var scheduler = new Cron("* * * * * *"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it('Short pattern should throw', function () { | ||
it("Short pattern should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * *'); | ||
var scheduler = new Cron("* * * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Long pattern should throw', function () { | ||
it("Long pattern should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * * * *'); | ||
var scheduler = new Cron("* * * * * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Letter in pattern should throw', function () { | ||
it("Letter in pattern should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* a * * * *'); | ||
var scheduler = new Cron("* a * * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Missing lower range should throw', function () { | ||
it("Missing lower range should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* -9 * * * *'); | ||
var scheduler = new Cron("* -9 * * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Missing upper range should throw', function () { | ||
it("Missing upper range should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* 0- * * * *'); | ||
var scheduler = new Cron("* 0- * * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Valid range should not throw', function () { | ||
it("Valid range should not throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* 0-9 * * * *'); | ||
var scheduler = new Cron("* 0-9 * * * *"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it('Valid seconds should not throw', function () { | ||
it("Valid seconds should not throw", function () { | ||
(function(){ | ||
var scheduler = new cron('0-59 * * * * *'); | ||
var scheduler = new Cron("0-59 * * * * *"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it('Too high second should throw', function () { | ||
it("Too high second should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('0-60 * * * * *'); | ||
var scheduler = new Cron("0-60 * * * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Valid minutes should not throw', function () { | ||
it("Valid minutes should not throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* 0-59 * * * *'); | ||
var scheduler = new Cron("* 0-59 * * * *"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it('Too high minute should throw', function () { | ||
it("Too high minute should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* 0-5,55,60 * * * *'); | ||
var scheduler = new Cron("* 0-5,55,60 * * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Valid hours should not throw', function () { | ||
it("Valid hours should not throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * 0-23 * * *'); | ||
var scheduler = new Cron("* * 0-23 * * *"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it('Too high hours minute should throw', function () { | ||
it("Too high hours minute should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * 0,23,24 * * *'); | ||
var scheduler = new Cron("* * 0,23,24 * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Valid days should not throw', function () { | ||
it("Valid days should not throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * 1-31 * *'); | ||
var scheduler = new Cron("* * * 1-31 * *"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it('Too high days should throw', function () { | ||
it("Too high days should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * 32 * *'); | ||
var scheduler = new Cron("* * * 32 * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Too low days should throw', function () { | ||
it("Too low days should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * 0 * *'); | ||
var scheduler = new Cron("* * * 0 * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Valid months should not throw', function () { | ||
it("Valid months should not throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * 1,2,3,4,5,6,7,8,9,10,11,12 *'); | ||
var scheduler = new Cron("* * * * 1,2,3,4,5,6,7,8,9,10,11,12 *"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it('Too high months should throw', function () { | ||
it("Too high months should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * 7-13 *'); | ||
var scheduler = new Cron("* * * * 7-13 *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Too low months should throw', function () { | ||
it("Too low months should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * 0-3 *'); | ||
var scheduler = new Cron("* * * * 0-3 *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Valid weekdays should not throw', function () { | ||
it("Valid weekdays should not throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * * 0,1,2,3,4,5,6,7'); | ||
var scheduler = new Cron("* * * * * 0,1,2,3,4,5,6,7"); | ||
scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it('Too high weekday should throw', function () { | ||
it("Too high weekday should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * * 8'); | ||
var scheduler = new Cron("* * * * * 8"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it('Too low weekday should throw', function () { | ||
it("Too low weekday should throw", function () { | ||
(function(){ | ||
var scheduler = new cron('* * * * * -1'); | ||
var scheduler = new Cron("* * * * * -1"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
@@ -186,8 +210,8 @@ }); | ||
describe('Scheduler', function () { | ||
describe("Scheduler", function () { | ||
it('0 0 0 * * * should return tomorrow, at 00:00:00', function () { | ||
var scheduler = new cron('0 0 0 * * *'), | ||
it("0 0 0 * * * should return tomorrow, at 00:00:00", function () { | ||
var scheduler = new Cron("0 0 0 * * *"), | ||
nextRun = scheduler.next(), | ||
nextDay = new Date(new Date().getTime()+24*60*60*1000); // Add one day | ||
nextDay = new Date(new Date().getTime()+24*60*60*1000); // Add one day | ||
@@ -205,8 +229,8 @@ // Set seconds, minutes and hours to 00:00:00 | ||
it('0 0 0 * * * with 40 iterations should return 40 days from now, at 00:00:00', function () { | ||
var scheduler = new cron('0 0 0 * * *'), | ||
it("0 0 0 * * * with 40 iterations should return 40 days from now", function () { | ||
var scheduler = new Cron("0 0 0 * * *"), | ||
prevRun = new Date(), | ||
nextRun, | ||
iterations = 40, | ||
compareDay = new Date(new Date().getTime()+40*24*60*60*1000); // Add one day | ||
compareDay = new Date(new Date().getTime()+40*24*60*60*1000); // Add one day | ||
@@ -229,8 +253,8 @@ while(iterations-->0) { | ||
it('0 * * * * * with 40 iterations should return 40 minutes from now', function () { | ||
var scheduler = new cron('0 * * * * *'), | ||
it("0 * * * * * with 40 iterations should return 45 minutes from now", function () { | ||
var scheduler = new Cron("0 * * * * *"), | ||
prevRun = new Date(), | ||
nextRun, | ||
iterations = 40, | ||
compareDay = new Date(new Date().getTime()+40*60*1000); // Add one day | ||
iterations = 45, | ||
compareDay = new Date(new Date().getTime()+45*60*1000); | ||
@@ -250,3 +274,48 @@ while(iterations-->0) { | ||
}); | ||
}) | ||
}); | ||
describe("Comprehensive testing ( will fail first day of the year)", function () { | ||
it("Test milliseconds to 01:01:91 XXXX-01-01 (most often next year), 1000s steps", function () { | ||
var prevRun = new Date(new Date().setMilliseconds(0)), | ||
target = new Date(new Date((prevRun.getFullYear()+1) + "-01-01 01:01:01").getTime()), | ||
scheduler = new Cron("1 1 1 1 1 *"), | ||
nextRun, | ||
left, | ||
diff; | ||
target.getTime().should.equal(scheduler.next().getTime()); | ||
if(target.getTime() == scheduler.next().getTime()) { | ||
while(prevRun < target) { | ||
left = scheduler.msToNext(prevRun); | ||
diff = Math.abs((target.getTime() - prevRun.getTime())-left); | ||
diff.should.be.below(1001); | ||
prevRun = new Date(prevRun.getTime() + 1000000); | ||
} | ||
} | ||
}); | ||
it("Test milliseconds to 23:59:59 XXXX-01-01 (most often next year), 1000s steps", function () { | ||
var prevRun = new Date(new Date().setMilliseconds(0)), | ||
target = new Date(new Date((prevRun.getFullYear()+1) + "-01-01 23:59:59").getTime()), | ||
scheduler = new Cron("59 59 23 1 1 *"), | ||
nextRun, | ||
left, | ||
diff; | ||
target.getTime().should.equal(scheduler.next().getTime()); | ||
if(target.getTime() == scheduler.next().getTime()) { | ||
while(prevRun < target) { | ||
left = scheduler.msToNext(prevRun); | ||
diff = Math.abs((target.getTime() - prevRun.getTime())-left); | ||
diff.should.be.below(1001); | ||
prevRun = new Date(prevRun.getTime() + 1000000); | ||
} | ||
} | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24964
9
606