Repository navigation
util.isDeepStrictEqual regex comparison #28766
Description
Activity
Not saying the existing behavior is right or wrong, but it does behave as documented. The
util.isDeepStrictEqual()documentation points to theassert.deepStrictEqual()documentation, which contains a comparison details section. The comparison details section specifically states: Only enumerable "own" properties are considered.Reacted by Graham FairweatherYes, I saw that and yes it behaves as described. And yes, I guess it is a question of "is this behaviour correct for strictly comparing regexes"? They are similar, but you would get a different result from using them.
This is an interesting edge case. It would be good to know how other assertion libraries work. In some cases it'll likely be good to distinguish the regular expression based on
lastIndexwhile in other cases it might be better to ignore it. I guess most people will just pass through "fresh" regular expressions wherelastIndexis set to0. That's why my feeling is +0.5 on adding a check for this specific case.These days many are deferring to the Node isDeepStrictEqual spec, so this is becoming the measuring stick. In my own previous library I compared
lastIndexas I feel it is the right thing to do, I don't think Lodash's isEqual does but again that is being deferred to this spec. I don't believe that ljharb's is-equal compares this. Chai's does not. Many others are just reworks of the previous Node deepEqual, which did not. Many just rely on comparing left and righttoStringwhich clearly does not consider this. For me it feels that a deep equal perhaps should not but a deep strict equal should.How about to add an option parameter for this function?
e.g.
assert.deepStrictEqual(a, b, { supportRegexp: true });
At worst, document the edge case and say that the developer must perform this check if necessary?
const util = require('util'); const rx1 = /a/; rx.lastIndex = 3; const rx2 = /a/ const isEqual = util.isDeepStrictEqual(rx1, rx2) && rx1.lastIndex === rx2.lastIndex;- addedutilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
on Dec 26, 2020 - added a commit that references this issue
on Dec 2, 2021
v10.14.1
Mac OS Darwin Kernel Version 18.6.0
My expectation would have been
false. I see thatareSimilarRegExpsdoes not perform a check onlastIndex, andkeyCheckperforms a comparison of enumerable keys, andlastIndexis not enumerable. This may be considered correct, but it feels wrong to me.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex
Thankyou for your time and consideration.