From a45c4a79827ef8efbc76b75ce2b8057230fbdb73 Mon Sep 17 00:00:00 2001 From: git-admin Date: Thu, 5 Mar 2015 16:13:56 +0100 Subject: [PATCH] Fix picking a null property with a dotted value. --- index.js | 2 +- test/pick.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 99cfd53..d931569 100644 --- a/index.js +++ b/index.js @@ -101,7 +101,7 @@ DotObject.prototype.pick = function(path, obj, remove) { if (path.indexOf(this.seperator) !== -1) { keys = path.split(this.seperator); for (i = 0; i < keys.length; i++) { - if (obj.hasOwnProperty(keys[i])) { + if (obj && obj.hasOwnProperty(keys[i])) { if (i === (keys.length - 1)) { if (remove) { val = obj[keys[i]]; diff --git a/test/pick.js b/test/pick.js index d7f435c..fb775aa 100644 --- a/test/pick.js +++ b/test/pick.js @@ -36,4 +36,46 @@ describe('DJ value picker:', function() { }); + it('Should be able to pick null properties', function() { + + var dj = new DJ(); + + var obj = { + 'some': null + }; + + var val = dj.pick('some', obj); + + (val === null).should.be.true; + + }); + + it('Should return undefined when picking an non-existing value', function() { + + var dj = new DJ(); + + var obj = { + 'some': null + }; + + var val = dj.pick('other', obj); + + (val === undefined).should.be.true; + + }); + + it('Should return undefined when picking an non-existing dotted value', function() { + + var dj = new DJ(); + + var obj = { + 'some': null + }; + + var val = dj.pick('some.other', obj); + + (val === undefined).should.be.true; + + }); + });