diff options
author | Nico Williams <nico@cryptonector.com> | 2019-02-21 08:17:41 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-21 08:17:41 -0600 |
commit | 9a0d5be790f0c89c02ffde11144157f067b19fd1 (patch) | |
tree | 3a16c1c9405c0c8e86cb170fca28b9bbc5282df4 | |
parent | 730c73094054f2ab6217bd4cb23c0e29bc4ce8ab (diff) | |
parent | 130152a2ac042ca5633c9311822768ed0dfaefe4 (diff) |
Merge pull request #1837 from muhmuhten/limit_0
-rw-r--r-- | src/builtin.jq | 6 | ||||
-rw-r--r-- | tests/jq.test | 8 |
2 files changed, 11 insertions, 3 deletions
diff --git a/src/builtin.jq b/src/builtin.jq index d5fa9b99..d3a4bb16 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 c027f538..f330fb0f 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"] |