Skip to content

Commit

Permalink
fix(util:deepGet): fix losing 0 value (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Mar 20, 2019
1 parent f491b4d commit bf37459
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/util/src/other/other.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ describe('abc: utils', () => {
it('should be return default value when paths include null value', () => {
expect(deepGet({ res: {} }, 'res.address.city')).toBeUndefined();
});
it('should be get zero value', () => {
const res = deepGet({ res: { zero: 0 } }, 'res.zero');
expect(res).toBe(0);
});
});

it('#deepCopy', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/util/src/other/other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export function deepGet(obj: any, path: string | string[], defaultValue?: any):
const checkObj = obj[path[0]];
return typeof checkObj === 'undefined' ? defaultValue : checkObj;
}
return path.reduce((o, k) => (o || {})[k], obj) || defaultValue;
const res = path.reduce((o, k) => (o || {})[k], obj);
return typeof res === 'undefined' ? defaultValue : res;
}

export function deepCopy(obj: any): any {
Expand Down

0 comments on commit bf37459

Please sign in to comment.