Skip to content

Commit 9a0d5be

Browse files
authored
Merge pull request #1837 from muhmuhten/limit_0
2 parents 730c730 + 130152a commit 9a0d5be

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/builtin.jq

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ def until(cond; next):
168168
if cond then . else (next|_until) end;
169169
_until;
170170
def limit($n; exp):
171-
if $n < 0 then exp
172-
else label $out | foreach exp as $item ($n; .-1; $item, if . <= 0 then break $out else empty end)
173-
end;
171+
if $n > 0 then label $out | foreach exp as $item ($n; .-1; $item, if . <= 0 then break $out else empty end)
172+
elif $n == 0 then empty
173+
else exp end;
174174
def isempty(g): 0 == ((label $go | g | (1, break $go)) // 0);
175175
def first(g): label $out | g | ., break $out;
176176
def last(g): reduce g as $item (null; $item);

tests/jq.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ null
304304
[11,22,33,44,55,66,77,88,99]
305305
[11,22,33]
306306

307+
[limit(0; error)]
308+
"badness"
309+
[]
310+
311+
[limit(1; 1, error)]
312+
"badness"
313+
[1]
314+
307315
[first(range(.)), last(range(.)), nth(0; range(.)), nth(5; range(.)), try nth(-1; range(.)) catch .]
308316
10
309317
[0,9,0,5,"nth doesn't support negative indices"]

0 commit comments

Comments
 (0)