summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2014-06-08 02:01:44 -0500
committerNicolas Williams <nico@cryptonector.com>2014-06-08 02:01:44 -0500
commit1dbe9317bce859e9beaa2d7e0ceaad5d561eb48e (patch)
tree8a4319b06b8ed63be88e6163238819385ea485c5 /builtin.c
parentef4f3a54feaa9ebf967dd944bb8da5108a351d07 (diff)
Add `indices(s)`, improve `index(s)`, `rindex(s)`
Now these deal with arrays as input and `s` being an array or a scalar.
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/builtin.c b/builtin.c
index ad4145a5..274576bc 100644
--- a/builtin.c
+++ b/builtin.c
@@ -688,8 +688,9 @@ static const char* const jq_builtins[] = {
"def from_entries: map({(.key): .value}) | add;",
"def with_entries(f): to_entries | map(f) | from_entries;",
"def reverse: [.[length - 1 - range(0;length)]];",
- "def index(i): .[i][0];",
- "def rindex(i): .[i][-1:][0];",
+ "def indices(i): if type == \"array\" and (i|type) == \"array\" then .[i] elif type == \"array\" then .[[i]] else .[i] end;",
+ "def index(i): if type == \"array\" and (i|type) == \"array\" then .[i] elif type == \"array\" then .[[i]] else .[i] end | .[0];",
+ "def rindex(i): if type == \"array\" and (i|type) == \"array\" then .[i] elif type == \"array\" then .[[i]] else .[i] end | .[-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\" and . != \"object\");",
"def any: reduce .[] as $i (false; . or $i);",