Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Update format-date to return empty string instead of 'Invalid Date' #155

Merged
merged 7 commits into from Apr 20, 2022

Conversation

jkuester
Copy link
Contributor

Updates the format-date xPath function to return '' instead of 'Invalid Date' when provided with an empty value or invalid date.

This normalizes the behavior with 1.x of openrosa and also with JavaRosa. See the linked issue for more information.

Closes enketo/enketo#864

.gitignore Outdated Show resolved Hide resolved
@alxndrsn
Copy link
Contributor

I think there are still place(s) where a date can be converted into the string "Invalid Date"; would it be consistent to address those in the same PR?

@jkuester
Copy link
Contributor Author

I think there are still place(s) where a date can be converted into the string "Invalid Date"; would it be consistent to address those in the same PR?

@alxndrsn I have done some digging (but am by no means an expert on this code base) and I have not found any other xPath functions that will directly return the String value "Invalid Date". (Which, I think, makes sense since the format-date/format-date-time functions are the only ones that I would expect to be converting date values to strings.)

That being said, the actual date function can return invalid Date values (Date objects that contain invalid data). When these are converted to strings (e.g. by calling date.toString()) they result in the string "Invalid Date" (because of the way Date is implemented in JS). Once easy way to get the date function to return an invalid value is to pass NaN.

type name label calculation
integer my_number Pick a Number  
calculate date_from_number My Date date(${my_number})
note show_date My Date: ${date_from_number}  

If no number value is entered, the note will show: My Date: Invalid Date. (Worth noting that this is the same behavior for the date function as in openrosa 1.x and so is not something that changed with the formate-date behavior.)

Does it make sense to try and update the date function to be smarter about its return value and instead of returning an invalid date, return an empty array node? This is what I see used when just directly getting the value of an unanswered date question:

{
  "t": "arr",
  "v": [ {} ]
}

Seems like that kind of change might have a lot of unintended side-effects, but I am not sure.

eyelidlessness added a commit to eyelidlessness/openrosa-xpath-evaluator that referenced this pull request Mar 17, 2022
This is a suggested change for enketo#155 so that:

- `asDate` still consistently returns a `Date` instance as one would expect
- `BlankDate` documents the purpose of its behavior
eyelidlessness added a commit to eyelidlessness/openrosa-xpath-evaluator that referenced this pull request Mar 17, 2022
This is a suggested change for enketo#155 so that:

- `asDate` still consistently returns a `Date` instance as one would expect
- `BlankDate` documents the purpose of its behavior
Copy link
Contributor

@eyelidlessness eyelidlessness left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For such a small change, this review was a bit of a wild ride! First I mistook the purposes of the changes entirely, then I got a little nitpicky, then I really stared at it for a while until I decided to try another approach entirely.

This change does certainly work as expected, but I think the alternative approach with a subclass will likely be easier to understand for future maintenance.

src/openrosa-extensions.js Outdated Show resolved Hide resolved
Copy link
Contributor

@eyelidlessness eyelidlessness left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you!

@eyelidlessness eyelidlessness merged commit 4067d28 into enketo:master Apr 20, 2022
@jkuester jkuester deleted the 154-fix-format-date branch April 21, 2022 20:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'Invalid Date' returned by format-date function when given an empty value
3 participants