From 6ed2938e970c5458f6871dc5612837e1c5bf11c6 Mon Sep 17 00:00:00 2001 From: Solomon English Date: Thu, 8 May 2014 01:08:16 -0700 Subject: [PATCH] support wrapping months --- later.js | 20 +++++++++++++++----- src/parse/cron.js | 28 ++++++++++++++++++++++------ test/parse/cron-test.js | 5 +++++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/later.js b/later.js index 2cdbb58..fc120a8 100644 --- a/later.js +++ b/later.js @@ -926,15 +926,25 @@ later = function() { return clone; } function add(sched, name, min, max, inc) { - var i = min; if (!sched[name]) { sched[name] = []; } - while (i <= max) { - if (sched[name].indexOf(i) < 0) { - sched[name].push(i); + var loopTo = function(start, max){ + var i = start; + while ( i <= max ){ + if (sched[name].indexOf(i) < 0) { + sched[name].push(i); + } + i += inc || 1; } - i += inc || 1; + } + if ( min > max ){ + var field = FIELDS[name]; + loopTo(min, field[2]); + loopTo(field[1], max); + } + else{ + loopTo(min, max); } } function addHash(schedules, curSched, value, hash) { diff --git a/src/parse/cron.js b/src/parse/cron.js index 634893e..87bb7a6 100644 --- a/src/parse/cron.js +++ b/src/parse/cron.js @@ -79,17 +79,33 @@ later.parse.cron = function (expr, hasSeconds) { * @param {Int} inc: The increment to use between min and max */ function add(sched, name, min, max, inc) { - var i = min; - if (!sched[name]) { sched[name] = []; } - while (i <= max) { - if (sched[name].indexOf(i) < 0) { - sched[name].push(i); + var loopTo = function(start, max){ + var i = start; + + while ( i <= max ){ + if (sched[name].indexOf(i) < 0) { + sched[name].push(i); + } + i += inc || 1; } - i += inc || 1; + } + + // if the min is greater than the max, loop it + if ( min > max ){ + var field = FIELDS[name]; + + // add up to the max value + loopTo(min, field[2]); + + // loop it over + loopTo(field[1], max); + } + else{ + loopTo(min, max); } } diff --git a/test/parse/cron-test.js b/test/parse/cron-test.js index a1c16f7..241763e 100644 --- a/test/parse/cron-test.js +++ b/test/parse/cron-test.js @@ -262,6 +262,11 @@ describe('Parse Cron', function() { p.schedules[0].should.eql({M: [1,3,5]}); }); + it('should parse a wrapped range value', function() { + var p = parse('* * * * 11-2 *', true); + p.schedules[0].should.eql({M: [11,12,1,2]}); + }); + }); describe('day of week', function() {