Skip to content

Commit

Permalink
Fix 'has' not handling deep null/undefined values (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariocasciaro committed Sep 1, 2016
1 parent e75f78b commit edfaa66
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
26 changes: 14 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
})(this, function(){
'use strict';

var
toStr = Object.prototype.toString,
_hasOwnProperty = Object.prototype.hasOwnProperty;
var toStr = Object.prototype.toString;
function hasOwnProperty(obj, prop) {
if(obj == null) {
return false
}
//to handle objects with null prototypes (too edge case?)
return Object.prototype.hasOwnProperty.call(obj, prop)
}

function isEmpty(value){
if (!value) {
Expand All @@ -26,7 +31,7 @@
return true;
} else if (typeof value !== 'string') {
for (var i in value) {
if (_hasOwnProperty.call(value, i)) {
if (hasOwnProperty(value, i)) {
return false;
}
}
Expand Down Expand Up @@ -79,7 +84,7 @@
};

function getShallowProperty(obj, prop) {
if (options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || _hasOwnProperty.call(obj, prop)) {
if (options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop)) {
return obj[prop];
}
}
Expand Down Expand Up @@ -116,24 +121,21 @@
}

objectPath.has = function (obj, path) {
if (obj == null) {
return false;
}

if (typeof path === 'number') {
path = [path];
} else if (typeof path === 'string') {
path = path.split('.');
}

if (!path || path.length === 0) {
return false;
return !!obj;
}

for (var i = 0; i < path.length; i++) {
var j = getKey(path[i]);

if((typeof j === 'number' && isArray(obj) && j < obj.length) ||
(options.includeInheritedProps ? (j in Object(obj)) : _hasOwnProperty.call(obj, j))) {
(options.includeInheritedProps ? (j in Object(obj)) : hasOwnProperty(obj, j))) {
obj = obj[j];
} else {
return false;
Expand Down Expand Up @@ -184,7 +186,7 @@
value.length = 0;
} else if (isObject(value)) {
for (i in value) {
if (_hasOwnProperty.call(value, i)) {
if (hasOwnProperty(value, i)) {
delete value[i];
}
}
Expand Down
20 changes: 18 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,16 @@ describe('has', function () {
expect(objectPath.has({}, 'a')).to.be.equal(false);
});

it('should return false for empty path', function () {
it('should handle empty paths properly', function () {
var obj = getTestObj();
expect(objectPath.has(obj, '')).to.be.equal(false);
expect(objectPath.has(obj, [])).to.be.equal(false);
expect(objectPath.has(obj, [''])).to.be.equal(false);
obj[''] = 1
expect(objectPath.has(obj, '')).to.be.equal(true);
expect(objectPath.has(obj, [''])).to.be.equal(true);

expect(objectPath.has(obj, [])).to.be.equal(true);
expect(objectPath.has(null, [])).to.be.equal(false);
});

it('should test under shallow object', function() {
Expand Down Expand Up @@ -614,6 +619,17 @@ describe('has', function () {
obj.key = undefined;
expect(objectPath.has(obj, 'key')).to.be.equal(true);
});

it('should work with deep undefined/null values', function() {
var obj = {};
expect(objectPath.has(obj, 'missing.test')).to.be.equal(false);

obj.missing = null;
expect(objectPath.has(obj, 'missing.test')).to.be.equal(false);

obj.sparseArray = [1, undefined, 3]
expect(objectPath.has(obj, 'sparseArray.1.test')).to.be.equal(false);
});
});


Expand Down

0 comments on commit edfaa66

Please sign in to comment.