diff --git a/src/jsonpath.js b/src/jsonpath.js index 15be426..e2bdbb5 100644 --- a/src/jsonpath.js +++ b/src/jsonpath.js @@ -608,7 +608,7 @@ JSONPath.prototype._slice = function ( const len = val.length, parts = loc.split(':'), step = (parts[2] && Number.parseInt(parts[2])) || 1; let start = (parts[0] && Number.parseInt(parts[0])) || 0, - end = (parts[1] && Number.parseInt(parts[1])) || len; + end = parts[1] ? Number.parseInt(parts[1]) : len; start = (start < 0) ? Math.max(0, start + len) : Math.min(len, start); end = (end < 0) ? Math.max(0, end + len) : Math.min(len, end); const ret = []; diff --git a/test/test.slice.js b/test/test.slice.js index afab5bd..5034642 100644 --- a/test/test.slice.js +++ b/test/test.slice.js @@ -42,4 +42,25 @@ describe('JSONPath - slice', function () { }); assert.deepEqual(result, expected); }); + + it('should return empty array for a slice with an explicit zero end', function () { + const jsonWithChildren = { + "name": "root", + "children": [ + {a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}, {a: 6} + ] + }; + assert.deepEqual(jsonpath({ + json: jsonWithChildren, + path: '$.children[:0]' + }), []); + assert.deepEqual(jsonpath({ + json: jsonWithChildren, + path: '$.children[2:0]' + }), []); + assert.deepEqual(jsonpath({ + json: jsonWithChildren, + path: '$.children[0:0]' + }), []); + }); });