Skip to content

Commit

Permalink
revamp walk/1
Browse files Browse the repository at this point in the history
Resolves jqlang#2584; see also jqlang#2611
  • Loading branch information
pkoppstein committed Jul 5, 2023
1 parent c077b95 commit eeb08b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,18 @@ def bsearch($target):
else .[2]
end
end;

#
# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys_unsorted[] as $key
( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f
elif type == "array" then map( walk(f) ) | f
else f
end;

def w:
if type == "object"
then map_values(w)
elif type == "array" then map(w)
else .
end
| f;
w;
#
# SQL-ish operators here:
def INDEX(stream; idx_expr):
reduce stream as $row ({}; .[$row|idx_expr|tostring] = $row);
Expand Down
4 changes: 4 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1744,3 +1744,7 @@ false
1
1

# Issue #2584
walk(select(IN({}, []) | not))
{"a":1,"b":[]}
{"a":1}

0 comments on commit eeb08b5

Please sign in to comment.