adobe / json-formula

Query language for JSON documents
http://opensource.adobe.com/json-formula/
Apache License 2.0
19 stars 8 forks source link

fromEntries corner cases #153

Closed Eswcvlad closed 4 months ago

Eswcvlad commented 5 months ago

Don't think this is very important in practice, since it is mostly just an inverse of entries, but for completeness sake...


fromEntries(`null`) throws a TypeError instead of returning an empty object. With the current type coercion rules this is unexpected and might be something to keep in mind.


Incomplete array pairs are silently skipped: fromEntries([["a", `null`], ["b"], `[]`]) -> {"a": null}. Having an error in such cases, I think, would make sense.


On the other hand, having a bigger pair is also silent: fromEntries([["a", 1, 2]]) -> {"a": 1}. Not sure, if it makes sense to be strict here, but might be worth-wile to mention, that this is supported.


Key in the pair is converted to string somewhat unexpectedly (JS leaking):

JohnBrinkman commented 5 months ago

fromEntries() wants a very specific structure: an array of arrays where the nested arrays have a key (string) and value (any).

fromEntries([[[1, 2, 3], "val"]]) -> {"[1,2,3]": "val"} fromEntries([[{a: 1}, "val"]]) -> {"{\"a\": 1}": "val"}

JohnBrinkman commented 4 months ago

Changed my mind on coercing key values to string. I've opted for a strict interpretation: