summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpkoppstein <pkoppstein@gmail.com>2017-02-26 02:35:49 -0500
committerNicolas Williams <nico@cryptonector.com>2017-04-15 16:45:00 -0500
commit4b4cf789e00f1139940f5f7cd2f2ddafaff3d89e (patch)
treed24f6888b19a8eda7efcc510bd7eef4c61be2a3d
parent76b1fc18f1d6cd574aa142f92a9a2ee6f7b6d1c6 (diff)
def isempty(g) # Testing 'isempty(empty)' at line number 1364
-rw-r--r--docs/content/3.manual/manual.yml10
-rw-r--r--jq.1.prebuilt25
-rw-r--r--src/builtin.jq1
-rw-r--r--tests/jq.test12
4 files changed, 45 insertions, 3 deletions
diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml
index 7bf9639d..f666ceb1 100644
--- a/docs/content/3.manual/manual.yml
+++ b/docs/content/3.manual/manual.yml
@@ -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: |
diff --git a/jq.1.prebuilt b/jq.1.prebuilt
index f09ee6ad..ef04d741 100644
--- a/jq.1.prebuilt
+++ b/jq.1.prebuilt
@@ -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\.
.
diff --git a/src/builtin.jq b/src/builtin.jq
index 1432d993..1a5d1e94 100644
--- a/src/builtin.jq
+++ b/src/builtin.jq
@@ -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;
diff --git a/tests/jq.test b/tests/jq.test
index c07d1dc0..12d2b4db 100644
--- a/tests/jq.test
+++ b/tests/jq.test
@@ -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
+