mirror of
https://github.com/nodejs/node.git
synced 2025-12-27 23:41:14 +00: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
@ -22,6 +22,7 @@ const primValues = {
|
|||||||
'empty_object': {},
|
'empty_object': {},
|
||||||
'regexp': /abc/i,
|
'regexp': /abc/i,
|
||||||
'date': new Date(),
|
'date': new Date(),
|
||||||
|
'invalidDate': new Date('foo'),
|
||||||
};
|
};
|
||||||
|
|
||||||
const primValues2 = {
|
const primValues2 = {
|
||||||
@ -34,6 +35,7 @@ const primValues2 = {
|
|||||||
'empty_object': {},
|
'empty_object': {},
|
||||||
'regexp': /abc/i,
|
'regexp': /abc/i,
|
||||||
'date': new Date(primValues.date),
|
'date': new Date(primValues.date),
|
||||||
|
'invalidDate': new Date('foo'),
|
||||||
};
|
};
|
||||||
|
|
||||||
const primValuesUnequal = {
|
const primValuesUnequal = {
|
||||||
@ -49,6 +51,7 @@ const primValuesUnequal = {
|
|||||||
'empty_object': [],
|
'empty_object': [],
|
||||||
'regexp': /abc/g,
|
'regexp': /abc/g,
|
||||||
'date': new Date(primValues.date.getTime() + 1),
|
'date': new Date(primValues.date.getTime() + 1),
|
||||||
|
'invalidDate': new Date(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
|
|||||||
@ -231,6 +231,9 @@ An alias of [`assert.ok()`][].
|
|||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v0.1.21
|
added: v0.1.21
|
||||||
changes:
|
changes:
|
||||||
|
- version: REPLACEME
|
||||||
|
pr-url: https://github.com/nodejs/node/pull/57627
|
||||||
|
description: Invalid dates are now considered equal.
|
||||||
- version: v24.0.0
|
- version: v24.0.0
|
||||||
pr-url: https://github.com/nodejs/node/pull/57622
|
pr-url: https://github.com/nodejs/node/pull/57622
|
||||||
description: Recursion now stops when either side encounters a circular
|
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
|
<!-- YAML
|
||||||
added: v1.2.0
|
added: v1.2.0
|
||||||
changes:
|
changes:
|
||||||
|
- version: REPLACEME
|
||||||
|
pr-url: https://github.com/nodejs/node/pull/57627
|
||||||
|
description: Invalid dates are now considered equal.
|
||||||
- version: v24.0.0
|
- version: v24.0.0
|
||||||
pr-url: https://github.com/nodejs/node/pull/57622
|
pr-url: https://github.com/nodejs/node/pull/57622
|
||||||
description: Recursion now stops when either side encounters a circular
|
description: Recursion now stops when either side encounters a circular
|
||||||
@ -2177,9 +2183,12 @@ added:
|
|||||||
- v23.4.0
|
- v23.4.0
|
||||||
- v22.13.0
|
- v22.13.0
|
||||||
changes:
|
changes:
|
||||||
- version: v24.0.0
|
- version: REPLACEME
|
||||||
pr-url: https://github.com/nodejs/node/pull/57370
|
pr-url: https://github.com/nodejs/node/pull/57627
|
||||||
description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
|
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.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
* `actual` {any}
|
* `actual` {any}
|
||||||
|
|||||||
@ -299,10 +299,15 @@ function objectComparisonStart(val1, val2, mode, memos) {
|
|||||||
} else if (val1Tag === '[object Object]') {
|
} else if (val1Tag === '[object Object]') {
|
||||||
return keyCheck(val1, val2, mode, memos, kNoIterator);
|
return keyCheck(val1, val2, mode, memos, kNoIterator);
|
||||||
} else if (isDate(val1)) {
|
} else if (isDate(val1)) {
|
||||||
if (!isDate(val2) ||
|
if (!isDate(val2)) {
|
||||||
DatePrototypeGetTime(val1) !== DatePrototypeGetTime(val2)) {
|
|
||||||
return false;
|
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)) {
|
} else if (isRegExp(val1)) {
|
||||||
if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
|
if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -752,6 +752,8 @@ test('Additional tests', () => {
|
|||||||
|
|
||||||
assertNotDeepOrStrict(new Date(), new Date(2000, 3, 14));
|
assertNotDeepOrStrict(new Date(), new Date(2000, 3, 14));
|
||||||
|
|
||||||
|
assertDeepAndStrictEqual(new Date('foo'), new Date('bar'));
|
||||||
|
|
||||||
assertDeepAndStrictEqual(/a/, /a/);
|
assertDeepAndStrictEqual(/a/, /a/);
|
||||||
assertDeepAndStrictEqual(/a/g, /a/g);
|
assertDeepAndStrictEqual(/a/g, /a/g);
|
||||||
assertDeepAndStrictEqual(/a/i, /a/i);
|
assertDeepAndStrictEqual(/a/i, /a/i);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user