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

Support array index access #71

Closed
saibotsivad opened this issue Oct 15, 2020 · 4 comments · Fixed by #82
Closed

Support array index access #71

saibotsivad opened this issue Oct 15, 2020 · 4 comments · Fixed by #82

Comments

@saibotsivad
Copy link

I ran into this issue when upgrading from 4->6, which of course is a breaking change, but I think the new behavior is inconsistent. 🙇

Consider some objects that have arrays, like:

const emptyArray = { foo: [] }
const oneItem = { foo: [ { fizz: 'buzz' } ] }

I want to access properties on the array's objects, and provide a default value if it doesn't exist.

// passes: default value is found
t.is(dotProp.get(emptyArray, 'foo.0.fizz', 'bazz'), 'bazz')
// passes: existing value is found
t.is(dotProp.get(oneItem, 'foo.0.fizz', 'bazz'), 'buzz')

However, in 4->6 the behavior changed when accessing an array index that doesn't exist:

// passes in 4 but fails in 6
t.is(dotProp.get({ foo: [ 'bar' ] }, 'foo.1', 'bazz'), 'bazz')
// passes in 6 but fails in 4
t.is(dotProp.get({ foo: [ 'bar' ] }, 'foo.1', 'bazz'), undefined)

I think that this behavior is ... inconsistent, probably?

If accessing a deeper property in an array element triggers a default value fallback, shouldn't accessing an array element trigger a fallback if that index doesn't exist?

@saibotsivad
Copy link
Author

For now I've had to update a few points in the code to set a default value manually, e.g. something like this:

// previously
const myValue = dotProp.get(data, 'foo.bar.3', defaultValue)
// after 4->6
let myValue = dotProp.get(data, 'foo.bar.3')
if (myValue === undefined) {
    myValue = defaultValue
}

@sindresorhus
Copy link
Owner

It's not even a breaking change as the behavior was never documented or officially supported. It only worked as a side-effect of the fact that arrays in JS are just objects.

That being said, the behavior you describe does make sense. However, this is a new feature and will need lots of tests and docs.

@sindresorhus sindresorhus changed the title What should default value for 'get' from array be? Support array index access Oct 18, 2020
@johansteffner
Copy link
Contributor

The expected behaviour here seems to have been fixed by #75.

@sindresorhus
Copy link
Owner

@johansteffner

That being said, the behavior you describe does make sense. However, this is a new feature and will need lots of tests and docs.

As long as it's not documented and tested, it could break in any patch release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants