Array is destructed
Closed this issue · 7 comments
aiphee commented
Hi, i thing that it should be mentioned in docs that if array is passed as variable it is destructed.
I had "fun" with that half hour until i found error in my code.
So please either make copy of array or mention it in docs.
Thank you.
const obj = {
a: {
b: {
c: 5,
},
},
};
const path = ['a', 'b', 'c'];
set(obj, path, 6);
console.log(obj.a.b); // Object { c: 6 }
set(obj, path, 6);
console.log(obj.a.b); // 6
aiphee commented
I would suggest this on first line of set:
path = path.split ? [...path] : path;
aiphee commented
Should i create pull request?
robinvdvleuten commented
@aiphee could you provide an example of what you mean? Your code example only shows setting an object property.
aiphee commented
@robinvdvleuten Yes, and in the process the variable path
gets changed which is wrong. Function params should never be mutated inside of function.
robinvdvleuten commented
@aiphee ok now I understand! Thanks for creating the issue!
aiphee commented
Thank you, works great! Could you also please make a new release?
robinvdvleuten commented
@aiphee just released at v2.0.1