Clarify the `//` operator (close #2189)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicolas Williams · committed by Nico Williams · parent 949d38e · Browse files · 3 files·+66 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+66 added−4 removed
- tests:+14 added−0 removed
- other:+52 added−4 removed
Provenance
- Commit
ddef804- Tree
55bc07d- Author
Nicolas Williams <nico@cryptonector.com>- Committer
Nico Williams <nico@cryptonector.com>- Parent
949d38e
3 files changed+66 added−4 removed
modified:docs/content/manual/manual.yml+33 added-3 removed
2382 unmodified lines2383 2384 - title: "Alternative operator: `//`"2385 body: |2386 2387- A filter of the form `a // b` produces the same2388- results as `a`, if `a` produces results other than `false`2389- and `null`. Otherwise, `a // b` produces the same results as `b`.2387+ The `//` operator produces all the values of its left-hand2388+ side that are neither `false` nor `null`, or, if the2389+ left-hand side produces no values other than `false` or2390+ `true`, then `//` produces all the values of its right-hand2391+ side.2392 2393+ A filter of the form `a // b` produces all the results of2394+ `a` that are not `false` or `null`. If `a` produces no2395+ results, or no results other than `false` or `null`, then `a2396+ // b` produces the results of `b`.2397+2398 This is useful for providing defaults: `.foo // 1` will2399 evaluate to `1` if there's no `.foo` element in the2400 input. It's similar to how `or` is sometimes used in Python2401 (jq's `or` operator is reserved for strictly Boolean2402 operations).2403 2404+ Note: `some_generator // defaults_here` is not the same2405+ as `some_generator | . // defaults_here`. The latter will2406+ produce default values for all non-`false`, non-`null`2407+ values of the left-hand side, while the former will not.2408+ Precedence rules can make this confusing. For example, in2409+ `false, 1 // 2` the left-hand side of `//` is `1`, not2410+ `false, 1` -- `false, 1 // 2` parses the same way as `false,2411+ (1 // 2)`. In `(false, null, 1) | . // 42` the left-hand2412+ side of `//` is `.`, which always produces just one value,2413+ while in `(false, null, 1) // 42` the left-hand side is a2414+ generator of three values, and since it produces a2415+ value other `false` and `null`, the default `42` is not2416+ produced.2417+2418 examples:2419+ - program: 'empty // 42'2420+ input: 'null'2421+ output: ['42']2422 - program: '.foo // 42'2423 input: '{"foo": 19}'2424 output: ['19']2425 - program: '.foo // 42'2426 input: '{}'2427 output: ['42']2428+ - program: '(false, null, 1) // 42'2429+ input: 'null'2430+ output: ['1']2431+ - program: '(false, null, 1) | . // 42'2432+ input: 'null'2433+ output: ['42', '42', '1']2434 2435 - title: try-catch2436 body: |2437 1244 unmodified linesmodified:tests/man.test+14 added
767 unmodified lines768 [true, false | not]769 null770 [false, true]771 772+empty // 42773+null774+42775+776 .foo // 42777 {"foo": 19}778 19779 780 .foo // 42781 {}782 42783 784+(false, null, 1) // 42785+null786+1787+788+(false, null, 1) | . // 42789+null790+42791+42792+1793+794 try .a catch ". is not an object"795 true796 ". is not an object"797 164 unmodified linesmodified:jq.1.prebuilt+19 added-1 removed
2569 unmodified lines2570 .2571 .IP "" 02572 .2573 .SS "Alternative operator: //"2574-A filter of the form \fBa // b\fR produces the same results as \fBa\fR, if \fBa\fR produces results other than \fBfalse\fR and \fBnull\fR\. Otherwise, \fBa // b\fR produces the same results as \fBb\fR\.2574+The \fB//\fR operator produces all the values of its left\-hand side that are neither \fBfalse\fR nor \fBnull\fR, or, if the left\-hand side produces no values other than \fBfalse\fR or \fBtrue\fR, then \fB//\fR produces all the values of its right\-hand side\.2575 .2576 .P2577+A filter of the form \fBa // b\fR produces all the results of \fBa\fR that are not \fBfalse\fR or \fBnull\fR\. If \fBa\fR produces no results, or no results other than \fBfalse\fR or \fBnull\fR, then \fBa // b\fR produces the results of \fBb\fR\.2578+.2579+.P2580 This is useful for providing defaults: \fB\.foo // 1\fR will evaluate to \fB1\fR if there\'s no \fB\.foo\fR element in the input\. It\'s similar to how \fBor\fR is sometimes used in Python (jq\'s \fBor\fR operator is reserved for strictly Boolean operations)\.2581 .2582+.P2583+Note: \fBsome_generator // defaults_here\fR is not the same as \fBsome_generator | \. // defaults_here\fR\. The latter will produce default values for all non\-\fBfalse\fR, non\-\fBnull\fR values of the left\-hand side, while the former will not\. Precedence rules can make this confusing\. For example, in \fBfalse, 1 // 2\fR the left\-hand side of \fB//\fR is \fB1\fR, not \fBfalse, 1\fR \-\- \fBfalse, 1 // 2\fR parses the same way as \fBfalse, (1 // 2)\fR\. In \fB(false, null, 1) | \. // 42\fR the left\-hand side of \fB//\fR is \fB\.\fR, which always produces just one value, while in \fB(false, null, 1) // 42\fR the left\-hand side is a generator of three values, and since it produces a value other \fBfalse\fR and \fBnull\fR, the default \fB42\fR is not produced\.2584+.2585 .IP "" 42586 .2587 .nf2588 2589+jq \'empty // 42\'2590+ null2591+=> 422592+2593 jq \'\.foo // 42\'2594 {"foo": 19}2595 => 192596 2597 jq \'\.foo // 42\'2598 {}2599 => 422600+2601+jq \'(false, null, 1) // 42\'2602+ null2603+=> 12604+2605+jq \'(false, null, 1) | \. // 42\'2606+ null2607+=> 42, 42, 12608 .2609 .fi2610 .2611 .IP "" 01503 unmodified lines