]> granicus.if.org Git - jq/commitdiff
Allow the 'keys' function to take arrays.
authorStephen Dolan <mu@netsoc.tcd.ie>
Mon, 22 Oct 2012 22:31:07 +0000 (23:31 +0100)
committerStephen Dolan <mu@netsoc.tcd.ie>
Mon, 22 Oct 2012 22:31:07 +0000 (23:31 +0100)
builtin.c
docs/content/3.manual/manual.yml

index c3d6af198a4c214ef8ec2cff7bcd27caf406d91e..f184ff7fbecd7c7dc7a9b0ddcf3fbc3260af4724 100644 (file)
--- a/builtin.c
+++ b/builtin.c
@@ -228,6 +228,12 @@ static void f_keys(jv input[], jv output[]) {
     }
     free(keys);
     jv_free(input[0]);
+  } else if (jv_get_kind(input[0]) == JV_KIND_ARRAY) {
+    int n = jv_array_length(input[0]);
+    output[0] = jv_array();
+    for (int i=0; i<n; i++){
+      output[0] = jv_array_set(output[0], i, jv_number(i));
+    }
   } else {
     output[0] = jv_invalid_with_msg(jv_string_fmt("'keys' only supports object, not %s",
                                                   jv_kind_name(jv_get_kind(input[0]))));
index 5981ce42b4ac251f486d72cec6be372952608473..6c1077001d5f07620a97128490a2148819ec09d6 100644 (file)
@@ -385,10 +385,16 @@ sections:
           same for any two objects with the same set of keys,
           regardless of locale settings.
 
+          When `keys` is given an array, it returns the valid indices
+          for that array: the integers from 0 to length-1.
+
         examples:
           - program: 'keys'
             input: '{"abc": 1, "abcd": 2, "Foo": 3}'
             output: ['["Foo", "abc", "abcd"]']
+          - program: 'keys'
+            input: '[42,3,35]'
+            output: ['[0,1,2]']
 
       - title: `select`
         body: |