Skip to content

Commit

Permalink
Fix deletion using assigning empty against arrays (fix jqlang#2051)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed May 29, 2020
1 parent 6306ac8 commit 6ba3fa6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def min_by(f): _min_by_impl(map([f]));
def add: reduce .[] as $x (null; . + $x);
def del(f): delpaths([path(f)]);
def _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value));
def _modify(paths; update): reduce path(paths) as $p (.; label $out | (setpath($p; getpath($p) | update) | ., break $out), delpaths([$p]));
def _modify(ps; f):
reduce path(ps) as $p
([., []]; label $out | ([0] + $p as $q | setpath($q; getpath($q) | f) | ., break $out), .[1] += [$p])
| . as $x | $x[0] | delpaths($x[1]);
def map_values(f): .[] |= f;

# recurse
Expand Down
5 changes: 5 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,11 @@ def inc(x): x |= .+1; inc(.[].a)
{"a":[{"b":5}]}
{"a":[{"c":3,"b":5}]}

# #2051, deletion using assigning empty against arrays
(.[] | select(. >= 2)) |= empty
[1,5,3,0,7]
[1,0]

.[2][3] = 1
[4]
[4, null, [null, null, null, 1]]
Expand Down

0 comments on commit 6ba3fa6

Please sign in to comment.