_.isEqual: Ensure that an egal comparison is performed for number objects and primitives.

This commit is contained in:
Kit Cambridge
2011-10-26 11:35:49 -06:00
parent ed3c05e74c
commit 3d1605f241
2 changed files with 9 additions and 5 deletions

View File

@@ -106,6 +106,8 @@ $(document).ready(function() {
ok(_.isEqual(new Number(75), new Number(75)), "Number objects with identical primitive values are equal");
ok(_.isEqual(75, new Number(75)), "Number primitives and their corresponding object wrappers are equal");
ok(_.isEqual(new Number(75), 75), "Commutative equality is implemented for number objects and primitives");
ok(!_.isEqual(new Number(0), -0), "`new Number(0)` and `-0` are not equal");
ok(!_.isEqual(0, new Number(-0)), "Commutative equality is implemented for `new Number(0)` and `-0`");
ok(!_.isEqual(new Number(75), new Number(63)), "Number objects with different primitive values are not equal");
ok(!_.isEqual(new Number(63), {valueOf: function(){ return 63; }}), "Number objects and objects with a `valueOf` method are not equal");

View File

@@ -684,14 +684,16 @@
// equivalent to `new String("5")`.
return String(a) == String(b);
case '[object Number]':
case '[object Boolean]':
// Coerce numbers, dates, and booleans to numeric primitive values.
a = +a;
b = +b;
// `NaN`s are equivalent, but non-reflexive.
return a != a ? b != b : a == b;
// `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
// other numeric values.
return a != a ? b != b : (a == 0 ? 1 / a == 1 / b : a == b);
case '[object Date]':
// Compare dates by their millisecond representations. Invalid dates are not equivalent.
case '[object Boolean]':
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
// millisecond representations. Note that invalid dates with millisecond representations
// of `NaN` are not equivalent.
return +a == +b;
// RegExps are compared by their source patterns and flags.
case '[object RegExp]':