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

cannot set w/ numeric key on an undefined nested object #59

Closed
json2d opened this issue Oct 7, 2021 · 3 comments
Closed

cannot set w/ numeric key on an undefined nested object #59

json2d opened this issue Oct 7, 2021 · 3 comments

Comments

@json2d
Copy link

json2d commented Oct 7, 2021

i'm trying to use set w/ a numeric key on a nested object that does not exist (i.e. it's undefined in the parent object)

UC.set('[0][1]', 'foo', {})
UC.set('0.1', 'foo', {}) // alternatively

expected result:

{0: {1: 'foo'}}

actual result:

{0: [null, 'foo']}

seems to yield an array by default when the key is numeric?

is there a way to specify the desired behavior and get the expected result?

v: unchanged@2.2.1

@planttheidea
Copy link
Owner

With a deep set, it infers what type of object you want based on the key type. Your nested key is a number, so it assumes an index of an array as that is a more common use case than a numbered object key.

Only options I can think of are either to create the nested object as desired so that inference doesn't need to happen, or have a post process that converts the array to an object.

@json2d
Copy link
Author

json2d commented Oct 11, 2021

@planttheidea thanks for the tip - the workaround you described definitely works, but i end up w/ a bit more low level code than i'd like

i could potentially see this kind of path w/ the dot notation potentially being an intuitive way to differentiate a deep set on an object w/ a numeric key:

UC.set('0.1', 'foo', {})

understandably though such a change could end up breaking other user's code

what are your thoughts on this?

@planttheidea
Copy link
Owner

planttheidea commented Oct 11, 2021

I did think it something else that may satisfy this: use an array of string keys instead of dot notation.

UC.set(['0', '1'], 'foo', {});

When an array, no parsing is done, so the key remains a string. This should cue the underlying "new object inferrer" to use an object.

I consider this an extreme edge case, and I'd rather not introduce a breaking change of far more common use cases to satisfy it.

@json2d json2d closed this as completed Oct 11, 2021
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

No branches or pull requests

2 participants