}
}
+static jv f_keys_unsorted(jq_state *jq, jv input) {
+ if (jv_get_kind(input) == JV_KIND_OBJECT || jv_get_kind(input) == JV_KIND_ARRAY) {
+ return jv_keys_unsorted(input);
+ } else {
+ return type_error(input, "has no keys");
+ }
+}
+
static jv f_sort(jq_state *jq, jv input){
if (jv_get_kind(input) == JV_KIND_ARRAY) {
return jv_sort(input, jv_copy(input));
{(cfunction_ptr)f_tonumber, "tonumber", 1},
{(cfunction_ptr)f_tostring, "tostring", 1},
{(cfunction_ptr)f_keys, "keys", 1},
+ {(cfunction_ptr)f_keys_unsorted, "keys_unsorted", 1},
{(cfunction_ptr)f_startswith, "startswith", 2},
{(cfunction_ptr)f_endswith, "endswith", 2},
{(cfunction_ptr)f_ltrimstr, "ltrimstr", 2},
"def recurse: recurse(.[]?);",
"def recurse_down: recurse;",
- "def to_entries: [keys[] as $k | {key: $k, value: .[$k]}];",
+ "def to_entries: [keys_unsorted[] as $k | {key: $k, value: .[$k]}];",
"def from_entries: map({(.key): .value}) | add | .//={};",
"def with_entries(f): to_entries | map(f) | from_entries;",
"def reverse: [.[length - 1 - range(0;length)]];",
input: '[[1,2], "string", {"a":2}, null]'
output: [2, 6, 1, 0]
- - title: "`keys`"
+ - title: "`keys`, `keys_unsorted`"
body: |
The builtin function `keys`, when given an object, returns
When `keys` is given an array, it returns the valid indices
for that array: the integers from 0 to length-1.
+ The `keys_unsorted` function is just like `keys`, but if
+ the input is an object then the keys will not be sorted,
+ instead the keys will roughly be in insertion order.
+
examples:
- program: 'keys'
input: '{"abc": 1, "abcd": 2, "Foo": 3}'
return r;
}
+jv jv_keys_unsorted(jv x) {
+ if (jv_get_kind(x) != JV_KIND_OBJECT)
+ return jv_keys(x);
+ jv answer = jv_array_sized(jv_object_length(jv_copy(x)));
+ jv_object_foreach(x, key, value) {
+ answer = jv_array_append(answer, key);
+ jv_free(value);
+ }
+ jv_free(x);
+ return answer;
+}
+
jv jv_keys(jv x) {
if (jv_get_kind(x) == JV_KIND_OBJECT) {
int nkeys = jv_object_length(jv_copy(x));