Skip to content

fix: compare own enumerable properties on Date, RegExp, and boxed primitives - #111

Closed
spokodev wants to merge 1 commit into
chaijs:mainfrom
spokodev:fix/own-props-on-date-regexp-boxed
Closed

spokodev wants to merge 1 commit into
chaijs:mainfrom
spokodev:fix/own-props-on-date-regexp-boxed

Conversation

@spokodev

@spokodev spokodev commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

deep-eql ignores own enumerable properties on Date, RegExp, and boxed-primitive (String/Number/Boolean) instances. Two such objects are reported deeply equal even when they carry differing own enumerable data properties.

import eql from 'deep-eql';
import util from 'node:util';

const a = new Date(0); a.foo = 1;
const b = new Date(0); b.foo = 2;

eql(a, b);                      // true  (wrong)
util.isDeepStrictEqual(a, b);   // false (oracle)

Same for RegExp (const r = /x/; r.foo = ...) and boxed new Number(1) / new String(...) / new Boolean(...).

This contradicts the documented contract in the README "Rules" section:

All own and inherited enumerable properties are considered.

Only Error is documented as an exception. The control case is already correct (eql({foo:1}, {foo:2}) === false), which shows that only the special-cased types bypass the rule.

Root cause

In extensiveDeepEqualByType(), the String/Number/Boolean/Date branch returns only the valueOf() comparison, and the RegExp branch returns only the toString() comparison. Both return before the objectEqual() default branch, so own enumerable properties are never compared.

Fix

AND the type-specific value check with the existing objectEqual() own-enumerable-key comparison for those branches. objectEqual() already returns true when both sides have zero own enumerable keys, so plain instances are unaffected. valueOf / -0 / NaN semantics and the documented Error special-case are untouched.

Verification

Red -> green: 5 new tests (one per affected type) assert that instances with differing own enumerable properties are not equal. They fail before the fix and pass after.

Each new case was cross-checked against node:util.isDeepStrictEqual as an oracle, including the cases that must stay equal (same prop value, no extra props, new Number(NaN), new Date(NaN)) and the Error special-case (extra own props still ignored, as documented).

After rebasing on main: lint clean, test:node 185 passing, test:browser (Chrome) 183 passed and 2 skipped. With main's index.js, exactly the 5 new tests fail. #118 added case 'BigInt': to the same branch, so a boxed BigInt with differing own enumerable properties is now not equal either, as util.isDeepStrictEqual reports.

…mitives

Date, RegExp, and boxed-primitive (String/Number/Boolean) instances were
compared only by their internal value (valueOf/toString), so two such
objects with differing own enumerable data properties were reported as
deeply equal. This contradicts the documented rule that all own and
inherited enumerable properties are considered (only Error is exempt) and
diverges from node's util.isDeepStrictEqual.

AND the type-specific value check with the existing objectEqual own-key
comparison so extra own properties are taken into account while valueOf,
-0/NaN, and the Error special-case semantics are preserved.
@spokodev
spokodev force-pushed the fix/own-props-on-date-regexp-boxed branch from 5690c87 to 61c4be2 Compare October 13, 2026 11:48
@spokodev

Copy link
Copy Markdown
Contributor Author

Rebased on main. The only conflict was in extensiveDeepEqualByType, where #118 added case 'BigInt': to the same branch; I kept it, so a boxed BigInt with differing own enumerable properties is now not equal either, as util.isDeepStrictEqual reports. Lint is clean, test:node has 185 passing and test:browser (Chrome) 183 passed with 2 skipped. With main's index.js, exactly the 5 new tests fail.

@43081j

43081j commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

since this is just an agent, im going to close this until a human shows that this is a problem in need of a fix.

@43081j 43081j closed this Oct 13, 2026
@spokodev

Copy link
Copy Markdown
Contributor Author

Hi, this is spokodev, I'm a real person here. Yes, I work with an AI agent, but I checked this one.

I found it by checking the README rule against the code, not from a failing project. Where these objects do come up: path-to-regexp 1.x, which react-router 5 depends on, returns a RegExp with an enumerable keys property listing the route's parameters. So pathToRegexp('/user/:id') and pathToRegexp('/user/:name') pass expect(a).to.deep.equal(b) in chai, while assert.deepStrictEqual says they differ. I haven't found a project that reported running into this, so if that isn't enough, leaving it closed is fine.

@43081j

43081j commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

if it comes up again we can revisit 👍 i.e. if there's demand for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants