diff icon indicating copy to clipboard operation
diff copied to clipboard

Different classes

Open kownacki opened this issue 9 years ago • 3 comments

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?

kownacki avatar Jun 27 '16 16:06 kownacki

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/element
    • D - indicates a property/element was deleted
    • E - indicates a property/element was edited
    • A - 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 occurred
  • item - 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.

flitbit avatar Jun 28 '16 21:06 flitbit

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.

kownacki avatar Jun 28 '16 23:06 kownacki

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?

jesperronn avatar Dec 21 '16 09:12 jesperronn