Skip to content

Commit 80879d1

Browse files
committed
fix: resilient walkSet and walkGet
1 parent bca65db commit 80879d1

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/shared.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export type TODO = any
5252
* @param path
5353
*/
5454
export function walkGet(obj: Record<string, any>, path: string): any {
55-
return path.split('.').reduce((target, key) => target[key], obj)
55+
return path.split('.').reduce((target, key) => target && target[key], obj)
5656
}
5757

5858
/**
@@ -75,10 +75,12 @@ export function walkSet<T extends object = Record<any, unknown>>(
7575
const target: any = keys.reduce(
7676
(target, key) =>
7777
// @ts-expect-error:
78-
target[key],
78+
target && target[key],
7979
obj
8080
)
8181

82+
if (target == null) return
83+
8284
return Array.isArray(target)
8385
? target.splice(Number(key), 1, value)
8486
: (target[key] = value)

0 commit comments

Comments
 (0)