robinvdvleuten/shvl

Array is destructed

Closed this issue · 7 comments

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

I would suggest this on first line of set:

path = path.split ? [...path] : path;

Should i create pull request?

@aiphee could you provide an example of what you mean? Your code example only shows setting an object property.

@robinvdvleuten Yes, and in the process the variable path gets changed which is wrong. Function params should never be mutated inside of function.

@aiphee ok now I understand! Thanks for creating the issue!

Thank you, works great! Could you also please make a new release?

@aiphee just released at v2.0.1