Skip to content
Myr

Simple and efficient implementation of walk/1 (#2795)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by pkoppstein · committed by GitHub · parent 0f80921 · Browse files · 2 files+27 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−7 removed

  • tests:+19 added−0 removed
  • other:+8 added−7 removed

2 files+27 added−7 removed

modified:src/builtin.jq+8 added-7 removed
244 unmodified lines
245 end;246247# Apply f to composite entities recursively, and to atoms248def walk(f):249 . as $in250 | if type == "object" then251 reduce keys_unsorted[] as $key252 ( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f253 elif type == "array" then map( walk(f) ) | f254 else f255 end;249 def w:250 if type == "object"251 then map_values(w)252 elif type == "array" then map(w)253 else .254 end255 | f;256 w;257258# pathexps could be a stream of dot-paths259def pick(pathexps):260 . as $in
18 unmodified lines
modified:tests/jq.test+19 added
1973 unmodified lines
19741975map(try implode catch .)1976[123,["a"],[nan]]1977["implode input must be an array","string (\"a\") can't be imploded, unicode codepoint needs to be numeric","number (null) can't be imploded, unicode codepoint needs to be numeric"]19781979# walk1980walk(.)1981{"x":0}1982{"x":0}19831984walk(1)1985{"x":0}1986119871988# The following is a regression test, not a requirement:1989[walk(.,1)]1990{"x":0}1991[{"x":0},1]19921993# Issue #25841994walk(select(IN({}, []) | not))1995{"a":1,"b":[]}1996{"a":1}