Repository: jsonata-js/jsonata (js)

Issue:
Handling of "undefined" input to negate operator is inconsistent?
JSONata, as a query system should be tolerant to finding ‘nothing’ as a routine thing. This is reflected by the fact that passing the `undefined` value (the Javascript value used to represent 'nothing' here) to any operator or function does not raise an error, instead just itself returning `undefined`. 

For instance:
Where the path `x` is not on the context:
`x + 1 => undefined` (no error is raised)

_(To my knowledge, the only meaningful exception to this behaviour is `$exists(undefined) => false`, but there may be others)._

This behaviour is not observed by the negate operator. E.g.
`-x` raises an error (`Cannot negate a non-numeric value: undefined`).

Is this another deliberate exception to this behaviour, or is this a bug?

Pull request description:
unary negation of undefined (no match) should return undefined
resolves #99 

When handling the unary negation operator `-`, let `undefined` (no match) pass through.

Patch:
diff --git a/jsonata.js b/jsonata.js
index 30ac3f0..c2dc89b 100644
--- a/jsonata.js
+++ b/jsonata.js
@@ -1817,7 +1817,9 @@ var jsonata = (function() {
         switch (expr.value) {
             case '-':
                 result = yield * evaluate(expr.expression, input, environment);
-                if (isNumeric(result)) {
+                if(typeof result === 'undefined') {
+                    result = undefined;
+                } else if (isNumeric(result)) {
                     result = -result;
                 } else {
                     throw {


Test patch:
diff --git a/test/test-suite/groups/numeric-operators/case016.json b/test/test-suite/groups/numeric-operators/case016.json
new file mode 100644
index 0000000..2668ba3
--- /dev/null
+++ b/test/test-suite/groups/numeric-operators/case016.json
@@ -0,0 +1,6 @@
+{
+    "expr": "- notexist",
+    "dataset": "dataset0",
+    "bindings": {},
+    "undefinedResult": true
+}
\ No newline at end of file