Skip to content

Commit

Permalink
Merge pull request #58 from Plloi/handle-nulls-bro
Browse files Browse the repository at this point in the history
Made changes to properly handle nulls in iCanHaz
  • Loading branch information
letsgetrandy committed Sep 6, 2016
2 parents cccbd6e + 991ea11 commit 11ef77e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
15 changes: 6 additions & 9 deletions brototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
callback(self.obj[key[i]], key[i]);
}
}

return Bro.TOTALLY;
} else {
optionsBro.iDontAlways('sorryBro').butWhenIdo();
Expand All @@ -92,12 +92,9 @@
}
var props = key.split('.'),
item = this.obj;
if (typeof item !== "undefined") {
for (var i = 0; i < props.length; i++) {
item = item[props[i]];
if (Bro(item).isThatEvenAThing() === Bro.NOWAY) {
return item;
}
for (var i = 0; i < props.length; i++) {
if (typeof item === "undefined" || item === null || Bro(item = item[props[i]]).isThatEvenAThing() === Bro.NOWAY) {
return undefined;
}
}
return item;
Expand Down Expand Up @@ -129,11 +126,11 @@
}
return props;
},

"hasRespect": function(prop) {
return this.obj.hasOwnProperty(prop);
},


"iDontAlways": function(methodString) {
var method = this.iCanHaz(methodString);
Expand Down
12 changes: 12 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ describe('Bro.doYouEven', function() {
assert.equal(bro.doYouEven('foo.bar'), false);
});

it('should fail gracefully if the object is null', function() {
var a = null,
bro = Bro(a);
assert.equal(bro.doYouEven('foo.bar'), false);
});

it('should fail gracefully if a traversed subproperty is null', function(){
var a = {test: null},
bro = Bro(a);
assert.equal(bro.doYouEven('test.0.test'), false);
});

it('should pass a simple callback function', function() {
var a = {foo: 'bar'},
bro = Bro(a);
Expand Down

0 comments on commit 11ef77e

Please sign in to comment.