Repository: jsonata-js/jsonata (js)

Issue:
Error selecting or performing operations on JSON objects containing "sequence" key
node v8.9.3
jsonata v1.5.4
Also able to duplicate on [JSONata Exerciser](http://try.jsonata.org/)

Attempt any selection on an object with a "selection" key.

### The following example works a s expected:
**JSON**
`{
  "foo": {
    "sequence1": 123,
    "bar": "baz"
  }
}`

**JSONata**
`foo.bar`

**Result**
`"baz"`

### When the key is changed to "sequence", it fails:
**JSON**
`{
  "foo": {
    "sequence": 123,
    "bar": "baz"
  }
}`

**JSONata**
`foo.bar`

**Result**
`n.value is not a function`

Repo search revealed 'sequence' being used in https://github.com/jsonata-js/jsonata/blob/7aa65f2295e0605a284e1f1083cf528aceb4ba79/src/jsonata.js#L1557-L1566

Pull request description:
more robust check for sequence property
resolves #247 

Patch:
diff --git a/src/jsonata.js b/src/jsonata.js
index 8f15677..be01367 100644
--- a/src/jsonata.js
+++ b/src/jsonata.js
@@ -1596,7 +1596,7 @@ var jsonata = (function() {
             exitCallback(expr, input, environment, result);
         }
 
-        if(result && result.sequence) {
+        if(result && isSequence(result)) {
             if(expr.keepArray) {
                 result.keepSingleton = true;
             }
@@ -1618,6 +1618,17 @@ var jsonata = (function() {
         return sequence;
     }
 
+    function isSequence(value) {
+        var sequence = value.sequence;
+        if(sequence === true) {
+            var desc = Object.getOwnPropertyDescriptor(value, 'sequence');
+            if(desc.enumerable === false) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     /**
      * Converts an array to a result sequence (by adding special properties)
      * @param {Array} arr - the array to convert
@@ -1725,7 +1736,7 @@ var jsonata = (function() {
         }
 
         var resultSequence = createSequence();
-        if(lastStep && result.length === 1 && Array.isArray(result[0]) && !result[0].sequence) {
+        if(lastStep && result.length === 1 && Array.isArray(result[0]) && !isSequence(result[0])) {
             resultSequence = result[0];
         } else {
             // flatten the sequence


Test patch:
diff --git a/test/test-suite/groups/literals/case018.json b/test/test-suite/groups/literals/case018.json
new file mode 100644
index 0000000..5e1a479
--- /dev/null
+++ b/test/test-suite/groups/literals/case018.json
@@ -0,0 +1,5 @@
+{
+    "expr": "{ 'foo': { 'sequence': 123, 'bar': 'baz' } } ",
+    "data": {},
+    "result": { "foo": { "sequence": 123, "bar": "baz" } }
+}
diff --git a/test/test-suite/groups/literals/case019.json b/test/test-suite/groups/literals/case019.json
new file mode 100644
index 0000000..2df1d04
--- /dev/null
+++ b/test/test-suite/groups/literals/case019.json
@@ -0,0 +1,5 @@
+{
+    "expr": "{ 'foo': { 'sequence': true, 'bar': 'baz' } } ",
+    "data": {},
+    "result": { "foo": { "sequence": true, "bar": "baz" } }
+}