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 analyze: (condition) -> null
match: (condition, request) -> match: (condition, request) ->
day = new Date().getDay() day = new Date().getDay()
return condition.days.charCodeAt(day) > 64 if condition.days
return condition.startDay <= day and day <= condition.endDay return condition.startDay <= day and day <= condition.endDay
compile: (condition) -> compile: (condition) ->
val = new U2.AST_Call( getDay = new U2.AST_Call(
args: [] args: []
expression: new U2.AST_Dot( expression: new U2.AST_Dot(
property: 'getDay' property: 'getDay'
@ -602,14 +603,34 @@ module.exports = exports =
) )
) )
) )
@between val, condition.startDay, condition.endDay if condition.days
str: (condition) -> condition.startDay + '~' + condition.endDay 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) -> fromStr: (str, condition) ->
[startDay, endDay] = str.split('~') if str.indexOf('~') < 0 and str.length == 7
condition.startDay = parseInt(startDay, 10) condition.days = str
condition.endDay = parseInt(endDay, 10) else
condition.startDay = 0 unless 0 <= condition.startDay <= 6 [startDay, endDay] = str.split('~')
condition.endDay = 0 unless 0 <= condition.endDay <= 6 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 condition
'TimeCondition': 'TimeCondition':

View File

@ -1,6 +1,6 @@
chai = require 'chai' chai = require 'chai'
should = chai.should() should = chai.should()
lolex = require 'lolex'; lolex = require 'lolex'
describe 'Conditions', -> describe 'Conditions', ->
Conditions = require '../src/conditions' Conditions = require '../src/conditions'
@ -408,6 +408,46 @@ describe 'Conditions', ->
testCondDay(cond, 5, not 'match') testCondDay(cond, 5, not 'match')
testCondDay(cond, 6, 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', -> describe 'TimeCondition', ->
clock = null clock = null
before -> before ->
@ -595,6 +635,22 @@ describe 'Conditions', ->
startDay: 0 startDay: 0
endDay: 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', -> it 'should encode & decode TimeCondition correctly', ->
condition = condition =
conditionType: 'TimeCondition' conditionType: 'TimeCondition'