YuriGor / deepdash

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

omitDeep doesn't omit objects #112

Closed emahuni closed 3 years ago

emahuni commented 3 years ago

The expected result is an omission of anything with the given key, but omitDeep seems to be omitting scalars only.

z = { id: 2, a: { b: { c: 'ccc'}, d: ['kk','jj']}}
_.omitDeep(z, 'c')
// => { id: 2, a: { b: {} } } ✅
_.omitDeep(z, 'b')
// => { id: 2, a: { b: { c: 'ccc' }, d: [ 'kk', 'jj' ] } } ❌
_.omitDeep(z, 'd')
// => { id: 2, a: { b: { c: 'ccc' }, d: [ 'kk', 'jj' ] } } ❌

// lodash omit()
_.omit(z, 'a.b')
// => { id: 2, a: { d: [ 'kk', 'jj' ] } } ✅

It's expected to work similarly to _.omit() as indicated above.

emahuni commented 3 years ago

this seem to be inherited in other places in the lib as well. Continuing the above example:

_.pickDeep(z, 'd')
// => { a: { d: [] } } ❌

// expected result similar to lodash pick
_.pick(z, 'a.d')
// => { a: { d: [ 'kk', 'jj' ] } } ✅
Instrumedley commented 3 years ago

I was about to create an issue. I noticed that it does not work for Array as well

YuriGor commented 3 years ago

Hi all, this is as expected by default config: omit keeps parent if some children are not omitted.

To omit from the top as you expect try adding _.omitDeep(z, 'b',{onMatch:{skipChildren:true}})

You can check docs: https://deepdash.io/#omitdeep

As you see in example - "bad2" is also not omitted, because there is "good" inside.

Any further feedback is welcome in this issue even after closing, if there will be enough votes about such defaults are confusing - I will consider to change defaults on next major version.

emahuni commented 2 years ago

yes, I think the defaults are confusing... what I wrote as an issue is exactly what anyone would expect without reading and understanding anything. Then for those esoteric uses, one would adjust options. I already know how .omit works, so when I searched and found this lib, the assumption is that it should just behave like .omit out of the box (since it's ...dash as in lodash). The defaults are more opinionated I think.

emahuni commented 2 years ago

I will try to use the config as indicated in the meantime, as I trust you will do the right thing 😅 in the next version.

emahuni commented 2 years ago

oh my, it's working without the options added, wanted to have a false test first... did you already apply and publish the change? I recently installed it.

YuriGor commented 2 years ago

Hi, no changes yet. I updated playground referenced by docs to illustrate skip children option I suggested above, so you can see before/after: https://codepen.io/yurigor/pen/zQOMNj?editors=0010 image You see in second case bad2/good was also omitted because of this option, but in first case it's still there.