object-path
Advanced tools
Comparing version 0.11.1 to 0.11.2
26
index.js
@@ -17,5 +17,10 @@ (function (root, factory){ | ||
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) | ||
} | ||
@@ -30,3 +35,3 @@ function isEmpty(value){ | ||
for (var i in value) { | ||
if (_hasOwnProperty.call(value, i)) { | ||
if (hasOwnProperty(value, i)) { | ||
return false; | ||
@@ -84,3 +89,3 @@ } | ||
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]; | ||
@@ -122,6 +127,2 @@ } | ||
objectPath.has = function (obj, path) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (typeof path === 'number') { | ||
@@ -134,3 +135,3 @@ path = [path]; | ||
if (!path || path.length === 0) { | ||
return false; | ||
return !!obj; | ||
} | ||
@@ -140,4 +141,5 @@ | ||
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]; | ||
@@ -193,3 +195,3 @@ } else { | ||
for (i in value) { | ||
if (_hasOwnProperty.call(value, i)) { | ||
if (hasOwnProperty(value, i)) { | ||
delete value[i]; | ||
@@ -196,0 +198,0 @@ } |
{ | ||
"name": "object-path", | ||
"description": "Access deep object properties using a path", | ||
"version": "0.11.1", | ||
"version": "0.11.2", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Mario Casciaro" |
20
test.js
@@ -553,7 +553,12 @@ 'use strict'; | ||
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); | ||
}); | ||
@@ -618,2 +623,13 @@ | ||
}); | ||
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); | ||
}); | ||
}); | ||
@@ -620,0 +636,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44730
1092