hughsk / shallow-equals

Determine if an array or object is equivalent with another, *not* recursively
MIT License
26 stars 8 forks source link

Inconsistent handling of undefined #5

Open kevinoid opened 8 years ago

kevinoid commented 8 years ago

shallow-equals currently considers undefined properties to be equal to properties which are not present, with the additional restriction that the number of own properties must be equal. This is somewhat counter-intuitive for comparisons such as the following:

> shallowEquals({a: undefined}, {b:undefined})
true
> shallowEquals({a: undefined, b:undefined}, {b:undefined})
false
> shallowEquals({a: undefined}, {a:undefined, b:undefined})
false

Where copying the properties between two objects which compare equal causes them to become unequal.

An easy fix would be requiring b.hasOwnProperty after a.hasOwnProperty (which would also make the comparison symmetric when the inherited value of one object is the same as the own value of the other).

Another fix, which would allow less strict comparisons, would be to add additional ownProperty arguments to the comparison function so the caller can decide if it is considered equal. This is more difficult to implement, since the traversal of b would require value checking, but it would be more flexible.

Either works for me.

sktguha commented 7 years ago

:+1: