Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array support causing unexpected value manipulated #35

Open
jasperjn opened this issue Sep 28, 2021 · 4 comments · May be fixed by #45
Open

Array support causing unexpected value manipulated #35

jasperjn opened this issue Sep 28, 2021 · 4 comments · May be fixed by #45

Comments

@jasperjn
Copy link

jasperjn commented Sep 28, 2021

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

set-value/index.js

Lines 148 to 150 in c574eb8

if (typeof next === 'number' && !Array.isArray(obj[key])) {
obj = obj[key] = [];
continue;

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
Copy link
Author

jasperjn commented Oct 5, 2021

@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!

@patrickfatrick
Copy link

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

@jasperjn
Copy link
Author

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

@snowbldr
Copy link

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 added a commit to snowbldr/set-value that referenced this issue Mar 26, 2022
@snowbldr snowbldr linked a pull request Mar 26, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@jasperjn @patrickfatrick @snowbldr and others