Skip to content

Commit dcf51d3

Browse files
committed
assert,util: handle invalid dates as equal in deep comparison
Invalid dates are now handled as equal in all deep comparisons.
1 parent a4f556f commit dcf51d3

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

benchmark/assert/deepequal-prims-and-objs-big-loop.js

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const primValues = {
2222
'empty_object': {},
2323
'regexp': /abc/i,
2424
'date': new Date(),
25+
'invalidDate': new Date('foo'),
2526
};
2627

2728
const primValues2 = {
@@ -33,6 +34,7 @@ const primValues2 = {
3334
'empty_object': {},
3435
'regexp': /abc/i,
3536
'date': new Date(primValues.date),
37+
'invalidDate': new Date('foo'),
3638
};
3739

3840
const primValuesUnequal = {
@@ -47,6 +49,7 @@ const primValuesUnequal = {
4749
'empty_object': [],
4850
'regexp': /abc/g,
4951
'date': new Date(primValues.date.getTime() + 1),
52+
'invalidDate': new Date(),
5053
};
5154

5255
const bench = common.createBenchmark(main, {

doc/api/assert.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ An alias of [`assert.ok()`][].
556556
<!-- YAML
557557
added: v0.1.21
558558
changes:
559+
- version: REPLACEME
560+
pr-url: https://github.com/nodejs/node/pull/XXXXXX
561+
description: Invalid dates are now considered equal.
559562
- version:
560563
- v22.2.0
561564
- v20.15.0
@@ -743,6 +746,9 @@ parameter is an instance of {Error} then it will be thrown instead of the
743746
<!-- YAML
744747
added: v1.2.0
745748
changes:
749+
- version: REPLACEME
750+
pr-url: https://github.com/nodejs/node/pull/XXXXXX
751+
description: Invalid dates are now considered equal.
746752
- version:
747753
- v22.2.0
748754
- v20.15.0
@@ -2595,9 +2601,12 @@ added:
25952601
- v23.4.0
25962602
- v22.13.0
25972603
changes:
2598-
- version: REPLACEME
2599-
pr-url: https://github.com/nodejs/node/pull/57370
2600-
description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
2604+
- version: REPLACEME
2605+
pr-url: https://github.com/nodejs/node/pull/XXXXXX
2606+
description: Invalid dates are now considered equal.
2607+
- version: REPLACEME
2608+
pr-url: https://github.com/nodejs/node/pull/57370
2609+
description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
26012610
-->
26022611

26032612
> Stability: 2 - Stable

lib/internal/util/comparisons.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,14 @@ function innerDeepEqual(val1, val2, mode, memos) {
230230
} else if (val1Tag === '[object Object]') {
231231
return keyCheck(val1, val2, mode, memos, kNoIterator);
232232
} else if (isDate(val1)) {
233-
if (!isDate(val2) ||
234-
DatePrototypeGetTime(val1) !== DatePrototypeGetTime(val2)) {
233+
if (!isDate(val2)) {
235234
return false;
236235
}
236+
const time1 = DatePrototypeGetTime(val1);
237+
const time2 = DatePrototypeGetTime(val2);
238+
if (time1 !== time2) {
239+
return NumberIsNaN(time1) && NumberIsNaN(time2);
240+
}
237241
} else if (isRegExp(val1)) {
238242
if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
239243
return false;

test/parallel/test-assert-deep.js

+2
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,8 @@ test('Additional tests', () => {
740740

741741
assertNotDeepOrStrict(new Date(), new Date(2000, 3, 14));
742742

743+
assertDeepAndStrictEqual(new Date('foo'), new Date('bar'));
744+
743745
assertDeepAndStrictEqual(/a/, /a/);
744746
assertDeepAndStrictEqual(/a/g, /a/g);
745747
assertDeepAndStrictEqual(/a/i, /a/i);

0 commit comments

Comments
 (0)