Comparing nested arrays stored in Json.Value can result in runtime error
Hello,
I work in a team that's using Elm and we're very happy with it, thank you all for your hard work.
We have encountered a runtime error when attempting to compare Json.value objects with different structures. After a bit of digging, it became clear that the underlying issue was around comparing arrays with different levels of nesting:
$ elm-repl
> import Json.Decode as Json
> Json.decodeString Json.value "[[1]]" == Json.decodeString Json.value "[]"
TypeError: Cannot read property '0' of undefined
I appreciate that comparing Json.value objects is not very good form, and we'll workaround this issue by not doing that. It's not desirable for us to decode these particular objects, as they can be very large and the Elm part of our application does not actually need to have a model of the contents.
In any case, it seems desirable to remove this class of runtime errors, since it seems quite feasible to do so.
The error is coming from the eqHelp function. It appears that extending the null check to cover undefined would resolve this issue.
I do not have currently have time to prepare a quality PR, but perhaps something like this?
- if (typeof x !== 'object' || (x === null || y === null))
+ if (typeof x !== 'object' || (x === null || y === null || y === undefined))
{
typeof x === 'function' && __Error_throw(5);
return false;
}
Unsure if this should apply to x as well, but I'm pretty sure the above fix would be sufficient to avoid this runtime error.
I hope this is enough to go on. Happy to answer any questions.
Elm version: 0.18 Tested on: Firefox and Chrome latest on Mac OS
Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!
Here is what to expect next, and if anyone wants to comment, keep these things in mind.
Also just noticed another curiosity:
> Json.decodeString Json.value "[]" == Json.decodeString Json.value "[[1]]"
True : Bool
Should probably return False. Happy to open a separate issue if desired.