Repository: jsonata-js/jsonata (js)

Issue:
Singleton operator has no effect on an array of objects
Consider the following input:
```
[
{"type":"command"},
{"type":"commands"}
]
```
and expression `$[type='command']`. It returns the first object properly. However, I want to use the singleton operator to have easier handling: `$[][type='command']` does not return an array of matches and `$[type='command'][]` does neither.
How can I always return an array of results in my case?

Pull request:
keepArray syntax should apply to all expression types
'keep array' syntax `[]` should apply to any expression type, not just location paths.

resolves #236 

Patch:
diff --git a/src/jsonata.js b/src/jsonata.js
index 1af405c..e4ec528 100644
--- a/src/jsonata.js
+++ b/src/jsonata.js
@@ -1410,6 +1410,9 @@ var jsonata = (function() {
                         throw err;
                     }
             }
+            if(expr.keepArray) {
+                result.keepArray = true;
+            }
             return result;
         };
 
@@ -1594,6 +1597,9 @@ var jsonata = (function() {
         }
 
         if(result && result.sequence) {
+            if(expr.keepArray) {
+                result.keepSingleton = true;
+            }
             result = result.value();
         }
 


Test patch:
diff --git a/test/test-suite/groups/flattening/case042.json b/test/test-suite/groups/flattening/case042.json
new file mode 100644
index 0000000..7132217
--- /dev/null
+++ b/test/test-suite/groups/flattening/case042.json
@@ -0,0 +1,5 @@
+{
+    "expr": "$[type='command'][]",
+    "data": [{"type":"command"},{"type":"commands"}],
+    "result": [{"type":"command"}]
+}
diff --git a/test/test-suite/groups/flattening/case043.json b/test/test-suite/groups/flattening/case043.json
new file mode 100644
index 0000000..0367515
--- /dev/null
+++ b/test/test-suite/groups/flattening/case043.json
@@ -0,0 +1,5 @@
+{
+    "expr": "$[][type='command']",
+    "data": [{"type":"command"},{"type":"commands"}],
+    "result": [{"type":"command"}]
+}
diff --git a/test/test-suite/groups/flattening/case044.json b/test/test-suite/groups/flattening/case044.json
new file mode 100644
index 0000000..b48a2f6
--- /dev/null
+++ b/test/test-suite/groups/flattening/case044.json
@@ -0,0 +1,5 @@
+{
+    "expr": "$filter($, function($e) { $e != 0 })[]",
+    "data": [0,0,5,0],
+    "result": [5]
+}