Skip to content

fix: handle null-prototype objects and NaN in shallow equality - #295

Merged
mbret merged 2 commits into
mbret:mainfrom
sogav21:fix/268-shallow-equality
Nov 13, 2026
Merged

mbret merged 2 commits into
mbret:mainfrom
sogav21:fix/268-shallow-equality

Conversation

@sogav21

@sogav21 sogav21 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

isShallowEqual throws for null-prototype dictionaries and objects that shadow hasOwnProperty, turning a useObserve emission into an error. Use Object.hasOwn for own-key checks and Object.is for value comparisons, with regressions through both the utility and useObserve. Comparison with null is also covered in both argument orders.

NaN equals NaN, while +0 and -0 differ, including object properties. Callers that want both zero signs treated alike should normalize them to 0 or supply their own compareFn. The docs clarify that this remains an own-enumerable-key comparison and does not inspect Date, Map or Set contents. Those limitations are documented without assertions that would prevent a future improvement.

Fixes #268.

Validation on Windows Node 26.13.1 with unchanged npm-ci locked dependencies:

  • The additional null-second-argument regression fails with TypeError on original main ec7dfe8bb396daed50803e0935fe13ae0067bfec; reverse-order and ordinary-object/null controls behave as expected.
  • npm run check: passed; npm run build: passed; npm run test:ci: 43 files / 328 tests passed.
  • Required checks ran on a canonical LF validation tree because the host checkout uses CRLF. All 167 tracked files were verified against the PR head plus this exact follow-up patch. Hosted CI for the follow-up has not run locally.

Prepared with OpenAI Codex assistance.

@mbret mbret left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes #268 as described. The inline comments ask for small changes only.

  • npm run check, npm run build and npm run test:ci pass on the head commit (327 tests).
  • The 6 new tests fail on main and pass on this branch.
  • Object.hasOwn is already used in createLocalStorageAdapter.ts, so the runtime requirements stay the same.

Generated by Claude Code

Comment thread src/lib/utils/shallowEqual.ts Outdated
@@ -1,18 +1,21 @@
export function isShallowEqual<A, B>(objA: A, objB: B): boolean {
// Check if both objects are null or undefined

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this comment say "either"? The condition now returns early when either value is null or undefined.

Suggested change
// Check if both objects are null or undefined
// Return early if either value is null or undefined

Generated by Claude Code

Comment on lines +3 to +8
if (
objA === null ||
objB === null ||
objA === undefined ||
objB === undefined
) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The objB === null check also fixes a second crash, and no test covers it. On main, isShallowEqual(Object.create(null), null) throws TypeError: Cannot convert undefined or null to object. Could you add a regression test to shallowEqual.test.ts?

it("compares a null-prototype object with null", () => {
  expect(isShallowEqual(Object.create(null), null)).toBe(false)
})

Generated by Claude Code

Comment thread src/lib/utils/shallowEqual.test.ts Outdated
Comment on lines +112 to +118
it("compares nested objects by reference without inspecting Date or Map contents", () => {
const value = { x: 1 }
expect(isShallowEqual({ value }, { value })).toBe(true)
expect(isShallowEqual({ value }, { value: { x: 1 } })).toBe(false)
expect(isShallowEqual(new Date(0), new Date(1))).toBe(true)
expect(isShallowEqual(new Map([["x", 1]]), new Map([["x", 2]]))).toBe(true)
})

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the Date and Map assertions? They make a known limitation part of the tested contract. The docs already state the limitation. If a later change compares Date contents, this test fails although the change is correct.

Suggested change
it("compares nested objects by reference without inspecting Date or Map contents", () => {
const value = { x: 1 }
expect(isShallowEqual({ value }, { value })).toBe(true)
expect(isShallowEqual({ value }, { value: { x: 1 } })).toBe(false)
expect(isShallowEqual(new Date(0), new Date(1))).toBe(true)
expect(isShallowEqual(new Map([["x", 1]]), new Map([["x", 2]]))).toBe(true)
})
it("compares nested objects by reference", () => {
const value = { x: 1 }
expect(isShallowEqual({ value }, { value })).toBe(true)
expect(isShallowEqual({ value }, { value: { x: 1 } })).toBe(false)
})

Generated by Claude Code


### Comparison semantics

`NaN` now compares equal to `NaN`, while positive and negative zero compare differently. Previously this helper used `===`, which did the opposite for these cases. If your application treats both zero signs as the same value, normalize them to `0` before comparison or pass a custom `compareFn` with your intended equality rule.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this paragraph describe only the current behavior? "now" and "Previously" read as a changelog, and they become stale after the next release. The release notes come from the commit message.

Suggested change
`NaN` now compares equal to `NaN`, while positive and negative zero compare differently. Previously this helper used `===`, which did the opposite for these cases. If your application treats both zero signs as the same value, normalize them to `0` before comparison or pass a custom `compareFn` with your intended equality rule.
`NaN` compares equal to `NaN`, and positive and negative zero compare as different values. If your application treats both zero signs as the same value, normalize them to `0` before comparison, or pass a custom `compareFn`.

Generated by Claude Code

@sogav21

sogav21 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the five inline comments in 43e1771:

  • 4561554550: the null/undefined comment says “either”.
  • 4561554907: added the null-prototype/null regression in both argument orders. The second-argument-null case fails on original main with the reported TypeError; reverse-order and ordinary-object controls pass.
  • 4561555349: removed the Date/Map assertions, keeping nested-reference coverage.
  • 4561555696: the helper documentation describes current behavior.
  • 4561556075: removed duplicated numeric details from compareFn, retaining the helper link and shallow-contents caveat.

All three required checks pass locally on Node 26.13.1, with 43 files / 328 tests. Validation used the exact canonical LF Git tree and unchanged locked dependencies. No comparator algorithm change was needed in this follow-up.

Prepared with OpenAI Codex assistance.

@mbret
mbret merged commit e244fa8 into mbret:main Nov 13, 2026
2 checks passed
@mbret

mbret commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Thanks for the work <3

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.

isShallowEqual: throws on objects without a prototype, and NaN never equals NaN

2 participants