diff --git a/lib/convert.js b/lib/convert.js index aad6a15..0f0aa82 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -99,15 +99,21 @@ warnings: [] }; }; - genRange = function(range, self){ - var result; + genRange = function(type, range, self){ + var result, min, max; self == null && (self = '.'); result = []; if (!isNonsense(range.min)) { - result.push(self + " >" + (range.minInclusive === true ? '=' : '') + " " + exprValue(range.min)); + min = type === 'inputDate' + ? "date(" + exprValue(range.min) + ")" + : exprValue(range.min); + result.push(self + " >" + (range.minInclusive === true ? '=' : '') + " " + min); } if (!isNonsense(range.max)) { - result.push(self + " <" + (range.maxInclusive === true ? '=' : '') + " " + exprValue(range.max)); + max = type === 'inputDate' + ? "date(" + exprValue(range.max) + ")" + : exprValue(range.max); + result.push(self + " <" + (range.maxInclusive === true ? '=' : '') + " " + max); } return result; }; @@ -146,12 +152,12 @@ if ((range = (ref$ = question.range, delete question.range, ref$)) != null) { question.constraint = ((ref$ = question.constraint) != null ? ref$ - : []).concat(genRange(range)); + : []).concat(genRange(question.type, range)); } if ((count = (ref$ = question.count, delete question.count, ref$)) != null) { question.constraint = ((ref$ = question.constraint) != null ? ref$ - : []).concat(genRange(count, 'count-selected(.)')); + : []).concat(genRange(question.type, count, 'count-selected(.)')); } if (question.constraint.length === 0) { delete question.constraint; diff --git a/spec/src/convert-question-spec.ls b/spec/src/convert-question-spec.ls index c50a13b..654a2fd 100644 --- a/spec/src/convert-question-spec.ls +++ b/spec/src/convert-question-spec.ls @@ -211,26 +211,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)') diff --git a/src/convert.ls b/src/convert.ls index a8e03ea..0069b8b 100644 --- a/src/convert.ls +++ b/src/convert.ls @@ -91,12 +91,14 @@ media-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. @@ -128,10 +130,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