Skip to content

Commit 42c8c7e

Browse files
committed
negative index #77
happened generally when the index is less than (-size) - i.e. counting in to the array still yields a negative index
1 parent 0645e7b commit 42c8c7e

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/main/java/com/dashjoin/jsonata/Jsonata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ Frame createFrameFromTuple(Frame environment, Map<String, Object> tuple) {
500500
// count in from end of array
501501
index = ((List)input).size() + index;
502502
}
503-
var item = index<((List)input).size() ? ((List)input).get(index) : null;
503+
var item = 0<=index && index<((List)input).size() ? ((List)input).get(index) : null;
504504
if(item != null) {
505505
if(item instanceof List) {
506506
results = (List)item;

src/test/java/com/dashjoin/jsonata/ArrayTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
public class ArrayTest {
1515

16+
@Test
17+
public void testNegativeIndex() {
18+
Jsonata expr = jsonata("item[-1]");
19+
Assertions.assertNull(expr.evaluate(Map.of("item", List.of())));
20+
expr = jsonata("$[-1]");
21+
Assertions.assertNull(expr.evaluate(List.of()));
22+
}
23+
1624
@Test
1725
public void testArray() {
1826
Jsonata expr1 = jsonata("{'key': $append($.[{'x': 'y'}],$.[{'a': 'b'}])}");

0 commit comments

Comments
 (0)