to_entries should not sort keys (fix #561)
This commit is contained in:
1 parent
f7a2af7052
commit
4a57b84db0
4 files changed
+28
-2
No files matched your search
@@ -488,6 +488,14 @@ static jv f_keys(jq_state *jq, jv input) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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){
|
static jv f_sort(jq_state *jq, jv input){
|
||||||
if (jv_get_kind(input) == JV_KIND_ARRAY) {
|
if (jv_get_kind(input) == JV_KIND_ARRAY) {
|
||||||
return jv_sort(input, jv_copy(input));
|
return jv_sort(input, jv_copy(input));
|
||||||
@@ -860,6 +868,7 @@ static const struct cfunction function_list[] = {
|
|||||||
{(cfunction_ptr)f_tonumber, "tonumber", 1},
|
{(cfunction_ptr)f_tonumber, "tonumber", 1},
|
||||||
{(cfunction_ptr)f_tostring, "tostring", 1},
|
{(cfunction_ptr)f_tostring, "tostring", 1},
|
||||||
{(cfunction_ptr)f_keys, "keys", 1},
|
{(cfunction_ptr)f_keys, "keys", 1},
|
||||||
|
{(cfunction_ptr)f_keys_unsorted, "keys_unsorted", 1},
|
||||||
{(cfunction_ptr)f_startswith, "startswith", 2},
|
{(cfunction_ptr)f_startswith, "startswith", 2},
|
||||||
{(cfunction_ptr)f_endswith, "endswith", 2},
|
{(cfunction_ptr)f_endswith, "endswith", 2},
|
||||||
{(cfunction_ptr)f_ltrimstr, "ltrimstr", 2},
|
{(cfunction_ptr)f_ltrimstr, "ltrimstr", 2},
|
||||||
@@ -965,7 +974,7 @@ static const char* const jq_builtins[] = {
|
|||||||
"def recurse: recurse(.[]?);",
|
"def recurse: recurse(.[]?);",
|
||||||
"def recurse_down: 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 from_entries: map({(.key): .value}) | add | .//={};",
|
||||||
"def with_entries(f): to_entries | map(f) | from_entries;",
|
"def with_entries(f): to_entries | map(f) | from_entries;",
|
||||||
"def reverse: [.[length - 1 - range(0;length)]];",
|
"def reverse: [.[length - 1 - range(0;length)]];",
|
||||||
|
|||||||
@@ -588,7 +588,7 @@ sections:
|
|||||||
input: '[[1,2], "string", {"a":2}, null]'
|
input: '[[1,2], "string", {"a":2}, null]'
|
||||||
output: [2, 6, 1, 0]
|
output: [2, 6, 1, 0]
|
||||||
|
|
||||||
- title: "`keys`"
|
- title: "`keys`, `keys_unsorted`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The builtin function `keys`, when given an object, returns
|
The builtin function `keys`, when given an object, returns
|
||||||
@@ -603,6 +603,10 @@ sections:
|
|||||||
When `keys` is given an array, it returns the valid indices
|
When `keys` is given an array, it returns the valid indices
|
||||||
for that array: the integers from 0 to length-1.
|
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:
|
examples:
|
||||||
- program: 'keys'
|
- program: 'keys'
|
||||||
input: '{"abc": 1, "abcd": 2, "Foo": 3}'
|
input: '{"abc": 1, "abcd": 2, "Foo": 3}'
|
||||||
|
|||||||
@@ -177,6 +177,7 @@ jv jv_setpath(jv, jv, jv);
|
|||||||
jv jv_getpath(jv, jv);
|
jv jv_getpath(jv, jv);
|
||||||
jv jv_delpaths(jv, jv);
|
jv jv_delpaths(jv, jv);
|
||||||
jv jv_keys(jv /*object or array*/);
|
jv jv_keys(jv /*object or array*/);
|
||||||
|
jv jv_keys_unsorted(jv /*object or array*/);
|
||||||
int jv_cmp(jv, jv);
|
int jv_cmp(jv, jv);
|
||||||
jv jv_group(jv, jv);
|
jv jv_group(jv, jv);
|
||||||
jv jv_sort(jv, jv);
|
jv jv_sort(jv, jv);
|
||||||
|
|||||||
@@ -428,6 +428,18 @@ static int string_cmp(const void* pa, const void* pb){
|
|||||||
return r;
|
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) {
|
jv jv_keys(jv x) {
|
||||||
if (jv_get_kind(x) == JV_KIND_OBJECT) {
|
if (jv_get_kind(x) == JV_KIND_OBJECT) {
|
||||||
int nkeys = jv_object_length(jv_copy(x));
|
int nkeys = jv_object_length(jv_copy(x));
|
||||||
|
|||||||
Reference in new issue
Block a user