diff --git a/omega-pac/src/conditions.coffee b/omega-pac/src/conditions.coffee index cf75359..895fe88 100644 --- a/omega-pac/src/conditions.coffee +++ b/omega-pac/src/conditions.coffee @@ -590,9 +590,10 @@ module.exports = exports = analyze: (condition) -> null match: (condition, request) -> day = new Date().getDay() + return condition.days.charCodeAt(day) > 64 if condition.days return condition.startDay <= day and day <= condition.endDay compile: (condition) -> - val = new U2.AST_Call( + getDay = new U2.AST_Call( args: [] expression: new U2.AST_Dot( property: 'getDay' @@ -602,14 +603,34 @@ module.exports = exports = ) ) ) - @between val, condition.startDay, condition.endDay - str: (condition) -> condition.startDay + '~' + condition.endDay + if condition.days + new U2.AST_Binary( + left: new U2.AST_Call( + expression: new U2.AST_Dot( + expression: new U2.AST_String value: condition.days + property: 'charCodeAt' + ) + args: [getDay] + ) + operator: '>' + right: new U2.AST_Number value: 64 + ) + else + @between getDay, condition.startDay, condition.endDay + str: (condition) -> + if condition.days + condition.days + else + condition.startDay + '~' + condition.endDay fromStr: (str, condition) -> - [startDay, endDay] = str.split('~') - condition.startDay = parseInt(startDay, 10) - condition.endDay = parseInt(endDay, 10) - condition.startDay = 0 unless 0 <= condition.startDay <= 6 - condition.endDay = 0 unless 0 <= condition.endDay <= 6 + if str.indexOf('~') < 0 and str.length == 7 + condition.days = str + else + [startDay, endDay] = str.split('~') + condition.startDay = parseInt(startDay, 10) + condition.endDay = parseInt(endDay, 10) + condition.startDay = 0 unless 0 <= condition.startDay <= 6 + condition.endDay = 0 unless 0 <= condition.endDay <= 6 condition 'TimeCondition': diff --git a/omega-pac/test/conditions.coffee b/omega-pac/test/conditions.coffee index 7f2eaaa..46bf39e 100644 --- a/omega-pac/test/conditions.coffee +++ b/omega-pac/test/conditions.coffee @@ -1,6 +1,6 @@ chai = require 'chai' should = chai.should() -lolex = require 'lolex'; +lolex = require 'lolex' describe 'Conditions', -> Conditions = require '../src/conditions' @@ -408,6 +408,46 @@ describe 'Conditions', -> testCondDay(cond, 5, not 'match') testCondDay(cond, 6, not 'match') + it 'should match according to .days', -> + cond = + conditionType: 'WeekdayCondition' + days: 'SMTWtFs' + + testCondDay(cond, 0, 'match') + testCondDay(cond, 1, 'match') + testCondDay(cond, 2, 'match') + testCondDay(cond, 3, 'match') + testCondDay(cond, 4, 'match') + testCondDay(cond, 5, 'match') + testCondDay(cond, 6, 'match') + + cond = + conditionType: 'WeekdayCondition' + days: 'S-TW-F-' + + testCondDay(cond, 0, 'match') + testCondDay(cond, 1, not 'match') + testCondDay(cond, 2, 'match') + testCondDay(cond, 3, 'match') + testCondDay(cond, 4, not 'match') + testCondDay(cond, 5, 'match') + testCondDay(cond, 6, not 'match') + + it 'should prefer .days to .startDay and .endDay', -> + cond = + conditionType: 'WeekdayCondition' + days: '--TW---' + startDay: 0 + endDay: 0 + + testCondDay(cond, 0, not 'match') + testCondDay(cond, 1, not 'match') + testCondDay(cond, 2, 'match') + testCondDay(cond, 3, 'match') + testCondDay(cond, 4, not 'match') + testCondDay(cond, 5, not 'match') + testCondDay(cond, 6, not 'match') + describe 'TimeCondition', -> clock = null before -> @@ -595,6 +635,22 @@ describe 'Conditions', -> startDay: 0 endDay: 0 ) + it 'should encode & decode WeekdayCondition with days', -> + condition = + conditionType: 'WeekdayCondition' + days: 'SMTWtFs' + result = Conditions.str(condition) + result.should.equal('Weekday: SMTWtFs') + cond = Conditions.fromStr(result) + cond.should.eql(condition) + + condition = + conditionType: 'WeekdayCondition' + days: 'SM-W-Fs' + result = Conditions.str(condition) + result.should.equal('Weekday: SM-W-Fs') + cond = Conditions.fromStr(result) + cond.should.eql(condition) it 'should encode & decode TimeCondition correctly', -> condition = conditionType: 'TimeCondition'