Comments (7)
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..
from deepdash.
Thank you, will check
from deepdash.
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)
from deepdash.
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:
- if the current deep value is a leaf - then no problem just use given by callback value
- if the current deep value is not a leaf, then:
- if callback returned original value (new value === original value) - then set empty array or object to result object and go deeper for children
- if callback returned something else (new value !== original value) - then use given value as is and skip children.
- even worst-case should work: if callback will modify the current value and then return it, instead of creating a brand new one.
With the current implementation, we may read all the children's keys before the callback, but this will happen only if 'leavesOnly'=true.
- So if 'leavesOnly=true' then callback will never receive not-leaf value.
- And if 'leavesOnly=false' - then iteration will go over new value, no pre-cached keys in this case.
- And empty arrays/objects are considered as leaves, so callback can take some empty object, add put some nested data inside, but it's ok to not process added children, in this case, we will use returned value as is - because it was a leaf before processing.
I think all the edge cases are covered now.
from deepdash.
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)
from deepdash.
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.
from deepdash.
Ok, done as described in a recent comment.
Your example to work as expected should look like this:
deepdash/test/mapValuesDeep.test.js
Lines 253 to 261 in ce96433
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 😞 )
from deepdash.
Related Issues (20)
- omitDeep changes input on empty array HOT 1
- omitDeep doesn't omit objects HOT 7
- ERR_PACKAGE_PATH_NOT_EXPORTED HOT 3
- how to remove values from json if they exist
- Feature Request Parent path
- filterDeep not working on deeply nested object HOT 2
- Working with promises HOT 2
- Frustrating that things like `pickDeep` and `filterDeep` can only return a nested structure HOT 3
- deepdash-es not working with jest-resolve HOT 9
- deepFilter keep parents
- ESM: IIterateeContext typescript interface not exposed
- findPathDeep doesn't return first Value path
- findDeep or findDeepValue - Return object that contains the key & value
- eachDeep with childrenPath == wrong parent, parent path etc
- Bug in documentation _.findDeep*
- findDeep return types problem HOT 3
- Add contributing docs HOT 1
- findDeep is not a function HOT 1
- explicit return values, why and how does this work HOT 1
- mapValuesDeep mutates the original object if `leavesOnly: false` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepdash.