Alexander Mills
Alexander Mills

Reputation: 100070

JavaScript or NPM module that checks to see if two object's properties clash/collide

I often times use _.extend or _.defaults (the Underscore library) with two objects to do JS mixins. But sometimes I fear property collision on JS objects. Is there some module I can use to check this at runtime?

Upvotes: 0

Views: 53

Answers (1)

chrisbajorin
chrisbajorin

Reputation: 6153

I don't know about a module, but it's pretty quick to write yourself. Since you mentioned underscore:

function objectsCollide(objA, objB) {

    var keysA = _.allKeys(objA);
    var keysB = _.allKeys(objB);
    return _.intersection(keysA, keysB).length ? true : false;
}

Upvotes: 1

Related Questions