Skip to content

Commit

Permalink
Merge pull request #80 from Losant/master
Browse files Browse the repository at this point in the history
Fix del for null/undefined props
  • Loading branch information
mariocasciaro authored Feb 27, 2017
2 parents 098d48b + 5df3264 commit 4fc16a0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@
}, {});
};

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];
}
}
Expand Down Expand Up @@ -186,7 +190,7 @@
value.length = 0;
} else if (isObject(value)) {
for (i in value) {
if (hasOwnProperty(value, i)) {
if (hasShallowProperty(value, i)) {
delete value[i];
}
}
Expand Down Expand Up @@ -261,9 +265,8 @@
}

var currentPath = getKey(path[0]);
var currentVal = getShallowProperty(obj, currentPath);
if(currentVal == null) {
return currentVal;
if (!hasShallowProperty(obj, currentPath)) {
return obj;
}

if(path.length === 1) {
Expand All @@ -273,9 +276,7 @@
delete obj[currentPath];
}
} else {
if (obj[currentPath] !== void 0) {
return objectPath.del(obj[currentPath], path.slice(1));
}
return objectPath.del(obj[currentPath], path.slice(1));
}

return obj;
Expand Down
36 changes: 35 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ describe('empty', function(){
obj.path = true;

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);
});

it('should empty each path according to their types', function(){
Expand All @@ -407,7 +410,9 @@ describe('empty', function(){
some:'property',
sub: {
'property': true
}
},
nullProp: null,
undefinedProp: void 0
},
instance: new Instance()
};
Expand All @@ -421,6 +426,17 @@ describe('empty', function(){
objectPath.empty(obj, 'object.sub');
expect(obj.object.sub).to.deep.equal({});

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');
//instance.test is not own property, so it shouldn't be emptied
expect(obj.instance.test).to.be.a('function');
Expand Down Expand Up @@ -452,6 +468,24 @@ describe('del', function(){
expect(obj.b.d).to.deep.equal(['a']);
});

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(){
var obj = getTestObj();

Expand Down

0 comments on commit 4fc16a0

Please sign in to comment.