Skip to content

Commit 0702320

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 7964eb3 commit 0702320

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

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

Lines changed: 3 additions & 0 deletions
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 = {
@@ -34,6 +35,7 @@ const primValues2 = {
3435
'empty_object': {},
3536
'regexp': /abc/i,
3637
'date': new Date(primValues.date),
38+
'invalidDate': new Date('foo'),
3739
};
3840

3941
const primValuesUnequal = {
@@ -49,6 +51,7 @@ const primValuesUnequal = {
4951
'empty_object': [],
5052
'regexp': /abc/g,
5153
'date': new Date(primValues.date.getTime() + 1),
54+
'invalidDate': new Date(),
5255
};
5356

5457
const bench = common.createBenchmark(main, {

doc/api/assert.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ An alias of [`assert.ok()`][].
231231
<!-- YAML
232232
added: v0.1.21
233233
changes:
234+
- version: REPLACEME
235+
pr-url: https://github.com/nodejs/node/pull/57627
236+
description: Invalid dates are now considered equal.
234237
- version: v24.0.0
235238
pr-url: https://github.com/nodejs/node/pull/57622
236239
description: Recursion now stops when either side encounters a circular
@@ -422,6 +425,9 @@ parameter is an instance of {Error} then it will be thrown instead of the
422425
<!-- YAML
423426
added: v1.2.0
424427
changes:
428+
- version: REPLACEME
429+
pr-url: https://github.com/nodejs/node/pull/57627
430+
description: Invalid dates are now considered equal.
425431
- version: v24.0.0
426432
pr-url: https://github.com/nodejs/node/pull/57622
427433
description: Recursion now stops when either side encounters a circular
@@ -2278,9 +2284,12 @@ added:
22782284
- v23.4.0
22792285
- v22.13.0
22802286
changes:
2281-
- version: v24.0.0
2282-
pr-url: https://github.com/nodejs/node/pull/57370
2283-
description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
2287+
- version: REPLACEME
2288+
pr-url: https://github.com/nodejs/node/pull/57627
2289+
description: Invalid dates are now considered equal.
2290+
- version: v24.0.0
2291+
pr-url: https://github.com/nodejs/node/pull/57370
2292+
description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
22842293
-->
22852294

22862295
* `actual` {any}

lib/internal/util/comparisons.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,15 @@ function objectComparisonStart(val1, val2, mode, memos) {
299299
} else if (val1Tag === '[object Object]') {
300300
return keyCheck(val1, val2, mode, memos, kNoIterator);
301301
} else if (isDate(val1)) {
302-
if (!isDate(val2) ||
303-
DatePrototypeGetTime(val1) !== DatePrototypeGetTime(val2)) {
302+
if (!isDate(val2)) {
304303
return false;
305304
}
305+
const time1 = DatePrototypeGetTime(val1);
306+
const time2 = DatePrototypeGetTime(val2);
307+
if (time1 !== time2) {
308+
// eslint-disable-next-line no-self-compare
309+
return time1 !== time1 && time2 !== time2;
310+
}
306311
} else if (isRegExp(val1)) {
307312
if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
308313
return false;

test/parallel/test-assert-deep.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,8 @@ test('Additional tests', () => {
752752

753753
assertNotDeepOrStrict(new Date(), new Date(2000, 3, 14));
754754

755+
assertDeepAndStrictEqual(new Date('foo'), new Date('bar'));
756+
755757
assertDeepAndStrictEqual(/a/, /a/);
756758
assertDeepAndStrictEqual(/a/g, /a/g);
757759
assertDeepAndStrictEqual(/a/i, /a/i);

0 commit comments

Comments
 (0)