Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jsonpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
21 changes: 21 additions & 0 deletions test/test.slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
}), []);
});
});