Comparing version 1.1.8 to 1.1.9
@@ -410,4 +410,5 @@ /* ------------------------------------------------------------------------------------ | ||
// If previous run is not set, use startAt | ||
prev = prev || this.opts.startAt || safeDate(); | ||
var | ||
prev = prev || this.opts.startAt || safeDate(), | ||
stopAt = this.opts.stopAt || this.schedulerDefaults.stopAt, | ||
@@ -429,2 +430,3 @@ cronDate = new CronDate(prev), | ||
Cron.prototype.validateOpts = function (opts) { | ||
// startAt is set, validate it | ||
if( opts.startAt !== undefined ) { | ||
@@ -436,2 +438,7 @@ if( opts.startAt.constructor !== Date ) { | ||
} | ||
// Raise if we did get an invalid date | ||
if (isNaN(opts.startAt)) { | ||
raise("Provided value for startAt could not be parsed as date."); | ||
} | ||
} | ||
@@ -442,2 +449,7 @@ if( opts.stopAt !== undefined ) { | ||
} | ||
// Raise if we did get an invalid date | ||
if (isNaN(opts.stopAt)) { | ||
raise("Provided value for stopAt could not be parsed as date."); | ||
} | ||
} | ||
@@ -456,3 +468,3 @@ return opts; | ||
Cron.prototype.schedule = function (opts, func, recurse) { | ||
Cron.prototype.schedule = function (opts, func) { | ||
@@ -516,3 +528,3 @@ var self = this, | ||
// Recurse | ||
self.schedule(opts, func, true); | ||
self.schedule(opts, func); | ||
}, waitMs ); | ||
@@ -519,0 +531,0 @@ |
// Licenced under MIT - croner - ©2016 Hexagon <github.com/hexagon> | ||
(function(){"use strict";function c(a){throw new TypeError("Cron parser: "+a)}function d(){return new Date((new Date).setMilliseconds(0))}function e(a,b){for(var c=0;c<a.length;c++)a[c]=b;return a}function f(a){this.seconds=a.getSeconds()+1,this.minutes=a.getMinutes(),this.hours=a.getHours(),this.days=a.getDate(),this.months=a.getMonth(),this.years=a.getFullYear()}function g(a){this.pattern=a,this.seconds=e(Array(60),0),this.minutes=e(Array(60),0),this.hours=e(Array(24),0),this.days=e(Array(31),0),this.months=e(Array(12),0),this.daysOfWeek=e(Array(8),0),this.parse()}function h(a,b,c){var d=this;return this instanceof h?(d.pattern=new g(a),d.schedulerDefaults={stopAt:1/0,maxRuns:1/0,kill:!1},"function"==typeof b&&(c=b,b={}),d.opts=d.validateOpts(b||{}),"undefined"==typeof c?d:this.schedule(b,c)):new h(a,b,c)}var a=this,b=Math.pow(2,31)-1;f.prototype.findNext=function(a,b,c,d){for(var e=void 0===d?this[a]+c:0+c,f=!1,g=e;g<b[a].length;g++)if(b[a][g]){this[a]=g-c,f=!0;break}return f},f.prototype.increment=function(a){for(var b=[["seconds","minutes",0],["minutes","hours",0],["hours","days",0],["days","months",-1],["months","years",0]],c=0;c<5;){if(!this.findNext(b[c][0],a,b[c][2]))for(this[b[c][1]]++;c>=0;)this.findNext(b[c][0],a,b[c][2],0),c--;c++}for(;!a.daysOfWeek[this.getDate().getDay()];)this.days+=1},f.prototype.getDate=function(){return new Date(this.years,this.months,this.days,this.hours,this.minutes,this.seconds,0)},g.prototype.parse=function(){"string"!=typeof this.pattern&&c("invalid configuration string ('"+this.pattern+"').");var b,d,f,g,h,a=this.pattern.trim().replace(/\s+/g," ").split(" "),e=/[^\/\*0-9,-]+/;for(6!==a.length&&c("invalid configuration format ('"+this.pattern+"'), exacly five space separated parts required."),d=0;d<a.length;d++)b=a[d].trim(),e.test(b)&&c("configuration entry "+(d+1)+" ("+b+") contains illegal characters.");f="*"!==a[4],g="*"!==a[5],h="*"!==a[3],g&&(f||h)&&c("configuration invalid, you can not combine month/date with day of week."),this.partToArray("seconds",a[0],0),this.partToArray("minutes",a[1],0),this.partToArray("hours",a[2],0),this.partToArray("days",a[3],-1),this.partToArray("months",a[4],-1),this.partToArray("daysOfWeek",a[5],0),this.daysOfWeek[0]&&(this.daysOfWeek[7]=1),this.daysOfWeek[7]&&(this.daysOfWeek[0]=1)},g.prototype.partToArray=function(a,b,d){var e,f,g,h,i,j=this[a];if("*"!==b)if(f=b.split(","),f.length>1)for(e=0;e<f.length;e++)this.partToArray(a,f[e],d);else if(b.indexOf("-")!==-1)for(f=b.split("-"),2!==f.length&&c("Syntax error, illegal range: '"+b+"'"),g=parseInt(f[0],10)+d,h=parseInt(f[1],10)+d,isNaN(g)?c("Syntax error, illegal lower range (NaN)"):isNaN(h)&&c("Syntax error, illegal upper range (NaN)"),(g<0||h>=j.length)&&c("Value out of range: '"+b+"'"),g>h&&c("From value is larger than to value: '"+b+"'"),e=g;e<=h;e++)j[e+d]=1;else if(b.indexOf("/")!==-1)for(f=b.split("/"),2!==f.length&&c("Syntax error, illegal stepping: '"+b+"'"),"*"!==f[0]&&c("Syntax error, left part of / needs to be * : '"+b+"'"),i=parseInt(f[1],10),isNaN(i)&&c("Syntax error, illegal stepping: (NaN)"),0===i&&c("Syntax error, illegal stepping: 0"),i>j.length&&c("Syntax error, steps cannot be greater than maximum value of part ("+j.length+")"),e=0;e<j.length;e+=i)j[e+d]=1;else e=parseInt(b,10)+d,(e<0||e>=j.length)&&c(a+" value out of range: '"+b+"'"),j[e]=1;else for(e=0;e<j.length;e++)j[e]=1},h.prototype.next=function(a){this.opts.startAt&&a<this.opts.startAt&&(a=this.opts.startAt);var e,a=a||this.opts.startAt||d(),b=this.opts.stopAt||this.schedulerDefaults.stopAt,c=new f(a);if(c.increment(this.pattern),e=c.getDate(),!(b&&e>=b))return e},h.prototype.validateOpts=function(a){return void 0!==a.startAt&&(a.startAt.constructor!==Date?a.startAt=new Date(Date.parse(a.startAt)-1):a.startAt=new Date(a.startAt.getTime()-1)),void 0!==a.stopAt&&a.stopAt.constructor!==Date&&(a.stopAt=new Date(Date.parse(a.stopAt))),a},h.prototype.msToNext=function(a){a=a||d();var b=this.next(a);return b?this.next(a)-a.getTime():b},h.prototype.schedule=function(a,c,e){var g,f=this,h=f.maxDelay||b;if(c||(c=a,a={}),a.paused="undefined"!=typeof a.paused&&a.paused,a.kill=a.kill||this.schedulerDefaults.kill,a.rest=a.rest||0,a.maxRuns||0===a.maxRuns||(a.maxRuns=this.schedulerDefaults.maxRuns),f.opts=f.validateOpts(a||{}),g=this.msToNext(a.previous),!(a.maxRuns<=0||void 0===g||a.kill))return g>h&&(g=h),a.currentTimeout=setTimeout(function(){g===h||a.paused||(a.maxRuns--,a.previous=d(),c()),a.paused&&(a.previous=d()),f.schedule(a,c,!0)},g),{stop:function(){a.kill=!0,a.currentTimeout&&clearTimeout(a.currentTimeout)},pause:function(){return(a.paused=!0)&&!a.kill},resume:function(){return!(a.paused=!1)&&!a.kill}}},"undefined"!=typeof module&&"object"==typeof module.exports?module.exports=h:"function"==typeof define&&define.amd?define([],function(){return h}):a.Cron=h}).call(this); | ||
(function(){"use strict";function c(a){throw new TypeError("Cron parser: "+a)}function d(){return new Date((new Date).setMilliseconds(0))}function e(a,b){for(var c=0;c<a.length;c++)a[c]=b;return a}function f(a){this.seconds=a.getSeconds()+1,this.minutes=a.getMinutes(),this.hours=a.getHours(),this.days=a.getDate(),this.months=a.getMonth(),this.years=a.getFullYear()}function g(a){this.pattern=a,this.seconds=e(Array(60),0),this.minutes=e(Array(60),0),this.hours=e(Array(24),0),this.days=e(Array(31),0),this.months=e(Array(12),0),this.daysOfWeek=e(Array(8),0),this.parse()}function h(a,b,c){var d=this;return this instanceof h?(d.pattern=new g(a),d.schedulerDefaults={stopAt:1/0,maxRuns:1/0,kill:!1},"function"==typeof b&&(c=b,b={}),d.opts=d.validateOpts(b||{}),"undefined"==typeof c?d:this.schedule(b,c)):new h(a,b,c)}var a=this,b=Math.pow(2,31)-1;f.prototype.findNext=function(a,b,c,d){for(var e=void 0===d?this[a]+c:0+c,f=!1,g=e;g<b[a].length;g++)if(b[a][g]){this[a]=g-c,f=!0;break}return f},f.prototype.increment=function(a){for(var b=[["seconds","minutes",0],["minutes","hours",0],["hours","days",0],["days","months",-1],["months","years",0]],c=0;c<5;){if(!this.findNext(b[c][0],a,b[c][2]))for(this[b[c][1]]++;c>=0;)this.findNext(b[c][0],a,b[c][2],0),c--;c++}for(;!a.daysOfWeek[this.getDate().getDay()];)this.days+=1},f.prototype.getDate=function(){return new Date(this.years,this.months,this.days,this.hours,this.minutes,this.seconds,0)},g.prototype.parse=function(){"string"!=typeof this.pattern&&c("invalid configuration string ('"+this.pattern+"').");var b,d,f,g,h,a=this.pattern.trim().replace(/\s+/g," ").split(" "),e=/[^\/\*0-9,-]+/;for(6!==a.length&&c("invalid configuration format ('"+this.pattern+"'), exacly five space separated parts required."),d=0;d<a.length;d++)b=a[d].trim(),e.test(b)&&c("configuration entry "+(d+1)+" ("+b+") contains illegal characters.");f="*"!==a[4],g="*"!==a[5],h="*"!==a[3],g&&(f||h)&&c("configuration invalid, you can not combine month/date with day of week."),this.partToArray("seconds",a[0],0),this.partToArray("minutes",a[1],0),this.partToArray("hours",a[2],0),this.partToArray("days",a[3],-1),this.partToArray("months",a[4],-1),this.partToArray("daysOfWeek",a[5],0),this.daysOfWeek[0]&&(this.daysOfWeek[7]=1),this.daysOfWeek[7]&&(this.daysOfWeek[0]=1)},g.prototype.partToArray=function(a,b,d){var e,f,g,h,i,j=this[a];if("*"!==b)if(f=b.split(","),f.length>1)for(e=0;e<f.length;e++)this.partToArray(a,f[e],d);else if(b.indexOf("-")!==-1)for(f=b.split("-"),2!==f.length&&c("Syntax error, illegal range: '"+b+"'"),g=parseInt(f[0],10)+d,h=parseInt(f[1],10)+d,isNaN(g)?c("Syntax error, illegal lower range (NaN)"):isNaN(h)&&c("Syntax error, illegal upper range (NaN)"),(g<0||h>=j.length)&&c("Value out of range: '"+b+"'"),g>h&&c("From value is larger than to value: '"+b+"'"),e=g;e<=h;e++)j[e+d]=1;else if(b.indexOf("/")!==-1)for(f=b.split("/"),2!==f.length&&c("Syntax error, illegal stepping: '"+b+"'"),"*"!==f[0]&&c("Syntax error, left part of / needs to be * : '"+b+"'"),i=parseInt(f[1],10),isNaN(i)&&c("Syntax error, illegal stepping: (NaN)"),0===i&&c("Syntax error, illegal stepping: 0"),i>j.length&&c("Syntax error, steps cannot be greater than maximum value of part ("+j.length+")"),e=0;e<j.length;e+=i)j[e+d]=1;else e=parseInt(b,10)+d,(e<0||e>=j.length)&&c(a+" value out of range: '"+b+"'"),j[e]=1;else for(e=0;e<j.length;e++)j[e]=1},h.prototype.next=function(a){this.opts.startAt&&a<this.opts.startAt&&(a=this.opts.startAt),a=a||this.opts.startAt||d();var e,b=this.opts.stopAt||this.schedulerDefaults.stopAt,c=new f(a);if(c.increment(this.pattern),e=c.getDate(),!(b&&e>=b))return e},h.prototype.validateOpts=function(a){return void 0!==a.startAt&&(a.startAt.constructor!==Date?a.startAt=new Date(Date.parse(a.startAt)-1):a.startAt=new Date(a.startAt.getTime()-1),isNaN(a.startAt)&&c("Provided value for startAt could not be parsed as date.")),void 0!==a.stopAt&&(a.stopAt.constructor!==Date&&(a.stopAt=new Date(Date.parse(a.stopAt))),isNaN(a.stopAt)&&c("Provided value for stopAt could not be parsed as date.")),a},h.prototype.msToNext=function(a){a=a||d();var b=this.next(a);return b?this.next(a)-a.getTime():b},h.prototype.schedule=function(a,c){var f,e=this,g=e.maxDelay||b;if(c||(c=a,a={}),a.paused="undefined"!=typeof a.paused&&a.paused,a.kill=a.kill||this.schedulerDefaults.kill,a.rest=a.rest||0,a.maxRuns||0===a.maxRuns||(a.maxRuns=this.schedulerDefaults.maxRuns),e.opts=e.validateOpts(a||{}),f=this.msToNext(a.previous),!(a.maxRuns<=0||void 0===f||a.kill))return f>g&&(f=g),a.currentTimeout=setTimeout(function(){f===g||a.paused||(a.maxRuns--,a.previous=d(),c()),a.paused&&(a.previous=d()),e.schedule(a,c)},f),{stop:function(){a.kill=!0,a.currentTimeout&&clearTimeout(a.currentTimeout)},pause:function(){return(a.paused=!0)&&!a.kill},resume:function(){return!(a.paused=!1)&&!a.kill}}},"undefined"!=typeof module&&"object"==typeof module.exports?module.exports=h:"function"==typeof define&&define.amd?define([],function(){return h}):a.Cron=h}).call(this); |
{ | ||
"name": "croner", | ||
"version": "1.1.8", | ||
"version": "1.1.9", | ||
"description": "Isomorphic JavaScript cron parser and scheduler.", | ||
@@ -5,0 +5,0 @@ "author": "Hexagon <github.com/hexagon>", |
@@ -87,2 +87,9 @@ /* | ||
it("Slash in pattern with number first should xthrow", function () { | ||
(function(){ | ||
var scheduler = new Cron("* 5/* * * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it("Slash in pattern without following number should throw", function () { | ||
@@ -358,2 +365,84 @@ (function(){ | ||
it("Valid startAt with Date should not throw", function () { | ||
(function () { | ||
var | ||
dayBefore = new Date(new Date().getTime()-24*60*60*1000), // Subtract one day | ||
scheduler = new Cron("0 0 12 * * *", { startAt: dayBefore }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Valid startAt with DateTime string should not throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { startAt: "2016-12-01 00:00:00" }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Valid startAt with Date string should not throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { startAt: "2016-12-01" }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Invalid startat should throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { startAt: "hellu throw" }), | ||
nextRun = scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it("startAt with time only should throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { startAt: "00:35:00" }), | ||
nextRun = scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it("Valid stopAt with Date should not throw", function () { | ||
(function () { | ||
var | ||
dayBefore = new Date(new Date().getTime()-24*60*60*1000), // Subtract one day | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: dayBefore }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Valid stopAt with DateTime string should not throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: "2016-12-01 00:00:00" }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Valid stopAt with Date string should not throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: "2016-12-01" }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Invalid stopAt should throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: "hellu throw" }), | ||
nextRun = scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it("stopAt with time only should throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: "00:35:00" }), | ||
nextRun = scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
}); | ||
@@ -360,0 +449,0 @@ |
@@ -87,2 +87,9 @@ /* | ||
it("Slash in pattern with number first should xthrow", function () { | ||
(function(){ | ||
var scheduler = new Cron("* 5/* * * * *"); | ||
scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it("Slash in pattern without following number should throw", function () { | ||
@@ -358,2 +365,84 @@ (function(){ | ||
it("Valid startAt with Date should not throw", function () { | ||
(function () { | ||
var | ||
dayBefore = new Date(new Date().getTime()-24*60*60*1000), // Subtract one day | ||
scheduler = new Cron("0 0 12 * * *", { startAt: dayBefore }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Valid startAt with DateTime string should not throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { startAt: "2016-12-01 00:00:00" }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Valid startAt with Date string should not throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { startAt: "2016-12-01" }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Invalid startat should throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { startAt: "hellu throw" }), | ||
nextRun = scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it("startAt with time only should throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { startAt: "00:35:00" }), | ||
nextRun = scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it("Valid stopAt with Date should not throw", function () { | ||
(function () { | ||
var | ||
dayBefore = new Date(new Date().getTime()-24*60*60*1000), // Subtract one day | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: dayBefore }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Valid stopAt with DateTime string should not throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: "2016-12-01 00:00:00" }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Valid stopAt with Date string should not throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: "2016-12-01" }), | ||
nextRun = scheduler.next(); | ||
}).should.not.throw(); | ||
}); | ||
it("Invalid stopAt should throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: "hellu throw" }), | ||
nextRun = scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
it("stopAt with time only should throw", function () { | ||
(function () { | ||
var | ||
scheduler = new Cron("0 0 12 * * *", { stopAt: "00:35:00" }), | ||
nextRun = scheduler.next(); | ||
}).should.throw(); | ||
}); | ||
}); | ||
@@ -360,0 +449,0 @@ |
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
54536
1265