Skip to content

Commit

Permalink
fix(null): handle more null cases
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Jul 14, 2021
1 parent 5cac562 commit 50feed4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/lib/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 50feed4

Please sign in to comment.