Skip to content

Support array index access #71

@saibotsivad

Description

@saibotsivad

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?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions