object-path
Advanced tools
Comparing version 0.11.3 to 0.11.4
17
index.js
@@ -86,4 +86,8 @@ (function (root, factory){ | ||
function hasShallowProperty(obj, prop) { | ||
return (options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop)) | ||
} | ||
function getShallowProperty(obj, prop) { | ||
if (options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop)) { | ||
if (hasShallowProperty(obj, prop)) { | ||
return obj[prop]; | ||
@@ -190,3 +194,3 @@ } | ||
for (i in value) { | ||
if (hasOwnProperty(value, i)) { | ||
if (hasShallowProperty(value, i)) { | ||
delete value[i]; | ||
@@ -266,5 +270,4 @@ } | ||
var currentPath = getKey(path[0]); | ||
var currentVal = getShallowProperty(obj, currentPath); | ||
if(currentVal == null) { | ||
return currentVal; | ||
if (!hasShallowProperty(obj, currentPath)) { | ||
return obj; | ||
} | ||
@@ -279,5 +282,3 @@ | ||
} else { | ||
if (obj[currentPath] !== void 0) { | ||
return objectPath.del(obj[currentPath], path.slice(1)); | ||
} | ||
return objectPath.del(obj[currentPath], path.slice(1)); | ||
} | ||
@@ -284,0 +285,0 @@ |
{ | ||
"name": "object-path", | ||
"description": "Access deep object properties using a path", | ||
"version": "0.11.3", | ||
"version": "0.11.4", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Mario Casciaro" |
@@ -46,3 +46,3 @@ | ||
``` | ||
tsd query object-path --action install --save | ||
typings install --save dt~object-path | ||
``` | ||
@@ -49,0 +49,0 @@ |
36
test.js
@@ -388,2 +388,5 @@ 'use strict'; | ||
expect(objectPath.empty(obj, 'inexistant')).to.equal(void 0); | ||
expect(objectPath.empty(null, 'path')).to.equal(void 0); | ||
expect(objectPath.empty(void 0, 'path')).to.equal(void 0); | ||
}); | ||
@@ -411,3 +414,5 @@ | ||
'property': true | ||
} | ||
}, | ||
nullProp: null, | ||
undefinedProp: void 0 | ||
}, | ||
@@ -426,2 +431,13 @@ instance: new Instance() | ||
objectPath.empty(obj, 'object.nullProp'); | ||
expect(obj.object.nullProp).to.equal(null); | ||
objectPath.empty(obj, 'object.undefinedProp'); | ||
expect(obj.object.undefinedProp).to.equal(void 0); | ||
expect(obj.object).to.have.property('undefinedProp'); | ||
objectPath.empty(obj, 'object.notAProp'); | ||
expect(obj.object.notAProp).to.equal(void 0); | ||
expect(obj.object).to.not.have.property('notAProp'); | ||
objectPath.empty(obj, 'instance.test'); | ||
@@ -458,2 +474,20 @@ //instance.test is not own property, so it shouldn't be emptied | ||
it('should remove null and undefined props (but not explode on nested)', function(){ | ||
var obj = { nullProp: null, undefinedProp: void 0 }; | ||
expect(obj).to.have.property('nullProp'); | ||
expect(obj).to.have.property('undefinedProp'); | ||
objectPath.del(obj, 'nullProp.foo'); | ||
objectPath.del(obj, 'undefinedProp.bar'); | ||
expect(obj).to.have.property('nullProp'); | ||
expect(obj).to.have.property('undefinedProp'); | ||
expect(obj).to.deep.equal({ nullProp: null, undefinedProp: void 0 }); | ||
objectPath.del(obj, 'nullProp'); | ||
objectPath.del(obj, 'undefinedProp'); | ||
expect(obj).to.not.have.property('nullProp'); | ||
expect(obj).to.not.have.property('undefinedProp'); | ||
expect(obj).to.deep.equal({}); | ||
}); | ||
it('should delete deep paths', function(){ | ||
@@ -460,0 +494,0 @@ var obj = getTestObj(); |
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
46045
1119