Skip to content

Commit 4fc16a0

Browse files
authoredFeb 27, 2017
Merge pull request #80 from Losant/master
Fix del for null/undefined props
2 parents 098d48b + 5df3264 commit 4fc16a0

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed
 

‎index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@
8383
}, {});
8484
};
8585

86+
function hasShallowProperty(obj, prop) {
87+
return (options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop))
88+
}
89+
8690
function getShallowProperty(obj, prop) {
87-
if (options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop)) {
91+
if (hasShallowProperty(obj, prop)) {
8892
return obj[prop];
8993
}
9094
}
@@ -186,7 +190,7 @@
186190
value.length = 0;
187191
} else if (isObject(value)) {
188192
for (i in value) {
189-
if (hasOwnProperty(value, i)) {
193+
if (hasShallowProperty(value, i)) {
190194
delete value[i];
191195
}
192196
}
@@ -261,9 +265,8 @@
261265
}
262266

263267
var currentPath = getKey(path[0]);
264-
var currentVal = getShallowProperty(obj, currentPath);
265-
if(currentVal == null) {
266-
return currentVal;
268+
if (!hasShallowProperty(obj, currentPath)) {
269+
return obj;
267270
}
268271

269272
if(path.length === 1) {
@@ -273,9 +276,7 @@
273276
delete obj[currentPath];
274277
}
275278
} else {
276-
if (obj[currentPath] !== void 0) {
277-
return objectPath.del(obj[currentPath], path.slice(1));
278-
}
279+
return objectPath.del(obj[currentPath], path.slice(1));
279280
}
280281

281282
return obj;

‎test.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ describe('empty', function(){
385385
obj.path = true;
386386

387387
expect(objectPath.empty(obj, 'inexistant')).to.equal(void 0);
388+
389+
expect(objectPath.empty(null, 'path')).to.equal(void 0);
390+
expect(objectPath.empty(void 0, 'path')).to.equal(void 0);
388391
});
389392

390393
it('should empty each path according to their types', function(){
@@ -407,7 +410,9 @@ describe('empty', function(){
407410
some:'property',
408411
sub: {
409412
'property': true
410-
}
413+
},
414+
nullProp: null,
415+
undefinedProp: void 0
411416
},
412417
instance: new Instance()
413418
};
@@ -421,6 +426,17 @@ describe('empty', function(){
421426
objectPath.empty(obj, 'object.sub');
422427
expect(obj.object.sub).to.deep.equal({});
423428

429+
objectPath.empty(obj, 'object.nullProp');
430+
expect(obj.object.nullProp).to.equal(null);
431+
432+
objectPath.empty(obj, 'object.undefinedProp');
433+
expect(obj.object.undefinedProp).to.equal(void 0);
434+
expect(obj.object).to.have.property('undefinedProp');
435+
436+
objectPath.empty(obj, 'object.notAProp');
437+
expect(obj.object.notAProp).to.equal(void 0);
438+
expect(obj.object).to.not.have.property('notAProp');
439+
424440
objectPath.empty(obj, 'instance.test');
425441
//instance.test is not own property, so it shouldn't be emptied
426442
expect(obj.instance.test).to.be.a('function');
@@ -452,6 +468,24 @@ describe('del', function(){
452468
expect(obj.b.d).to.deep.equal(['a']);
453469
});
454470

471+
it('should remove null and undefined props (but not explode on nested)', function(){
472+
var obj = { nullProp: null, undefinedProp: void 0 };
473+
expect(obj).to.have.property('nullProp');
474+
expect(obj).to.have.property('undefinedProp');
475+
476+
objectPath.del(obj, 'nullProp.foo');
477+
objectPath.del(obj, 'undefinedProp.bar');
478+
expect(obj).to.have.property('nullProp');
479+
expect(obj).to.have.property('undefinedProp');
480+
expect(obj).to.deep.equal({ nullProp: null, undefinedProp: void 0 });
481+
482+
objectPath.del(obj, 'nullProp');
483+
objectPath.del(obj, 'undefinedProp');
484+
expect(obj).to.not.have.property('nullProp');
485+
expect(obj).to.not.have.property('undefinedProp');
486+
expect(obj).to.deep.equal({});
487+
});
488+
455489
it('should delete deep paths', function(){
456490
var obj = getTestObj();
457491

0 commit comments

Comments
 (0)
Please sign in to comment.