Skip to content

Commit

Permalink
fix(utils): support numerical keys in walkSet
Browse files Browse the repository at this point in the history
Fixes #195
  • Loading branch information
posva committed Jul 1, 2018
1 parent 66ccd52 commit b8fc424
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ export function walkSet (obj, path, value) {
const target = keys.reduce((target, key) => target[key], obj)
// global isFinite is different from Number.isFinite
// it converts values to numbers
if (isFinite(key)) target.splice(key, 1, value)
if (isFinite(key) && Array.isArray(target)) target.splice(key, 1, value)
else target[key] = value
}
10 changes: 9 additions & 1 deletion test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSnapshot, extractRefs } from '../src/utils'
import { createSnapshot, extractRefs, walkSet } from '../src/utils'
import { Key, db, _id, DocumentReference, GeoPoint, DocumentSnapshot, Timestamp } from './helpers'

let id, doc, snapshot, collection, docRef
Expand Down Expand Up @@ -139,3 +139,11 @@ test('extracts refs from array', async () => {
'arr.2': docRef
})
})

test('supports numbers as keys in objects', () => {
const obj = {
'0': 'foo'
}
expect(() => walkSet(obj, '0', 'bar')).not.toThrow()
expect(obj['0']).toBe('bar')
})

0 comments on commit b8fc424

Please sign in to comment.