Support selecting individual .days in WeekdayCondition.

This commit is contained in:
FelisCatus 2016-03-17 19:18:48 +08:00
parent 482c6f6340
commit 63938d0e15
2 changed files with 86 additions and 9 deletions

View File

@ -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,9 +603,29 @@ 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) ->
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)

View File

@ -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'