From ca3070f9c22a6dcb2c6a6d367042d78a6ea12aea Mon Sep 17 00:00:00 2001 From: andrew-coleman Date: Fri, 23 Oct 2020 09:06:22 +0100 Subject: [PATCH] $eval() - array inputs should be wrapped The logic for handling the second parameter to this function should be the same as for handling the input to any expression. This includes wrapping a top level array input in a singleton sequence so it gets treated as a single input. This code was missing from this function and is added here. Signed-off-by: andrew-coleman --- src/jsonata.js | 5 +++ .../groups/function-eval/case008.json | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 test/test-suite/groups/function-eval/case008.json diff --git a/src/jsonata.js b/src/jsonata.js index aa40ccf4..bfc8db99 100644 --- a/src/jsonata.js +++ b/src/jsonata.js @@ -1759,6 +1759,11 @@ var jsonata = (function() { var input = this.input; if(typeof focus !== 'undefined') { input = focus; + // if the input is a JSON array, then wrap it in a singleton sequence so it gets treated as a single input + if(Array.isArray(input) && !isSequence(input)) { + input = createSequence(input); + input.outerWrapper = true; + } } try { diff --git a/test/test-suite/groups/function-eval/case008.json b/test/test-suite/groups/function-eval/case008.json new file mode 100644 index 00000000..e22d718e --- /dev/null +++ b/test/test-suite/groups/function-eval/case008.json @@ -0,0 +1,34 @@ +[ + { + "expr": "$eval(\"{ 'test': 1 }\", [])", + "data": {}, + "bindings": {}, + "result": { + "test": 1 + } + }, + { + "expr": "$eval(\"{ 'test': 1 }\")", + "data": [], + "bindings": {}, + "result": { + "test": 1 + } + }, + { + "expr": "$eval(\"{ 'test': 1 }\", [1,2,3])", + "data": [], + "bindings": {}, + "result": { + "test": 1 + } + }, + { + "expr": "$eval(\"{ 'test': 1 }\")", + "data": [1,2,3], + "bindings": {}, + "result": { + "test": 1 + } + } +]