Skip to content

Commit

Permalink
bug: call date() for date type range constraints.
Browse files Browse the repository at this point in the history
  • Loading branch information
issa-tseng committed Mar 6, 2019
1 parent cbb7fb5 commit 9b86a64
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
18 changes: 12 additions & 6 deletions lib/convert.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 21 additions & 5 deletions spec/src/convert-question-spec.ls
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,42 @@ describe \constraint ->
result = { type: \inputText, length: false } |> convert-simple
expect(result.constraint).toBe(undefined)

test 'build number/date range constraint generation (incl/excl combo)' ->
test 'build number range constraint generation (incl/excl combo)' ->
result = { type: \inputNumber, range: { min: 3, minInclusive: true, max: 9 } } |> convert-simple
expect(result.constraint).toBe('(. >= 3) and (. < 9)')

test 'build number/date range constraint generation (excl/incl combo)' ->
test 'build number range constraint generation (excl/incl combo)' ->
result = { type: \inputNumber, range: { min: 3, max: 9, maxInclusive: true } } |> convert-simple
expect(result.constraint).toBe('(. > 3) and (. <= 9)')

test 'build number/date range constraint generation (min-only)' ->
test 'build number range constraint generation (min-only)' ->
result = { type: \inputNumber, range: { min: 3, minInclusive: true } } |> convert-simple
expect(result.constraint).toBe('(. >= 3)')

test 'build number/date range constraint generation (max)' ->
test 'build number range constraint generation (max)' ->
result = { type: \inputNumber, range: { max: 3, maxInclusive: true } } |> convert-simple
expect(result.constraint).toBe('(. <= 3)')

test 'build number/date range false pruning' ->
test 'build text range false pruning' ->
result = { type: \inputText, range: false } |> convert-simple
expect(result.constraint).toBe(undefined)

test 'build date range constraint generation (incl/excl combo)' ->
result = { type: \inputDate, range: { min: '2009-01-01', minInclusive: true, max: '2009-12-31' } } |> convert-simple
expect(result.constraint).toBe("(. >= date('2009-01-01')) and (. < date('2009-12-31'))")

test 'build date range constraint generation (excl/incl combo)' ->
result = { type: \inputDate, range: { min: '2009-01-01', max: '2009-12-31', maxInclusive: true } } |> convert-simple
expect(result.constraint).toBe("(. > date('2009-01-01')) and (. <= date('2009-12-31'))")

test 'build date range constraint generation (min-only)' ->
result = { type: \inputDate, range: { min: '2009-01-01', minInclusive: true } } |> convert-simple
expect(result.constraint).toBe("(. >= date('2009-01-01'))")

test 'build date range constraint generation (max)' ->
result = { type: \inputDate, range: { max: '2009-12-31', maxInclusive: true } } |> convert-simple
expect(result.constraint).toBe("(. <= date('2009-12-31'))")

test 'build select multiple response count constraint generation' ->
result = { type: \inputSelectMany, count: { min: 3, max: 9, maxInclusive: true } } |> convert-simple
expect(result.constraint).toBe('(count-selected(.) > 3) and (count-selected(.) <= 9)')
Expand Down
12 changes: 7 additions & 5 deletions src/convert.ls
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ range-appearance-conversion =
new-context = -> { seen-fields: {}, choices: {}, warnings: [] }

# creates a range expression.
gen-range = (range, self = \.) ->
gen-range = (type, range, self = \.) ->
result = []
unless is-nonsense(range.min)
result.push("#self >#{if range.minInclusive is true then \= else ''} #{expr-value(range.min)}")
min = if type is \inputDate then "date(#{expr-value(range.min)})" else expr-value(range.min)
result.push("#self >#{if range.minInclusive is true then \= else ''} #{min}")
unless is-nonsense(range.max)
result.push("#self <#{if range.maxInclusive is true then \= else ''} #{expr-value(range.max)}")
max = if type is \inputDate then "date(#{expr-value(range.max)})" else expr-value(range.max)
result.push("#self <#{if range.maxInclusive is true then \= else ''} #{max}")
result

# returns an intermediate-formatted question clone (purely functional), but mutates context.
Expand Down Expand Up @@ -110,10 +112,10 @@ convert-question = (question, context, prefix = []) ->
question.constraint = (question.constraint ? []) ++ "regex(., \"^.{#{length.min},#{length.max}}$\")"
# merge number/date range.
if (range = delete question.range)?
question.constraint = (question.constraint ? []) ++ gen-range(range)
question.constraint = (question.constraint ? []) ++ gen-range(question.type, range)
# merge select multiple choice count range.
if (count = delete question.count)?
question.constraint = (question.constraint ? []) ++ gen-range(count, 'count-selected(.)')
question.constraint = (question.constraint ? []) ++ gen-range(question.type, count, 'count-selected(.)')
# convert constraint field back into an expression.
if question.constraint.length is 0
delete question.constraint
Expand Down

0 comments on commit 9b86a64

Please sign in to comment.