omit-deep icon indicating copy to clipboard operation
omit-deep copied to clipboard

omitDeep mutates objects

Open mattgabor opened this issue 6 years ago • 4 comments

It looks like the omitDeep function is mutating the object I pass in as well as returning it, is this expected behavior or should it be a pure function?

mattgabor avatar Jul 26 '18 21:07 mattgabor

Object.assign() also mutates objects, but you avoid mutation by shallow cloning before you pass the object to omit-deep (or Object.assign), by passing an empty object as the first argument:

const obj = Object.assign({} , myObject)`

In this case, if the mutation is happening in omit-deep in a place where you aren't able to get to the code or choose whether or not the object is mutated, I would think that's a bug. I'll take a look as soon as I have a chance.

jonschlinkert avatar Jul 26 '18 21:07 jonschlinkert

Makes sense, I was able to avoid this by doing a shallow clone of my object before passing it to omit-deep but ideally there would be a way to decide whether to mutate or not, since the function returns a new object anyways. But that’s up to you of course 🙂

mattgabor avatar Jul 26 '18 21:07 mattgabor

@mattgabor Actually, the function does not return a new object. Unless you give undefined as its first parameter. And that is going to change soon.

c-vetter avatar Sep 11 '18 09:09 c-vetter

For anyone having this same issue, I can recommend omit-deep-lodash. It does not mutate inputs, as evidenced by https://github.com/odynvolk/omit-deep-lodash/pull/16

spawnia avatar Oct 13 '20 09:10 spawnia