Permalink
Browse files

Support standard no-seconds cron syntax by defaulting to 0.

  • Loading branch information...
1 parent e3204de commit 7102fdbb4e1e0c5fa31a09dc0e39fb36aabcb1ba @danhbear committed Jul 24, 2012
Showing with 38 additions and 6 deletions.
  1. +1 −1 README.md
  2. +9 −5 lib/cron.js
  3. +13 −0 tests/test-cron.js
  4. +15 −0 tests/test-crontime.js
View
@@ -7,7 +7,7 @@ Originally this project was a NodeJS fork of [James Padolsey's][jamespadolsey] [
After [Craig Condon][crcn] made some updates and changes to the code base this has evolved to something that has a bit of both. The cron syntax parsing is mostly James' while using timeout instead of interval is Craig's.
-Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format.
+Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format. Although the Cron patterns suported here extend on the standard Unix format to support seconds digits, leaving it off will default to 0 and match the Unix behavior.
Usage (basic cron usage):
==========
View
@@ -27,6 +27,7 @@ function CronTime(source, zone) {
CronTime.map = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'];
CronTime.constraints = [ [0, 59], [0, 59], [0, 23], [1, 31], [0, 11], [0, 6] ];
+CronTime.parseDefaults = [ '0', '*', '*', '*', '*', '*' ];
CronTime.aliases = {
jan:0, feb:1, mar:2, apr:3, may:4, jun:5, jul:6, aug:7, sep:8, oct:9, nov:10, dec:11,
sun:0, mon:1, tue:2, wed:3, thu:4, fri:5, sat:6
@@ -179,11 +180,14 @@ CronTime.prototype = {
throw new Error('Unknown alias: ' + alias);
}),
split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/),
- cur, len = 6;
-
- while (len--) {
- cur = split[len] || '*';
- this._parseField(cur, CronTime.map[len], CronTime.constraints[len]);
+ cur, i = 0, len = CronTime.map.length;
+
+ for (; i < CronTime.map.length; i++) {
+ // If the split source string doesn't contain all digits,
+ // assume defaults for first n missing digits.
+ // This adds support for 5-digit standard cron syntax
+ cur = split[i - (len - split.length)] || CronTime.parseDefaults[i];
+ this._parseField(cur, CronTime.map[i], CronTime.constraints[i]);
}
},
View
@@ -34,6 +34,19 @@ module.exports = testCase({
assert.done();
}, 5250);
},
+ 'test standard cron no-seconds syntax doesnt send on seconds (* * * * *)': function(assert) {
+ assert.expect(1);
+ var called = 0;
+ var c = new cron.CronJob('* * * * *', function() {
+ called++;
+ }, null, true);
+ setTimeout(function() {
+ c.stop();
+ // Note that it could be called once if the test lands on a minute boundary
+ assert.ok(called <= 1);
+ assert.done();
+ }, 5250);
+ },
'test every second for 5 seconds with oncomplete (* * * * * *)': function(assert) {
assert.expect(6);
var c = new cron.CronJob('* * * * * *', function(done) {
View
@@ -37,6 +37,21 @@ module.exports = testCase({
});
assert.done();
},
+ 'test no second digit doesnt throw, i.e. standard cron format (8 8 8 8 5)': function(assert) {
+ assert.expect(1);
+ assert.doesNotThrow(function() {
+ new cron.CronTime('* * * * *');
+ });
+ assert.done();
+ },
+ 'test no second digit defaults to 0, i.e. standard cron format (8 8 8 8 5)': function(assert) {
+ assert.expect(1);
+ var now = new Date();
+ var standard = new cron.CronTime('8 8 8 8 5');
+ var extended = new cron.CronTime('0 8 8 8 8 5');
+ assert.ok(standard._getNextDateFrom(now).getTime() === extended._getNextDateFrom(now).getTime());
+ assert.done();
+ },
'test hyphen (0-10 * * * * *)': function(assert) {
assert.expect(1);
assert.doesNotThrow(function() {

0 comments on commit 7102fdb

Please sign in to comment.