XCMP: eXtremely accurate object Comparison!
Ever tried to compare two JS variables for equality? Something like this:
var apples = 'apple' 'avocado';var oranges = 'lemon';if apples === oranges console;else console
See? It's just as simple as using a ===
between the variables we want to compare!
But wait.
What about this one?
var applesHere = 'apple' 'apple';var applesThere = 'apple' 'apple'; if applesHere === applesThere ;else ;
In the rather sad example above, applesHere
and applesThere
are referentially distinct (i.e., they refer to different memory addresses), but structurally equivalent, that is, they have the exact same shape and content; they would have been equal if they were compared the right way.
The moral of the snippet above, of course, is that using ===
indiscriminately can have really tragic consequences. :(
Had we used the structural-comparison-superpowers of xcmp
, though, our little blue mudball of a planet might have survived unannihilated:
npm install xcmp
var xcmp = ; var applesHere = 'apple' 'apple';var applesThere = 'apple' 'apple'; if xcmp ;else ;
It's not even that! xcmp
can handle even more complex cases like this:
var xcmp = ; var a = ; a; var b = ; b; console; // True!
... or this:
var xcmp = ; var a = ; a; var b = ; b; console; // True!
... or even this: (on which even node
's very own assert.deepEqual
fails!)
var xcmp = ; var a = ; a; var b = ; b; console; // True!
... And don't worry, a typo in the call to the annihilation function, above, resulted in a run-time error before the annihilation was launched.
But please be very careful next time! :)
Advanced Usage
Even though it is perfectly fine to call xcmpEqual
as xcmpEqual(someObj, someOtherObj)
, xcmp
has quite more to provide -- it can receive extra parameters that can, to a reasonable extent, modify the way xcmp works.
The full signature of xcmp.xcmpEqual
is as follows:
(N.B. none of the parameters below are mandatory)