mirror of
https://github.com/stedolan/jq.git
synced 2024-05-11 05:55:39 +00:00
Fix cut-n-paste in leaf_paths; doc and test 'em
This commit is contained in:
@@ -679,7 +679,7 @@ static const char* const jq_builtins[] = {
|
|||||||
"def index(i): .[i][0];",
|
"def index(i): .[i][0];",
|
||||||
"def rindex(i): .[i][-1:][0];",
|
"def rindex(i): .[i][-1:][0];",
|
||||||
"def paths: path(recurse(if (type|. == \"array\" or . == \"object\") then .[] else empty end))|select(length > 0);",
|
"def paths: path(recurse(if (type|. == \"array\" or . == \"object\") then .[] else empty end))|select(length > 0);",
|
||||||
"def leaf_paths: . as $dot|paths|select(. as $p|$dot|getpath($p)|type|. == \"array\" or . == \"object\");",
|
"def leaf_paths: . as $dot|paths|select(. as $p|$dot|getpath($p)|type|. != \"array\" and . != \"object\");",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -638,6 +638,36 @@ sections:
|
|||||||
input: '[1,2,3]'
|
input: '[1,2,3]'
|
||||||
output: ['[2,3,4]']
|
output: ['[2,3,4]']
|
||||||
|
|
||||||
|
- title: `paths`
|
||||||
|
body: |
|
||||||
|
|
||||||
|
Outputs the paths to all the elements in its input (except it
|
||||||
|
does not output the empty list, representing . itself).
|
||||||
|
|
||||||
|
`paths` is equivalent to
|
||||||
|
|
||||||
|
def paths: path(recurse(if (type|. == "array" or . == "object") then .[] else empty end))|select(length > 0);
|
||||||
|
|
||||||
|
examples:
|
||||||
|
- program: '[paths]'
|
||||||
|
input: '[1,[[],{"a":2}]]'
|
||||||
|
output: ['[[0],[1],[1,0],[1,1],[1,1,"a"]]']
|
||||||
|
|
||||||
|
- title: `leaf_paths`
|
||||||
|
body: |
|
||||||
|
|
||||||
|
Outputs the paths to all the leaves (non-array, non-object
|
||||||
|
elements) in its input.
|
||||||
|
|
||||||
|
`leaf_paths` is equivalent to
|
||||||
|
|
||||||
|
def leaf_paths: . as $dot|paths|select(. as $p|$dot|getpath($p)|type|. != "array" and . != "object");
|
||||||
|
|
||||||
|
examples:
|
||||||
|
- program: '[leaf_paths]'
|
||||||
|
input: '[1,[[],{"a":2}]]'
|
||||||
|
output: ['[[0],[1,1,"a"]]']
|
||||||
|
|
||||||
- title: `add`
|
- title: `add`
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
|
|||||||
@@ -440,6 +440,14 @@ path(.)
|
|||||||
42
|
42
|
||||||
[]
|
[]
|
||||||
|
|
||||||
|
[paths]
|
||||||
|
[1,[[],{"a":2}]]
|
||||||
|
[[0],[1],[1,0],[1,1],[1,1,"a"]]
|
||||||
|
|
||||||
|
[leaf_paths]
|
||||||
|
[1,[[],{"a":2}]]
|
||||||
|
[[0],[1,1,"a"]]
|
||||||
|
|
||||||
["foo",1] as $p | getpath($p), setpath($p; 20), delpaths([$p])
|
["foo",1] as $p | getpath($p), setpath($p; 20), delpaths([$p])
|
||||||
{"bar": 42, "foo": ["a", "b", "c", "d"]}
|
{"bar": 42, "foo": ["a", "b", "c", "d"]}
|
||||||
"b"
|
"b"
|
||||||
|
|||||||
Reference in New Issue
Block a user