mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
assert,util: handle invalid dates as equal in deep comparison
Invalid dates are now handled as equal in all deep comparisons. PR-URL: https://github.com/nodejs/node/pull/57627 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
This commit is contained in:
parent
c3b986853c
commit
11222f1a27
4 changed files with 24 additions and 5 deletions
|
@ -22,6 +22,7 @@ const primValues = {
|
|||
'empty_object': {},
|
||||
'regexp': /abc/i,
|
||||
'date': new Date(),
|
||||
'invalidDate': new Date('foo'),
|
||||
};
|
||||
|
||||
const primValues2 = {
|
||||
|
@ -34,6 +35,7 @@ const primValues2 = {
|
|||
'empty_object': {},
|
||||
'regexp': /abc/i,
|
||||
'date': new Date(primValues.date),
|
||||
'invalidDate': new Date('foo'),
|
||||
};
|
||||
|
||||
const primValuesUnequal = {
|
||||
|
@ -49,6 +51,7 @@ const primValuesUnequal = {
|
|||
'empty_object': [],
|
||||
'regexp': /abc/g,
|
||||
'date': new Date(primValues.date.getTime() + 1),
|
||||
'invalidDate': new Date(),
|
||||
};
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
|
|
|
@ -231,6 +231,9 @@ An alias of [`assert.ok()`][].
|
|||
<!-- YAML
|
||||
added: v0.1.21
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/57627
|
||||
description: Invalid dates are now considered equal.
|
||||
- version: v24.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/57622
|
||||
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
|
|||
<!-- YAML
|
||||
added: v1.2.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/57627
|
||||
description: Invalid dates are now considered equal.
|
||||
- version: v24.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/57622
|
||||
description: Recursion now stops when either side encounters a circular
|
||||
|
@ -2177,6 +2183,9 @@ added:
|
|||
- v23.4.0
|
||||
- v22.13.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/57627
|
||||
description: Invalid dates are now considered equal.
|
||||
- version: v24.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/57370
|
||||
description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
|
||||
|
|
|
@ -299,10 +299,15 @@ function objectComparisonStart(val1, val2, mode, memos) {
|
|||
} else if (val1Tag === '[object Object]') {
|
||||
return keyCheck(val1, val2, mode, memos, kNoIterator);
|
||||
} else if (isDate(val1)) {
|
||||
if (!isDate(val2) ||
|
||||
DatePrototypeGetTime(val1) !== DatePrototypeGetTime(val2)) {
|
||||
if (!isDate(val2)) {
|
||||
return false;
|
||||
}
|
||||
const time1 = DatePrototypeGetTime(val1);
|
||||
const time2 = DatePrototypeGetTime(val2);
|
||||
if (time1 !== time2) {
|
||||
// eslint-disable-next-line no-self-compare
|
||||
return time1 !== time1 && time2 !== time2;
|
||||
}
|
||||
} else if (isRegExp(val1)) {
|
||||
if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
|
||||
return false;
|
||||
|
|
|
@ -752,6 +752,8 @@ test('Additional tests', () => {
|
|||
|
||||
assertNotDeepOrStrict(new Date(), new Date(2000, 3, 14));
|
||||
|
||||
assertDeepAndStrictEqual(new Date('foo'), new Date('bar'));
|
||||
|
||||
assertDeepAndStrictEqual(/a/, /a/);
|
||||
assertDeepAndStrictEqual(/a/g, /a/g);
|
||||
assertDeepAndStrictEqual(/a/i, /a/i);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue