-
Notifications
You must be signed in to change notification settings - Fork 180
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
Treat Number Keys as Valid Keys in the set Function #446
Comments
I think you just have to define |
Thank you for your reply! I really appreciate your time in looking into this. The core issue actually comes from how the path is being parsed. For example: set({}, 'user.1083.name', 'Alice') Before (unexpected behavior): { user: [, , , , , ..., { name: 'Alice' }] } // user becomes an array After the fix (expected behavior): { user: { "1083": { name: 'Alice' } } } // handled number-key as a key This update ensures that numeric keys are always treated as object properties, so the function now works correctly even if user isn’t pre-defined. Let me know if you have any other thoughts! 😊 |
Your PR changes the path grammar. If we're gonna make a breaking change, the “path” argument should really be an array. Then we could differentiate There could be a |
The docs for import { get } from 'radash'
const fish = {
name: 'Bass',
weight: 8,
sizes: [
{
maturity: 'adult',
range: [7, 18],
unit: 'inches'
}
]
}
get( fish, 'sizes[0].range[1]' ) // 18
get( fish, 'sizes.0.range.1' ) // 18 So I would consider your PR a breaking change. |
@aleclarson So I thought it would be reasonable to treat number-like strings as object keys when they are used without brackets, since there are cases where numbers are used as keys. But I understand your concern. The I also really like your idea of using an array as a path. It would allow us to explicitly distinguish "1234" (string) from 1234 (number), making the behavior clearer. So how about adding support for array paths first? |
Issue Description:
Currently, the set function does not correctly handle number keys, treating them incorrectly as array indices rather than valid object keys. This causes issues when a path with a number index is provided. The logic needs to be updated to treat number keys as valid keys for objects.
AS-IS
TO-BE
Tasks:
The text was updated successfully, but these errors were encountered: