From 9b86a64e5bf001f381b453f8fbac51c35d39ac6e Mon Sep 17 00:00:00 2001 From: Clint Tseng Date: Tue, 5 Mar 2019 16:26:34 -0800 Subject: [PATCH] bug: call date() for date type range constraints. * see https://github.com/opendatakit/build/issues/197 and https://github.com/opendatakit/build/pull/221 --- lib/convert.js | 18 ++++++++++++------ spec/src/convert-question-spec.ls | 26 +++++++++++++++++++++----- src/convert.ls | 12 +++++++----- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/convert.js b/lib/convert.js index c3360a1..e7c1364 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -80,15 +80,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; }; @@ -127,12 +133,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 f92608e..1cbf273 100644 --- a/spec/src/convert-question-spec.ls +++ b/spec/src/convert-question-spec.ls @@ -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)') diff --git a/src/convert.ls b/src/convert.ls index b70525a..cd164a4 100644 --- a/src/convert.ls +++ b/src/convert.ls @@ -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. @@ -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