Skip to content

Commit

Permalink
def isempty(g) # Testing 'isempty(empty)' at line number 1364
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoppstein authored and nicowilliams committed Apr 15, 2017
1 parent 76b1fc1 commit 4b4cf78
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/content/3.manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,16 @@ sections:
input: '[0,1,2,3,4,5]'
output: ['[1,3,5]']

- title: "`isempty(exp)`"
body: |
Returns true if `exp` produces no outputs, false otherwise.
examples:
- program: 'isempty(empty)'
input: 'null'
output: ['true']

- title: "`limit(n; exp)`"
body: |
Expand Down
25 changes: 22 additions & 3 deletions jq.1.prebuilt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "JQ" "1" "February 2017" "" ""
.TH "JQ" "1" "April 2017" "" ""
.
.SH "NAME"
\fBjq\fR \- Command\-line JSON processor
Expand Down Expand Up @@ -445,7 +445,7 @@ jq supports the same set of datatypes as JSON \- numbers, strings, booleans, arr
Booleans, null, strings and numbers are written the same way as in javascript\. Just like everything else in jq, these simple values take an input and produce an output \- \fB42\fR is a valid jq expression that takes an input, ignores it, and returns 42 instead\.
.
.SS "Array construction: []"
As in JSON, \fB[]\fR is used to construct arrays, as in \fB[1,2,3]\fR\. The elements of the arrays can be any jq expression\. All of the results produced by all of the expressions are collected into one big array\. You can use it to construct an array out of a known quantity of values (as in \fB[\.foo, \.bar, \.baz]\fR) or to "collect" all the results of a filter into an array (as in \fB[\.items[]\.name]\fR)
As in JSON, \fB[]\fR is used to construct arrays, as in \fB[1,2,3]\fR\. The elements of the arrays can be any jq expression, including a pipeline\. All of the results produced by all of the expressions are collected into one big array\. You can use it to construct an array out of a known quantity of values (as in \fB[\.foo, \.bar, \.baz]\fR) or to "collect" all the results of a filter into an array (as in \fB[\.items[]\.name]\fR)
.
.P
Once you understand the "," operator, you can look at jq\'s array syntax in a different light: the expression \fB[1,2,3]\fR is not using a built\-in syntax for comma\-separated arrays, but is instead applying the \fB[]\fR operator (collect results) to the expression 1,2,3 (which produces three different results)\.
Expand All @@ -460,6 +460,10 @@ If you have a filter \fBX\fR that produces four results, then the expression \fB
jq \'[\.user, \.projects[]]\'
{"user":"stedolan", "projects": ["jq", "wikiflow"]}
=> ["stedolan", "jq", "wikiflow"]

jq \'[ \.[] | \. * 2]\'
[1, 2, 3]
=> [2, 4, 6]
.
.fi
.
Expand All @@ -469,7 +473,7 @@ jq \'[\.user, \.projects[]]\'
Like JSON, \fB{}\fR is for constructing objects (aka dictionaries or hashes), as in: \fB{"a": 42, "b": 17}\fR\.
.
.P
If the keys are "identifier\-like", then the quotes can be left off, as in \fB{a:42, b:17}\. Keys generated by expressions need to be parenthesized, e\.g\.,\fR{("a"+"b"):59}`\.
If the keys are "identifier\-like", then the quotes can be left off, as in \fB{a:42, b:17}\fR\. Keys generated by expressions need to be parenthesized, e\.g\., \fB{("a"+"b"):59}\fR\.
.
.P
The value can be any expression (although you may need to wrap it in parentheses if it\'s a complicated one), which gets applied to the {} expression\'s input (remember, all filters have an input and an output)\.
Expand Down Expand Up @@ -2878,6 +2882,21 @@ jq \'reduce \.[] as $n ([]; if $n%2==0 then empty else \. + [$n] end)\'
.
.IP "" 0
.
.SS "isempty(exp)"
Returns true if \fBexp\fR produces no outputs, false otherwise\.
.
.IP "" 4
.
.nf

jq \'isempty(empty)\'
null
=> true
.
.fi
.
.IP "" 0
.
.SS "limit(n; exp)"
The \fBlimit\fR function extracts up to \fBn\fR outputs from \fBexp\fR\.
.
Expand Down
1 change: 1 addition & 0 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ 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, null]; if .[0] < 1 then break $out else [.[0] -1, $item] end; .[1]) 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);
def nth($n; g): if $n < 0 then error("nth doesn't support negative indices") else last(limit($n + 1; g)) end;
Expand Down
12 changes: 12 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1408,4 +1408,16 @@ true
{"a": {"b": [1, {"b": 3}]}}
{"a": {"b": 1}}

isempty(empty)
null
true

isempty(range(3))
null
false

isempty(1,error("foo"))
null
false


0 comments on commit 4b4cf78

Please sign in to comment.