set-value icon indicating copy to clipboard operation
set-value copied to clipboard

Array support causing unexpected value manipulated

Open jasperjn opened this issue 2 years ago • 4 comments

Hi, there

Since our team recently upgraded set-value from v3 to v4, we found there's a severe problem dealing with numeric path. before v4 paths are treated as properties, so it worked as expected like the following example Screen Shot 2021-09-28 at 3 01 26 PM

but since array index supported in v4, path contains numbers that causes target object's value transformed into an array anyway, and all its properties are gone. Screen Shot 2021-09-28 at 3 01 19 PM

the root cause might be on https://github.com/jonschlinkert/set-value/blob/c574eb8d94ba21d22b6fe5a5741fd4dbfc9dc47a/index.js#L148-L150 but the quandary is, how to determine a numeric path is tending to set value to an array index or an object property?

I made a codesandbox to reproduce this issue, versions switch is available on the left panel, please let me know if any further information is needed

jasperjn avatar Sep 28 '21 07:09 jasperjn

@KVROHITH098 Hi, what did you mean it fixed? I opened the link you provided and the version is 3.x so it did work. but if you switch the version to 4.x it breaks!

jasperjn avatar Oct 05 '21 02:10 jasperjn

I think the only way to sensibly handle arrays without breaking objects with numeric-string keys is to handle bracket notation like lodash does. eg a.b[0].c

patrickfatrick avatar Oct 22 '21 03:10 patrickfatrick

@patrickfatrick Ditto, It should be more explicit than just turning everything into array with numeric keys.

jasperjn avatar Oct 25 '21 08:10 jasperjn

I just experienced this issue during development. It was pretty surprising that the set-value call was deleting my data :|

At least an option to toggle this would be nice...

snowbldr avatar Mar 25 '22 16:03 snowbldr