Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix del for null/undefined props #80

Merged
merged 1 commit into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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