Skip to content

Commit

Permalink
Addressed comments and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
capak07 committed Feb 20, 2024
1 parent 289bc3c commit bb21c85
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/jv_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,10 @@ jv jv_delpaths(jv object, jv paths) {
jv_free(paths);
return jv_invalid_with_msg(jv_string("Paths must be specified as an array"));
}
paths = jv_sort(jv_array_slice(paths, 0, jv_array_length(jv_copy(paths))), jv_copy(paths));
jv_array_foreach(paths, i, elem) {
int paths_length = jv_array_length(jv_copy(paths));
paths = jv_sort(jv_array_slice(paths, 0, paths_length), jv_copy(paths));
for(int i = 0; i < paths_length; i++) {
jv elem = jv_array_get(jv_copy(paths), i);
if (jv_get_kind(elem) != JV_KIND_ARRAY) {
jv_free(object);
jv_free(paths);
Expand All @@ -512,7 +514,7 @@ jv jv_delpaths(jv object, jv paths) {
}
jv_free(elem);
}
if (jv_array_length(jv_copy(paths)) == 0) {
if (paths_length == 0) {
// nothing is being deleted
jv_free(paths);
return object;
Expand All @@ -522,7 +524,7 @@ jv jv_delpaths(jv object, jv paths) {
jv_free(paths);
jv_free(object);
return jv_null();
}
}
return delpaths_sorted(object, paths, 0);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,10 @@ try delpaths(0) catch .
{}
"Paths must be specified as an array"

map(delpaths([[6], [7], [8], [9], [10]]))
[[0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]]
[[0, 1, 2, 3, 4, 5]]

del(.), del(empty), del((.foo,.bar,.baz) | .[2,3,0]), del(.foo[0], .bar[0], .foo, .baz.bar[0].x)
{"foo": [0,1,2,3,4], "bar": [0,1]}
null
Expand Down

0 comments on commit bb21c85

Please sign in to comment.