Skip to content

Commit

Permalink
fix: resilient walkSet and walkGet
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Nov 21, 2022
1 parent bca65db commit 80879d1
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type TODO = any
* @param path
*/
export function walkGet(obj: Record<string, any>, path: string): any {
return path.split('.').reduce((target, key) => target[key], obj)
return path.split('.').reduce((target, key) => target && target[key], obj)
}

/**
Expand All @@ -75,10 +75,12 @@ export function walkSet<T extends object = Record<any, unknown>>(
const target: any = keys.reduce(
(target, key) =>
// @ts-expect-error:
target[key],
target && target[key],
obj
)

if (target == null) return

return Array.isArray(target)
? target.splice(Number(key), 1, value)
: (target[key] = value)
Expand Down

0 comments on commit 80879d1

Please sign in to comment.