mattphillips / deep-object-diff

Deep diffs two objects, including nested structures of arrays and objects, and returns the difference. ❄️
https://www.npmjs.com/package/deep-object-diff
MIT License
1.05k stars 89 forks source link

Comparing objects with empty prototypes (no hasOwnProperty method) #7

Closed denisnd closed 7 years ago

denisnd commented 7 years ago

I'm using deep-object-diff to compare objects generated by query-string. Structures returned by its parser have no hasOwnProperty method (https://github.com/sindresorhus/query-string/issues/47) and so deep-object-diff can't work on them as it relies on the method.

Steps to reproduce:

var deepObjectDiff = require('deep-object-diff');

var a = Object.create(null);
a.foo = 'bar;

var b = Object.create(null);
b.foo = 'baz';

console.log( deepObjectDiff.diff(a, b) );

Error message:

TypeError: rhs.hasOwnProperty is not a function
    at C:\Users\nd\object-diff\node_modules\deep-object-diff\dist\diff\index.js:55:18
mattphillips commented 7 years ago

@denisnd Hey thanks for raising the issue! I've just published a fix in version 1.0.3 so if you try the same code again all should be good :smile:.

It will treat the object as a normal JS object that has the .hasOwnProperty function available, this means that it will work on nested structures that are missing this property at any level.

var deepObjectDiff = require('deep-object-diff');

var a = Object.create(null);
a.foo = 'bar';

var b = Object.create(null);
b.foo = 'baz';

console.log( deepObjectDiff.diff(a, b) ); // { foo: 'baz' }
denisnd commented 7 years ago

@mattphillips Thank you for quick fixing! Now everything is working fine.