]> granicus.if.org Git - jq/commitdiff
def isempty(g) # Testing 'isempty(empty)' at line number 1364
authorpkoppstein <pkoppstein@gmail.com>
Sun, 26 Feb 2017 07:35:49 +0000 (02:35 -0500)
committerNicolas Williams <nico@cryptonector.com>
Sat, 15 Apr 2017 21:45:00 +0000 (16:45 -0500)
docs/content/3.manual/manual.yml
jq.1.prebuilt
src/builtin.jq
tests/jq.test

index 7bf9639df87997d39f9195ba971d214a17b5a21a..f666ceb1b6e96563601e7dd2ed62001ab32bdfc1 100644 (file)
@@ -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: |
 
index f09ee6ad909b61cb0fe8352f9fdf0b047dd14666..ef04d74109f59b7d57c0e32f0af5768223bf5749 100644 (file)
@@ -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
@@ -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)\.
@@ -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
 .
@@ -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)\.
@@ -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\.
 .
index 1432d9933eb6916e4b197122cca5425bfce49bf2..1a5d1e941bd56d82bcee4f3ec3f99571ac4c378b 100644 (file)
@@ -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;
index c07d1dc0026cb9a6cc07488fa6b4ef99696c947e..12d2b4dba0cbc801737af9f637771b5431f0c867 100644 (file)
@@ -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
+