Skip to content

Commit

Permalink
Fix last/0 definition to make pick(last) works
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Aug 13, 2023
1 parent 1d37721 commit 6920005
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def nth($n; g):
if $n < 0 then error("nth doesn't support negative indices")
else label $out | foreach g as $item ($n + 1; . - 1; if . <= 0 then $item, break $out else empty end) end;
def first: .[0];
def last: .[-1];
def last: .[length-1];
def nth($n): .[$n];
def combinations:
if length == 0 then [] else
Expand Down
7 changes: 5 additions & 2 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,11 @@ pick(first|first)
[[10,20],30]
[[10]]

# negative indices in path expressions (since last/1 is .[-1])
try pick(last) catch .
pick(last)
[1,2]
[null,2]

try pick(.[-1]) catch .
[1,2]
"pick/1 does not support negative indices and array slicing"

Expand Down

0 comments on commit 6920005

Please sign in to comment.