From 130152a2ac042ca5633c9311822768ed0dfaefe4 Mon Sep 17 00:00:00 2001 From: Muh Muhten Date: Thu, 21 Feb 2019 04:11:41 -0500 Subject: [PATCH] Ensure limit(0; ...) is empty --- src/builtin.jq | 6 +++--- tests/jq.test | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/builtin.jq b/src/builtin.jq index d5fa9b999e..d3a4bb16d1 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -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); diff --git a/tests/jq.test b/tests/jq.test index c027f5382a..f330fb0f6d 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -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"]