Skip to content

Commit

Permalink
fix(utils/get): handle case when nested value is falsy (bootstrap-vue…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfunkhouser authored and tmorehouse committed Apr 2, 2019
1 parent 7be99a8 commit 40f6cb7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const get = (obj, path, defaultValue = null) => {
}

// Traverse path in object to find result
return steps.every(step => isObject(obj) && obj.hasOwnProperty(step) && (obj = obj[step]))
return steps.every(step => isObject(obj) && obj.hasOwnProperty(step) && (obj = obj[step]) != null)
? obj
: defaultValue
}
Expand Down
9 changes: 9 additions & 0 deletions src/utils/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ describe('get', () => {
expect(get({}, [], 0)).toBe(0)
expect(get({ a: 'b' }, 'b', {})).toEqual({})
expect(get({ a: { c: 'd' } }, 'a.d', [])).toEqual([])
expect(get({ a: { c: undefined } }, 'a.c')).toBe(null)
expect(get({ a: 0, b: false }, 'c')).toBe(null)
})

it('returns expected value', async () => {
const obj1 = { a: 'b' }
const obj2 = { a: { b: { c: { d: 'e' } } } }
const obj3 = { a: [{ b: 'c' }] }
const obj4 = { a: [[{ b: 'c' }], [{ d: { e: ['f'] } }]] }
const obj5 = { a: { b: 0, c: '', d: false } }
const obj6 = { a: 0, b: false }

expect(get(obj1, 'a')).toBe('b')
expect(get(obj1, ['a'])).toBe('b')
Expand All @@ -36,6 +40,11 @@ describe('get', () => {
expect(get(obj4, 'a[1][0].d.e[0]')).toBe('f')
expect(get(obj4, ['a', 1, 0, 'd', 'e', 0])).toBe('f')
expect(get(obj4, ['a[1]', 0, 'd', 'e[0]'])).toBe('f')
expect(get(obj5, 'a.b')).toBe(0)
expect(get(obj5, 'a.c')).toBe('')
expect(get(obj5, 'a.d')).toBe(false)
expect(get(obj6, 'a')).toBe(0)
expect(get(obj6, 'b')).toBe(false)
})

it('handles when field name has dot', async () => {
Expand Down

0 comments on commit 40f6cb7

Please sign in to comment.