Skip to content
Myr

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

3 files+66 added−4 removed

modified:docs/content/manual/manual.yml+33 added-3 removed
2382 unmodified lines
23832384 - title: "Alternative operator: `//`"2385 body: |23862387 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.23922393 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`.23972398 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).24032404 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.24172418 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']24342435 - title: try-catch2436 body: |2437
1244 unmodified lines
modified:tests/man.test+14 added
767 unmodified lines
768[true, false | not]769null770[false, true]771772empty // 42773null77442775776.foo // 42777{"foo": 19}77819779780.foo // 42781{}78242783784(false, null, 1) // 42785null7861787788(false, null, 1) | . // 42789null79042791427921793794try .a catch ". is not an object"795true796". is not an object"797
164 unmodified lines
modified:jq.1.prebuilt+19 added-1 removed
2569 unmodified lines
2570.2571.IP "" 02572.2573.SS "Alternative operator: //"2574A 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\.2574The \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.P2577A 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.P2580This 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.P2583Note: \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.nf25882589jq \'empty // 42\'2590 null2591=> 4225922593jq \'\.foo // 42\'2594 {"foo": 19}2595=> 1925962597jq \'\.foo // 42\'2598 {}2599=> 4226002601jq \'(false, null, 1) // 42\'2602 null2603=> 126042605jq \'(false, null, 1) | \. // 42\'2606 null2607=> 42, 42, 12608.2609.fi2610.2611.IP "" 0
1503 unmodified lines