Skip to content

Commit

Permalink
Merge pull request #14 from HuehueJS/issue-#13
Browse files Browse the repository at this point in the history
Issue #13
  • Loading branch information
rafaelim authored Nov 21, 2018
2 parents 30dde78 + 2b269f7 commit 3b3793d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@huehuejs/common-lang",
"private": false,
"version": "0.1.0",
"version": "0.1.2",
"description": "Exploring",
"main": "index.js",
"types": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const getattr = (obj: any, key: Key | KeyPiece, defaultValue: any = undef
let currentValue = obj;
for (let k of (key as Key)) {
currentValue = currentValue[k];
if (currentValue === undefined) {
if (isNullOrUndefined(currentValue)) {
return defaultValue;
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/getattr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe('#getattr', () => {
let obj = { 'key': 1 }
expect(getattr(obj, 'wrong_key', 'default')).to.equal('default')
})
it('when key is an array and doesn\'t exists should return the default value', () => {
let obj = { 'key': 1 }
expect(getattr(obj, ['wrong_key','another_key'], 'default')).to.equal('default')
})
it('when obj is null should return the default value', () => {
let obj = null;
expect(getattr(obj, 'key', 'default')).to.equal('default')
Expand Down

0 comments on commit 3b3793d

Please sign in to comment.