def isempty(g) # Testing 'isempty(empty)' at line number 1364
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by pkoppstein · committed by Nicolas Williams · parent 76b1fc1 · Browse files · 4 files·+45 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+45 added−3 removed
- tests:+12 added−0 removed
- other:+33 added−3 removed
Provenance
- Commit
4b4cf78- Tree
d24f688- Author
pkoppstein <pkoppstein@gmail.com>- Committer
Nicolas Williams <nico@cryptonector.com>- Parent
76b1fc1
4 files changed+45 added−3 removed
modified:docs/content/3.manual/manual.yml+10 added
2601 unmodified lines2602 - program: 'reduce .[] as $n ([]; if $n%2==0 then empty else . + [$n] end)'2603 input: '[0,1,2,3,4,5]'2604 output: ['[1,3,5]']2605 2606+ - title: "`isempty(exp)`"2607+ body: |2608+2609+ Returns true if `exp` produces no outputs, false otherwise.2610+2611+ examples:2612+ - program: 'isempty(empty)'2613+ input: 'null'2614+ output: ['true']2615+2616 - title: "`limit(n; exp)`"2617 body: |2618 2619 The `limit` function extracts up to `n` outputs from `exp`.578 unmodified linesmodified:src/builtin.jq+1 added
166 unmodified lines167 def _until:168 if cond then . else (next|_until) end;169 _until;170 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;171+def isempty(g): 0 == ((label $go | g | (1, break $go)) // 0);172 def first(g): label $out | g | ., break $out;173 def last(g): reduce g as $item (null; $item);174 def nth($n; g): if $n < 0 then error("nth doesn't support negative indices") else last(limit($n + 1; g)) end;175 def first: .[0];133 unmodified linesmodified:tests/jq.test+12 added
1406 unmodified lines1407 (.. | select(type == "object" and has("b") and (.b | type) == "array")|.b) |= .[0]1408 {"a": {"b": [1, {"b": 3}]}}1409 {"a": {"b": 1}}1410 1411+isempty(empty)1412+null1413+true1414 1415+isempty(range(3))1416+null1417+false1418+1419+isempty(1,error("foo"))1420+null1421+false1422+1423+modified:jq.1.prebuilt+22 added-3 removed
1 .\" generated with Ronn/v0.7.32 .\" http://github.com/rtomayko/ronn/tree/0.7.33 .4-.TH "JQ" "1" "February 2017" "" ""4+.TH "JQ" "1" "April 2017" "" ""5 .6 .SH "NAME"7 \fBjq\fR \- Command\-line JSON processor8 .435 unmodified lines444 .P445 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\.446 .447 .SS "Array construction: []"448-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)448+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)449 .450 .P451 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)\.452 .6 unmodified lines459 460 jq \'[\.user, \.projects[]]\'461 {"user":"stedolan", "projects": ["jq", "wikiflow"]}462 => ["stedolan", "jq", "wikiflow"]463+464+jq \'[ \.[] | \. * 2]\'465+ [1, 2, 3]466+=> [2, 4, 6]467 .468 .fi469 .470 .IP "" 01 unmodified line472 .SS "Object Construction: {}"473 Like JSON, \fB{}\fR is for constructing objects (aka dictionaries or hashes), as in: \fB{"a": 42, "b": 17}\fR\.474 .475 .P472-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}`\.476+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\.477 .478 .P479 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)\.480 .2400 unmodified lines2881 .fi2882 .2883 .IP "" 02884 .2885+.SS "isempty(exp)"2886+Returns true if \fBexp\fR produces no outputs, false otherwise\.2887+.2888+.IP "" 42889+.2890+.nf2891+2892+jq \'isempty(empty)\'2893+ null2894+=> true2895+.2896+.fi2897+.2898+.IP "" 02899+.2900 .SS "limit(n; exp)"2901 The \fBlimit\fR function extracts up to \fBn\fR outputs from \fBexp\fR\.2902 .2903 .IP "" 4487 unmodified lines