Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix a bug where just entering /4 was legal syntax

  • Loading branch information...
commit d6ddb370b5f2c5f52a025f8818ded63907110559 1 parent 71d9006
@maltzj maltzj authored
Showing with 40 additions and 28 deletions.
  1. +33 −27 lib/cron.js
  2. +7 −1 tests/test-crontime.js
View
60 lib/cron.js
@@ -179,9 +179,9 @@ CronTime.prototype = {
throw new Error('Unknown alias: ' + alias);
}),
- split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/),
+ split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/),
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.
@@ -194,8 +194,10 @@ CronTime.prototype = {
/**
* Parse a field from the cron syntax.
*/
- _parseField: function(field, type, constraints) {
- var rangePattern = /(\d+?)(?:-(\d+?))?(?:\/(\d+?))?(?:,|$)/g,
+ _parseField: function(field, type, constraints){
+
+ //var rangePattern = /^(\*)(?:\/(\d+))?$|(\d+)(?:-(\d+))?(?:\/(\d+))?(?:,|$)/g
+ var rangePattern = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g,
typeObj = this[type],
diff, pointer,
low = constraints[0],
@@ -203,29 +205,33 @@ CronTime.prototype = {
// * is a shortcut to [lower-upper] range
field = field.replace(/\*/g, low + '-' + high);
-
- if (field.match(rangePattern)) {
- field.replace(rangePattern, function($0, lower, upper, step) {
- step = parseInt(step) || 1;
-
- // Positive integer higher than constraints[0]
- lower = Math.max(low, ~~Math.abs(lower));
-
- // Positive integer lower than constraints[1]
- upper = upper ? Math.min(high, ~~Math.abs(upper)) : lower;
-
- // Count from the lower barrier to the upper
- pointer = lower;
-
- do {
- typeObj[pointer] = true
- pointer += step;
- } while(pointer <= upper);
-
- });
- } else {
- throw new Error('Field (' + field + ') cannot be parsed');
- }
+
+ //commas separate information, so split based on those
+ var allRanges = field.split(',');
+
+ for(var i = 0; i < allRanges.length; i++){
+ if (allRanges[i].match(rangePattern)) {
+ allRanges[i].replace(rangePattern, function($0, lower, upper, step) {
+ step = parseInt(step) || 1;
+ // Positive integer higher than constraints[0]
+ lower = Math.max(low, ~~Math.abs(lower));
+
+ // Positive integer lower than constraints[1]
+ upper = upper ? Math.min(high, ~~Math.abs(upper)) : lower;
+
+ // Count from the lower barrier to the upper
+ pointer = lower;
+
+ do {
+ typeObj[pointer] = true
+ pointer += step;
+ }while(pointer <= upper);
+
+ });
+ } else {
+ throw new Error('Field (' + field + ') cannot be parsed');
+ }
+ }
}
};
View
8 tests/test-crontime.js
@@ -148,5 +148,11 @@ module.exports = testCase({
assert.ok(next > start);
}
assert.done();
- }
+ },
+ 'test illegal repetition syntax': function(assert){
+ assert.throws(function(){
+ new cron.CronTime('* * /4 * * *');
+ });
+ assert.done();
+ }
});
Please sign in to comment.
Something went wrong with that request. Please try again.