From b7204156beb8e9720e64287f0ef6699f141afd41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ttore=20Leandro=20Tognoli?= Date: Mon, 19 Nov 2018 13:20:02 -0200 Subject: [PATCH 1/3] fix --- src/index.ts | 2 +- test/getattr.spec.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 2f8eb87..2417990 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; } } diff --git a/test/getattr.spec.ts b/test/getattr.spec.ts index a303c3b..a6b00e2 100644 --- a/test/getattr.spec.ts +++ b/test/getattr.spec.ts @@ -34,6 +34,10 @@ describe('#getattr', function () { 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', function () { + 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', function () { let obj = null; expect(getattr(obj, 'key', 'default')).to.equal('default') From 680d8477152f48d2ee92eec414d0731a14eecfea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ttore=20Leandro=20Tognoli?= Date: Mon, 19 Nov 2018 13:22:28 -0200 Subject: [PATCH 2/3] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4c2909a..ce23b45 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@huehuejs/common-lang", "private": false, - "version": "0.1.0", + "version": "0.1.1", "description": "Exploring", "main": "index.js", "types": "index.d.ts", From 2b269f7f6e66ea543dc8d4c0fd3196ec3f50b379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ttore=20Leandro=20Tognoli?= Date: Mon, 19 Nov 2018 13:22:56 -0200 Subject: [PATCH 3/3] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ce23b45..19088e2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@huehuejs/common-lang", "private": false, - "version": "0.1.1", + "version": "0.1.2", "description": "Exploring", "main": "index.js", "types": "index.d.ts",