Skip to content

Commit b8fc424

Browse files
committed
fix(utils): support numerical keys in walkSet
Fixes #195
1 parent 66ccd52 commit b8fc424

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Diff for: src/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ export function walkSet (obj, path, value) {
7474
const target = keys.reduce((target, key) => target[key], obj)
7575
// global isFinite is different from Number.isFinite
7676
// it converts values to numbers
77-
if (isFinite(key)) target.splice(key, 1, value)
77+
if (isFinite(key) && Array.isArray(target)) target.splice(key, 1, value)
7878
else target[key] = value
7979
}

Diff for: test/utils.spec.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createSnapshot, extractRefs } from '../src/utils'
1+
import { createSnapshot, extractRefs, walkSet } from '../src/utils'
22
import { Key, db, _id, DocumentReference, GeoPoint, DocumentSnapshot, Timestamp } from './helpers'
33

44
let id, doc, snapshot, collection, docRef
@@ -139,3 +139,11 @@ test('extracts refs from array', async () => {
139139
'arr.2': docRef
140140
})
141141
})
142+
143+
test('supports numbers as keys in objects', () => {
144+
const obj = {
145+
'0': 'foo'
146+
}
147+
expect(() => walkSet(obj, '0', 'bar')).not.toThrow()
148+
expect(obj['0']).toBe('bar')
149+
})

0 commit comments

Comments
 (0)