Fix cut-n-paste in leaf_paths; doc and test 'em
This commit is contained in:
1 parent
a45c937f80
commit
1fa55a3fae
3 files changed
+39
-1
No files matched your search
@@ -679,7 +679,7 @@ static const char* const jq_builtins[] = {
|
||||
"def index(i): .[i][0];",
|
||||
"def rindex(i): .[i][-1:][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]'
|
||||
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`
|
||||
body: |
|
||||
|
||||
|
||||
@@ -440,6 +440,14 @@ path(.)
|
||||
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])
|
||||
{"bar": 42, "foo": ["a", "b", "c", "d"]}
|
||||
"b"
|
||||
|
||||
Reference in new issue
Block a user