Make _.isEqual treat invalid dates as equiv.

This commit is contained in:
John-David Dalton
2016-05-23 15:25:31 -07:00
parent 3fdac985c7
commit c67e347255
3 changed files with 7 additions and 9 deletions

View File

@@ -5168,18 +5168,14 @@
case boolTag: case boolTag:
case dateTag: case dateTag:
// Coerce dates and booleans to numbers, dates to milliseconds and case numberTag:
// booleans to `1` or `0` treating invalid dates coerced to `NaN` as // Coerce booleans to `1` or `0` and dates to milliseconds.
// not equal. // Invalid dates are coerced to `NaN`.
return +object == +other; return eq(+object, +other);
case errorTag: case errorTag:
return object.name == other.name && object.message == other.message; return object.name == other.name && object.message == other.message;
case numberTag:
// Treat `NaN` vs. `NaN` as equal.
return (object != +object) ? other != +other : object == +other;
case regexpTag: case regexpTag:
case stringTag: case stringTag:
// Coerce regexes to strings and treat strings, primitives and objects, // Coerce regexes to strings and treat strings, primitives and objects,

View File

@@ -9777,9 +9777,10 @@
var date = new Date(2012, 4, 23); var date = new Date(2012, 4, 23);
assert.strictEqual(_.isEqual(date, new Date(2012, 4, 23)), true); assert.strictEqual(_.isEqual(date, new Date(2012, 4, 23)), true);
assert.strictEqual(_.isEqual(new Date('a'), new Date('b')), true);
assert.strictEqual(_.isEqual(date, new Date(2013, 3, 25)), false); assert.strictEqual(_.isEqual(date, new Date(2013, 3, 25)), false);
assert.strictEqual(_.isEqual(date, { 'getTime': lodashStable.constant(+date) }), false); assert.strictEqual(_.isEqual(date, { 'getTime': lodashStable.constant(+date) }), false);
assert.strictEqual(_.isEqual(new Date('a'), new Date('a')), false);
}); });
QUnit.test('should compare error objects', function(assert) { QUnit.test('should compare error objects', function(assert) {

View File

@@ -285,6 +285,7 @@
'Commutative equality is implemented for `0` and `-0`', 'Commutative equality is implemented for `0` and `-0`',
'`new Number(0)` and `-0` are not equal', '`new Number(0)` and `-0` are not equal',
'Commutative equality is implemented for `new Number(0)` and `-0`', 'Commutative equality is implemented for `new Number(0)` and `-0`',
'Invalid dates are not equal',
'false' 'false'
], ],
'isFinite': [ 'isFinite': [