TomFrost / Jexl

Javascript Expression Language: Powerful context-based expression parser and evaluator
MIT License
559 stars 90 forks source link

Inconsistency between null and undefined operations #133

Open mapedraza opened 10 months ago

mapedraza commented 10 months ago

After debugging some issues found in https://github.com/telefonicaid/iotagent-node-lib/issues/1440 we faced that the Library behaviour when facing null and undefined variables (or constants) in expressions is not so consistent. After that, we made some tests that con can check in the following JEXL Playground

In which the following expressions are defined:

{
t0n:null,
t0u:undefined,
t1n:n,
t1u:u,
t2n:n==null,
t2u:u==null,
t3n:n==undefined,
t3u:u==undefined,
t4n:null*2,
t4u:undefined*2,
t5n:n*2,
t5u:u*2,
t6n:null+2,
t6u:undefined+2,
t7n:n+2,
t7u:u+2,
t8n:n*2==null,
t8u:u*2==null,
t9n:n*2==undefined,
t9u:u*2==undefined
}

And the following output is returned:

{
  "t1n": null,
  "t2n": true,
  "t2u": true,
  "t3n": true,
  "t3u": true,
  "t4n": null,
  "t4u": null,
  "t5n": 0,
  "t5u": null,
  "t6n": null,
  "t6u": null,
  "t7n": 2,
  "t7u": null,
  "t8n": false,
  "t8u": false,
  "t9n": false,
  "t9u": false
}

Note that n:null and u is not defined (undefined)

It is specially meaningful cases like t5n that is 0 and t5u that is not appearing (undefined) or t4n vs t8n.

The expected behaviour is, that any arithmetic operation involving null or undefined return that value (or null, or undefined), being aligned when using null and undefined constants

mapedraza commented 10 months ago

Maybe related: https://github.com/TomFrost/Jexl/issues/65