Skip to content

Commit

Permalink
fix(midway-web): path might be numeric string within safelyGet()
Browse files Browse the repository at this point in the history
- safelyGet(['a','1'], {a: {"1": 2}})  // => 2
- safelyGet(['a','1'], {a: {b: 2}})  // => undefined
  • Loading branch information
waitingsong committed Jul 9, 2019
1 parent cb97b23 commit 5b48eff
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/midway-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export function isTypeScriptEnvironment(): boolean {
/**
* safelyGet(['a','b'],{a: {b: 2}}) // => 2
* safelyGet(['a','b'],{c: {b: 2}}) // => undefined
* safelyGet(['a','1'],{a: {"1": 2}}) // => 2
* safelyGet(['a','1'],{a: {b: 2}}) // => undefined
* safelyGet('a.b',{a: {b: 2}}) // => 2
* safelyGet('a.b',{c: {b: 2}}) // => undefined
*/
Expand All @@ -57,7 +59,7 @@ export function safelyGet(list: string | string[], obj?: object): any {
return (_obj: object) => safelyGet(list, _obj);
}

if (typeof obj === 'undefined' || obj === null) {
if (typeof obj === 'undefined' || typeof obj !== 'object' || obj === null) {
return void 0;
}
const pathArrValue = typeof list === 'string' ? list.split('.') : list;
Expand All @@ -67,6 +69,9 @@ export function safelyGet(list: string | string[], obj?: object): any {
if (typeof willReturn === 'undefined' || willReturn === null) {
return void 0;
}
else if (typeof willReturn !== 'object') { // for willReturn is string and key is numeric string
return void 0;
}
willReturn = willReturn[key];
}

Expand Down

0 comments on commit 5b48eff

Please sign in to comment.