diff
diff copied to clipboard
Different classes
Hi,
deepDiff.diff({}, []) shows difference but this doesn't:
function A() {}
var a = new A();
var b = {};
deepDiff.diff(a, b)
I think it would be reasonable to consider two objects of different classes as different - just as we consider [] to be different from {}.
Is it a bug, negligence or deliberate move?
Probably negligence, although I do note in the readme that I was most interested in structural differences and class is usually associated with behavior and not structure. Nonetheless, we could add a new kind C indicating class difference:
kind- indicates the kind of change; will be one of the following:C- indicates a difference in the object's class (as indicated by.constructor.name)N- indicates a newly added property/elementD- indicates a property/element was deletedE- indicates a property/element was editedA- indicates a change occurred within an array
path- the property path (from the left-hand-side root)lhs- the value on the left-hand-side of the comparison (undefined if kind === 'N')rhs- the value on the right-hand-side of the comparison (undefined if kind === 'D')index- when kind === 'A', indicates the array index where the change occurreditem- when kind === 'A', contains a nested change record indicating the change that occurred at the array index
Such a change would be a major version change due to the potential of breaking code that expects only the previously defined kinds.
I'd like to see some votes on this before implementing.
For me it looks great. Although it can make a confusion when comparing array to object, because it now shows "E" difference kind and technically what's different is the class, so "C" would also be expected.
The other way to go is to treat arrays like objects and don't care about class at all (maybe only for functions). These would return no difference: deepDiff.diff({}, []), deepDiff.diff({"0": "foo"}, ["foo"])
Both ways have pluses and minuses. You should decide yourself what to choose.
I am voting on this although I am less interested in the difference in classes than in properties (for data comparison).
If data comparison gets slowed down by class comparison, then perhaps such a change could be an extension to the project?