_.isEqual: Ensure that 0 and -0 are not equivalent. NaN values should be equal.

This commit is contained in:
Kit Goncharov
2011-07-12 22:16:12 -06:00
parent 9d0b43221a
commit cf812e77bc
2 changed files with 7 additions and 7 deletions

View File

@@ -73,7 +73,7 @@ $(document).ready(function() {
ok(!_.isEqual(5, NaN), '5 is not equal to NaN');
ok(NaN != NaN, 'NaN is not equal to NaN (native equality)');
ok(NaN !== NaN, 'NaN is not equal to NaN (native identity)');
ok(!_.isEqual(NaN, NaN), 'NaN is not equal to NaN');
ok(_.isEqual(NaN, NaN), 'NaN is equal to NaN');
ok(_.isEqual(new Date(100), new Date(100)), 'identical dates are equal');
ok(_.isEqual((/hello/ig), (/hello/ig)), 'identical regexes are equal');
ok(!_.isEqual(null, [1]), 'a falsy is never equal to a truthy');

View File

@@ -596,7 +596,7 @@
// Internal recursive comparison function.
function eq(a, b, stack) {
// Identical objects are equal.
if (a === b) return true;
if (a === b) return a != 0 || 1 / a == 1 / b;
// A strict comparison is necessary because `null == undefined`.
if (a == null) return a === b;
// Compare object types.
@@ -606,8 +606,8 @@
if (a == b) return true;
// Ensure that both values are truthy or falsy.
if ((!a && b) || (a && !b)) return false;
// `NaN` values are toxic.
if (_.isNaN(a) || _.isNaN(b)) return false;
// `NaN` values are equal.
if (_.isNaN(a)) return _.isNaN(b);
if (_.isDate(a)) return _.isDate(b) && a.getTime() == b.getTime();
// Compare RegExps by their source patterns and flags.
if (_.isRegExp(a)) return _.isRegExp(b) && a.source == b.source &&
@@ -631,11 +631,11 @@
}
// Add the object to the stack of traversed objects.
stack.push(a);
var result = true;
// Deep comparse the contents.
// Deep compare the contents.
var aKeys = _.keys(a), bKeys = _.keys(b);
// Ensure that both objects contain the same number of properties.
if (result = aKeys.length == bKeys.length) {
var result = aKeys.length == bKeys.length;
if (result) {
// Recursively compare properties.
for (var key in a) {
if (!(result = key in b && eq(a[key], b[key], stack))) break;