Tweaking select docs

This commit is contained in:
Mike Farah 2022-01-15 18:22:26 +11:00
parent 21cdbab0d7
commit de0716e875
2 changed files with 32 additions and 32 deletions

View File

@ -2,26 +2,6 @@
Select is used to filter arrays and maps by a boolean expression. Select is used to filter arrays and maps by a boolean expression.
## Select elements from array with regular expression
See more regular expression examples under the `string` operator docs.
Given a sample.yml file of:
```yaml
- this_0
- not_this
- nor_0_this
- thisTo_4
```
then
```bash
yq eval '.[] | select(test("[a-zA-Z]+_[0-9]$"))' sample.yml
```
will output
```yaml
this_0
thisTo_4
```
## Select elements from array using wildcard prefix ## Select elements from array using wildcard prefix
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
@ -75,6 +55,26 @@ go
going going
``` ```
## Select elements from array with regular expression
See more regular expression examples under the `string` operator docs.
Given a sample.yml file of:
```yaml
- this_0
- not_this
- nor_0_this
- thisTo_4
```
then
```bash
yq eval '.[] | select(test("[a-zA-Z]+_[0-9]$"))' sample.yml
```
will output
```yaml
this_0
thisTo_4
```
## Select items from a map ## Select items from a map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
@ -92,7 +92,7 @@ cat
dog dog
``` ```
## Use select + with_entries to filter map keys ## Use select and with_entries to filter map keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
name: bob name: bob

View File

@ -27,16 +27,6 @@ var selectOperatorScenarios = []expressionScenario{
expression: `select(false)`, expression: `select(false)`,
expected: []string{}, expected: []string{},
}, },
{
description: "Select elements from array with regular expression",
subdescription: "See more regular expression examples under the `string` operator docs.",
document: `[this_0, not_this, nor_0_this, thisTo_4]`,
expression: `.[] | select(test("[a-zA-Z]+_[0-9]$"))`,
expected: []string{
"D0, P[0], (!!str)::this_0\n",
"D0, P[3], (!!str)::thisTo_4\n",
},
},
{ {
description: "Select elements from array using wildcard prefix", description: "Select elements from array using wildcard prefix",
document: `[cat,goat,dog]`, document: `[cat,goat,dog]`,
@ -65,6 +55,16 @@ var selectOperatorScenarios = []expressionScenario{
"D0, P[3], (!!str)::going\n", "D0, P[3], (!!str)::going\n",
}, },
}, },
{
description: "Select elements from array with regular expression",
subdescription: "See more regular expression examples under the `string` operator docs.",
document: `[this_0, not_this, nor_0_this, thisTo_4]`,
expression: `.[] | select(test("[a-zA-Z]+_[0-9]$"))`,
expected: []string{
"D0, P[0], (!!str)::this_0\n",
"D0, P[3], (!!str)::thisTo_4\n",
},
},
{ {
skipDoc: true, skipDoc: true,
document: "a: hello", document: "a: hello",
@ -107,7 +107,7 @@ var selectOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
description: "Use select + with_entries to filter map keys", description: "Use select and with_entries to filter map keys",
document: `{name: bob, legs: 2, game: poker}`, document: `{name: bob, legs: 2, game: poker}`,
expression: `with_entries(select(.key | test("ame$")))`, expression: `with_entries(select(.key | test("ame$")))`,
expected: []string{ expected: []string{