Fix nth/2 to emit empty on index out of range

This commit is contained in:
itchyny authored and Nico Williams committed 2023-07-08 23:13:15 -05:00
1 parent e2bc8de8bb
commit d8072564c2
3 files changed
+11 -7

No files matched your search

+3 -1
View File
@@ -163,7 +163,9 @@ def any(condition): any(.[]; condition);
def all: all(.[]; .);
def any: any(.[]; .);
def last(g): reduce g as $item (null; $item);
def nth($n; g): if $n < 0 then error("nth doesn't support negative indices") else last(limit($n + 1; g)) end;
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 nth($n): .[$n];