Delete doesnt delte more than 7 items from an array
Zejnilovic opened this issue · 8 comments
a = {:itemList=>
[{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"},
{:alfa=>"beta"}]}
JsonPath.for(a.to_json).delete('$..itemList[1:10]')
returns
=> #<JsonPath::Proxy:0x000000014a0208
@obj=
{"itemList"=>
[{"alfa"=>"beta"}, {"alfa"=>"beta"}, {"alfa"=>"beta"}, {"alfa"=>"beta"}, {"alfa"=>"beta"}, {"alfa"=>"beta"}]}>
Which is too much of items returned. I can switch the 1:10 for 1:7 or 1:8, doesn't make a difference
More information follos
=> {:itemList=>
[{:alfa1=>"beta1"},
{:alfa2=>"beta2"},
{:alfa3=>"beta3"},
{:alfa4=>"beta4"},
{:alfa5=>"beta5"},
{:alfa6=>"beta6"},
{:alfa7=>"beta7"},
{:alfa8=>"beta8"},
{:alfa9=>"beta9"},
{:alfa10=>"beta10"},
{:alfa11=>"beta11"},
{:alfa12=>"beta12"}]}
[20] pry(main)>
[21] pry(main)> b = JsonPath.for(a.to_json).delete('$..itemList[1:6]').to_hash
=> {"itemList"=>
[{"alfa1"=>"beta1"},
{"alfa3"=>"beta3"},
{"alfa5"=>"beta5"},
{"alfa7"=>"beta7"},
{"alfa9"=>"beta9"},
{"alfa11"=>"beta11"}]}
It seems to me you are not deleting them in a bulk but one by one and that means
[1,2,3,4,5,6,7,8].delete 2
=> [1,3,4,5,6,7,8]
# this then skips the original 3 and goes to the new index 3 which is 4
[1,3,4,5,6,7,8].delete 3
=> [1,3,5,6,7,8]
I think this problem could be solved by reversing the order of deletion
In file enumerable.rb on line 63 I added reverse each and everything works fine for me now.
From:
(start_idx..end_idx).step(step) { |i| each(node, i, pos + 1, &blk) }
To:
(start_idx..end_idx).step(step).reverse_each { |i| each(node, i, pos + 1, &blk) }
Hi. Sorry for the late answer, I don't get notifications of new Issues unless I'm tagged.
Going to take a look at this as soon as I can. :)
@Skarlso next time I will tag you.
I made a Pull Request. It is not a complete fix but it does work. I presume there are better ways, but at least it underlines the problem.
Thank you for your reply
Np. I saw. I'll take a look later on, I think a bit of refactoring might do a better job rather than a fork. But maybe not. :) we'll see. :)
Sorry that it takes this long. :( I'm busy. But I started to look at this.
The step count is fishy... There is a bug there somewhere.
Not a problem, I understand that these projects are not life but a hobby.
I see there being a problem.
if you do:
[0,1,2,3,4,5,6,7].delete(1)
it deletes number 1 on position one. Then comes the second index:
[0,2,3,4,5,6,7].delete(2)
But second index is no longer the second index it was at the beginning and it deletes the number 3.
I think this is the problem. That is why I hotfixed it so it deletes from the end to the beginning.