Skip to content

Commit

Permalink
Merge pull request #1837 from muhmuhten/limit_0
Browse files Browse the repository at this point in the history
  • Loading branch information
nicowilliams authored Feb 21, 2019
2 parents 730c730 + 130152a commit 9a0d5be
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def until(cond; next):
if cond then . else (next|_until) end;
_until;
def limit($n; exp):
if $n < 0 then exp
else label $out | foreach exp as $item ($n; .-1; $item, if . <= 0 then break $out else empty end)
end;
if $n > 0 then label $out | foreach exp as $item ($n; .-1; $item, if . <= 0 then break $out else empty end)
elif $n == 0 then empty
else exp end;
def isempty(g): 0 == ((label $go | g | (1, break $go)) // 0);
def first(g): label $out | g | ., break $out;
def last(g): reduce g as $item (null; $item);
Expand Down
8 changes: 8 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ null
[11,22,33,44,55,66,77,88,99]
[11,22,33]

[limit(0; error)]
"badness"
[]

[limit(1; 1, error)]
"badness"
[1]

[first(range(.)), last(range(.)), nth(0; range(.)), nth(5; range(.)), try nth(-1; range(.)) catch .]
10
[0,9,0,5,"nth doesn't support negative indices"]
Expand Down

0 comments on commit 9a0d5be

Please sign in to comment.