YuriGor / deepdash

eachDeep, filterDeep, findDeep, someDeep, omitDeep, pickDeep, keysDeep etc.. Tree traversal library written in Underscore/Lodash fashion
https://deepdash.io/
MIT License
275 stars 12 forks source link

mapValuesDeep array issue #60

Closed GaborTorma closed 4 years ago

GaborTorma commented 4 years ago

Hi

I found some interesting things in mapValuesDeep funtcion (and maybe on the other map function...): If a change an array value to the object, the array values remaining in the object with index key.

You can check it on my pen: https://codepen.io/gabortorma/pen/zYvgXaG

Thanx! Gábor

YuriGor commented 4 years ago

Thank you, will check

YuriGor commented 4 years ago

Hi, could you please make sure you are not processing children of this array you changed to be an object?

As per docs, mapValuesDeep keeps the same structure, so when you are iterating over children - children values will be set to the old paths, and old path used to have 0 index, so Lodash's set method will create all the missing parents.

If you want to change structure - use mapKeysDeep, it should handle such complicated cases. (But probably i didn't test converting arrays to objects)

YuriGor commented 4 years ago

The more I think about this issue, the closer I am to adding support of expected by you behavior, because it really makes more sense..

YuriGor commented 4 years ago

Thank you for a very good example! After a bit of meditation on this, I see the easy(=fast) and a reasonable way to solve it is to do something like you tried to do:

With the current implementation, we may read all the children's keys before the callback, but this will happen only if 'leavesOnly'=true.

I think all the edge cases are covered now.

YuriGor commented 4 years ago

Oh, we also have tree mode, when childrenPath is specified in options. So in this case we need to clone whole value from callback, except children. (Similar to filterDeep in tree mode)

YuriGor commented 4 years ago

No, relying on the fact of reference equality is a bad design. If the user needs immutable mapping - he has no control.

So I'll better check if returned not-leaf value is of the same type as original or not. By default if it is the same type - we should go deeper over children, otherwise - should not. Additionally, I will add 'skipChildren' method to the context. If user call this method with true or false - it will override the default behavior.

YuriGor commented 4 years ago

Ok, done as described in a recent comment. Your example to work as expected should look like this: https://github.com/YuriGor/deepdash/blob/ce9643332ed04b64f4b3114222554b5f998b4103/test/mapValuesDeep.test.js#L253-L261

Let me know if you will find something else or stuck with how to implement something. Currently, docs are not covering such details (I always have no time for good docs :disappointed: )