Skip to content
Myr

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

4 files+45 added−3 removed

modified:docs/content/3.manual/manual.yml+10 added
2601 unmodified lines
2602 - 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]']26052606 - title: "`isempty(exp)`"2607 body: |26082609 Returns true if `exp` produces no outputs, false otherwise.26102611 examples:2612 - program: 'isempty(empty)'2613 input: 'null'2614 output: ['true']26152616 - title: "`limit(n; exp)`"2617 body: |26182619 The `limit` function extracts up to `n` outputs from `exp`.
578 unmodified lines
modified:src/builtin.jq+1 added
166 unmodified lines
167 def _until:168 if cond then . else (next|_until) end;169 _until;170def 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;171def isempty(g): 0 == ((label $go | g | (1, break $go)) // 0);172def first(g): label $out | g | ., break $out;173def last(g): reduce g as $item (null; $item);174def nth($n; g): if $n < 0 then error("nth doesn't support negative indices") else last(limit($n + 1; g)) end;175def first: .[0];
133 unmodified lines
modified:tests/jq.test+12 added
1406 unmodified lines
1407(.. | select(type == "object" and has("b") and (.b | type) == "array")|.b) |= .[0]1408{"a": {"b": [1, {"b": 3}]}}1409{"a": {"b": 1}}14101411isempty(empty)1412null1413true14141415isempty(range(3))1416null1417false14181419isempty(1,error("foo"))1420null1421false14221423
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 lines
444.P445Booleans, 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: []"448As 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)448As 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.P451Once 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 lines
459460jq \'[\.user, \.projects[]]\'461 {"user":"stedolan", "projects": ["jq", "wikiflow"]}462=> ["stedolan", "jq", "wikiflow"]463464jq \'[ \.[] | \. * 2]\'465 [1, 2, 3]466=> [2, 4, 6]467.468.fi469.470.IP "" 0
1 unmodified line
472.SS "Object Construction: {}"473Like JSON, \fB{}\fR is for constructing objects (aka dictionaries or hashes), as in: \fB{"a": 42, "b": 17}\fR\.474.475.P472If 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}`\.476If 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.P479The 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 lines
2881.fi2882.2883.IP "" 02884.2885.SS "isempty(exp)"2886Returns true if \fBexp\fR produces no outputs, false otherwise\.2887.2888.IP "" 42889.2890.nf28912892jq \'isempty(empty)\'2893 null2894=> true2895.2896.fi2897.2898.IP "" 02899.2900.SS "limit(n; exp)"2901The \fBlimit\fR function extracts up to \fBn\fR outputs from \fBexp\fR\.2902.2903.IP "" 4
487 unmodified lines