object-path icon indicating copy to clipboard operation
object-path copied to clipboard

Inconsistent behaviour in empty method.

Open xgbuils opened this issue 9 years ago • 1 comments

I read about this test and don't understand why exist.

it('should ignore invalid arguments safely', function(){
    var obj = {};
    // 1
    expect(objectPath.empty()).to.equal(void 0);
    // 2
    expect(objectPath.empty(obj, 'path')).to.equal(void 0);
    // 3
    expect(objectPath.empty(obj, '')).to.equal(obj);

    obj.path = true;
    // 4
    expect(objectPath.empty(obj, 'inexistant')).to.equal(obj);
});

2nd an 4th assertions are essentialy the same thing. 
In second assertion `obj` is an object and `path` property does not exist in `obj`.
In fourth assertion `obj` is an object and `existant` property does not exist in `obj`

I think that it should return the same value `undefined` or the same `object` passed.

xgbuils avatar Dec 14 '15 22:12 xgbuils

2: when the source object is empty, it returns undefined (void 0). https://github.com/mariocasciaro/object-path/blob/master/index.js#L198 4: the object isn't empty (obj is {path: true}), so it returns itself because the path doesn't exist https://github.com/mariocasciaro/object-path/blob/master/index.js#L203

This behavior will change and be more consistent in the upcoming (major breaking change) 1.0 that is almost done. The reason we can't return the object itself when the object is empty is because in some instances it can be considered truthy (typeof {} !== 'undefined').

pocesar avatar Dec 15 '15 09:12 pocesar