diff options
author | Nicolas Williams <nico@cryptonector.com> | 2014-02-05 23:14:05 -0600 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2014-02-05 23:14:05 -0600 |
commit | 1fa55a3fae3dc6b8a2018ce64786b5b5570fde70 (patch) | |
tree | 482c79eae9e84fac1891c981af91f6e0f7275ce2 | |
parent | a45c937f80dd93a298d7baf0947385db9f3032dc (diff) |
Fix cut-n-paste in `leaf_paths`; doc and test 'em
-rw-r--r-- | builtin.c | 2 | ||||
-rw-r--r-- | docs/content/3.manual/manual.yml | 30 | ||||
-rw-r--r-- | tests/all.test | 8 |
3 files changed, 39 insertions, 1 deletions
@@ -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\");", }; diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index 0261738a..ff9d429a 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -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: | diff --git a/tests/all.test b/tests/all.test index f50b7eb1..96a08da1 100644 --- a/tests/all.test +++ b/tests/all.test @@ -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" |