diff --git a/src/lib/__tests__/utils.ts b/src/lib/__tests__/utils.ts index 9ddfe81..9082f03 100644 --- a/src/lib/__tests__/utils.ts +++ b/src/lib/__tests__/utils.ts @@ -37,6 +37,14 @@ describe('utils', () => { const result = createOrApplyPath(null, ['some', 'path'], 'new value') expect(result).toEqual(null) }) + it('should handle null of key inside object', () => { + const result = createOrApplyPath( + { some: null }, + ['some', 'path'], + 'new value' + ) + expect(result).toEqual({ some: null }) + }) it('should map an object', () => { const object = { some: { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 0349301..8632a91 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -20,12 +20,14 @@ export const createOrApplyPath = ( while (thisPath.length > 1) { const [head, ...tail] = thisPath thisPath = tail - if (current[head] === undefined) { + if (current?.[head] === undefined) { current[head] = {} } current = current[head] } - current[thisPath[0]] = value + if (current) { + current[thisPath[0]] = value + } return obj }