Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Canceling exceptions #87

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions later.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,12 @@ later = function() {
return parseExpr(hasSeconds ? e : "0 " + e);
};
later.parse.recur = function() {
var schedules = [], exceptions = [], cur, curArr = schedules, curName, values, every, modifier, applyMin, applyMax, i, last;
var schedules = [], exceptions = [], newException = {}, settingException = false, cur, curArr = schedules, curName, values, every, modifier, applyMin, applyMax, i, last;
function add(name, min, max) {
name = modifier ? name + "_" + modifier : name;
if (!cur) {
if (settingException && !cur) {
cur = curArr[curArr.push(newException) - 1];
} else if (!cur) {
curArr.push({});
cur = curArr[0];
}
Expand Down Expand Up @@ -1175,14 +1177,37 @@ later = function() {
add(last.n, start, end);
return this;
},
and: function() {
cur = curArr[curArr.push({}) - 1];
and: function(exceptionTag) {
if (exceptionTag && settingException) {
cur = curArr[curArr.push({
tag: exceptionTag
}) - 1];
} else {
cur = curArr[curArr.push({}) - 1];
}
if (curArr !== exceptions) {
newException = {};
settingException = false;
}
return this;
},
except: function() {
except: function(tag) {
if (tag) {
newException.tag = tag;
}
settingException = true;
curArr = exceptions;
cur = null;
return this;
},
removeException: function(tag) {
for (var i = 0; i < exceptions.length; i++) {
if (exceptions[i].tag === tag) {
exceptions.splice(i, 1);
break;
}
}
return this;
}
};
};
Expand Down
51 changes: 46 additions & 5 deletions src/parse/recur.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ later.parse.recur = function () {

var schedules = [],
exceptions = [],
newException = {},
settingException = false,
cur,
curArr = schedules,
curName,
Expand All @@ -29,7 +31,9 @@ later.parse.recur = function () {
function add(name, min, max) {
name = modifier ? name + '_' + modifier : name;

if (!cur) {
if (settingException && !cur) {
cur = curArr[curArr.push(newException) - 1];
} else if (!cur) {
curArr.push({});
cur = curArr[0];
}
Expand Down Expand Up @@ -484,10 +488,19 @@ later.parse.recur = function () {
* recur().every(5).minutes().on(1).dayOfWeek().and().every(10)
* .minutes().on(2).dayOfWeek();
*
* @param {String} exceptionTag: An optional tag used to cancel an exception in a composite exception schedule
* @api public
*/
and: function () {
cur = curArr[curArr.push({}) - 1];
and: function (exceptionTag) {
if (exceptionTag && settingException) {
cur = curArr[curArr.push({tag: exceptionTag}) - 1];
} else {
cur = curArr[curArr.push({}) - 1];
}
if (curArr !== exceptions) {
newException = {};
settingException = false;
}
return this;
},

Expand All @@ -502,12 +515,40 @@ later.parse.recur = function () {
* recur().at('08:00:00').on(2).dayOfWeek().except()
* .dayOfWeekCount(1);
*
* @param {String} tag: An optional tag used to cancel an exception
* @api public
*/
except: function () {
except: function (tag) {
if (tag) {
newException.tag = tag;
}
settingException = true;
curArr = exceptions;
cur = null;
return this;
},

/**
* Removes exceptions from a schedule. The given tag will be checked
* against the exceptions' tags in the array and will be removed
* based on a matching String tag. To remove an
* exception:
*
* var schedule = recur().at('08:00:00').on(2).dayOfWeek().except('Monday')
* .dayOfWeekCount(1);
* schedule.removeException('Monday');
*
* @param {String} tag: An optional tag used to cancel an exception
* @api public
*/
removeException: function (tag) {
for (var i = 0; i < exceptions.length; i++) {
if (exceptions[i].tag === tag) {
exceptions.splice(i, 1);
break;
}
}
return this;
}
};
};
};
32 changes: 31 additions & 1 deletion test/parse/recur-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,36 @@ describe('Parse Recur', function() {
r.exceptions[1].M.should.eql([0]);
});

it('should tag exception schedules', function() {
var r = recur().except('Sunday').on(0).dayOfWeek();
r.exceptions[0].should.eql({tag: 'Sunday', d: [0]});
r.exceptions[0].tag.should.eql('Sunday');
});

it('should tag both exception schedules when using .and()', function() {
var r = recur().except('Start time').on(5).minute().and('Skip month').on(0).month();
r.exceptions[0].should.eql({tag: 'Start time', m: [5]});
r.exceptions[1].should.eql({tag: 'Skip month', M: [0]});
r.exceptions[0].tag.should.eql('Start time');
r.exceptions[1].tag.should.eql('Skip month');
});

it('should remove an exception from the exception schedule', function() {
var r = recur().except('Sunday').on(0).dayOfWeek();
r.removeException('Sunday');
r.exceptions.should.eql([]);
});

it('should remove an exception from a composite exception schedule', function() {
var r = recur().except('Start time').on(5).minute().and('Skip month').on(0).month();
r.exceptions[0].tag.should.eql('Start time');
r.exceptions[1].tag.should.eql('Skip month');
r.removeException('Start time');
r.exceptions[0].tag.should.eql('Skip month');
r.removeException('Skip month');
r.exceptions.should.eql([]);
});

});

});
});