From 0e00c383deef044658852f3dcf3fa32d1138f09f Mon Sep 17 00:00:00 2001 From: sogav21 <223452947+sogav21@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:20:18 +0200 Subject: [PATCH 1/2] fix: handle null-prototype objects and NaN in shallow equality --- gitbook/reactjrx/bindings/react/useobserve.md | 2 +- .../utils/javascript/isshallowequal.md | 10 +++- .../useObserve/useObserve.compare.test.tsx | 56 +++++++++++++++++++ src/lib/utils/shallowEqual.test.ts | 50 +++++++++++++++++ src/lib/utils/shallowEqual.ts | 24 ++++---- 5 files changed, 130 insertions(+), 12 deletions(-) diff --git a/gitbook/reactjrx/bindings/react/useobserve.md b/gitbook/reactjrx/bindings/react/useobserve.md index 4874b227..00a681f0 100644 --- a/gitbook/reactjrx/bindings/react/useobserve.md +++ b/gitbook/reactjrx/bindings/react/useobserve.md @@ -43,7 +43,7 @@ interface Options { * `source`: An observable, a function returning one, or `undefined`. Pass a stable observable directly: the hook subscribes to it again when its reference changes. Otherwise pass a function with `deps`, so the observable is only created again when a dependency changes. With `undefined`, nothing is observed and the default value is returned. * **optional** `options`: * `defaultValue`: returned until the source emits its first value. A `BehaviorSubject`, or any source that emits synchronously when subscribed, doesn't need it: its value is there on the first render. - * `compareFn`: tells whether a new value equals the previous one. An equal value doesn't re-render. Defaults to strict equality (`===`). Pass `isShallowEqual` (exported by reactjrx), or your own function, to compare objects by content. + * `compareFn`: tells whether a new value equals the previous one. An equal value doesn't re-render. Defaults to strict equality (`===`). Pass [isShallowEqual](../../utils/javascript/isshallowequal.md) (exported by reactjrx) to compare own enumerable properties one level deep with `Object.is`, or your own function for a different equality rule. `isShallowEqual` treats `NaN` as equal and distinguishes positive and negative zero; it does not compare the contents of `Date`, `Map` or `Set` objects. * **optional** `deps`: The values the function depends on. When one of them changes (compared with [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)), the function is called again and its observable replaces the previous one. ### Returns diff --git a/gitbook/reactjrx/utils/javascript/isshallowequal.md b/gitbook/reactjrx/utils/javascript/isshallowequal.md index 9acec94a..48838ded 100644 --- a/gitbook/reactjrx/utils/javascript/isshallowequal.md +++ b/gitbook/reactjrx/utils/javascript/isshallowequal.md @@ -1,6 +1,6 @@ # isShallowEqual -Returns true when two values are equal one level deep: the same primitive, or two objects with the same `constructor` and the same own enumerable string keys holding `===` values. Symbol and non-enumerable properties are not compared. +Returns true when two values are equal one level deep: primitives compared with `Object.is`, or two objects with the same `constructor` and the same own enumerable string keys holding `Object.is`-equal values. Objects with a null prototype and objects with an own `hasOwnProperty` key are supported. Symbol and non-enumerable properties are not compared. ### Usage @@ -9,10 +9,18 @@ import { isShallowEqual } from "reactjrx" isShallowEqual({ a: 1, b: "x" }, { a: 1, b: "x" }) // true isShallowEqual({ a: { b: 1 } }, { a: { b: 1 } }) // false, `a` holds two different objects +isShallowEqual({ a: NaN }, { a: NaN }) // true +isShallowEqual(0, -0) // false ``` Pass it as the `compareFn` of [useObserve](../../bindings/react/useobserve.md) to skip the render of a new object that holds the same values. +### 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. + +This is a shallow comparison of enumerable properties, not a comparison of built-in object contents. Two `Date` objects with different timestamps or two `Map` or `Set` objects with different entries can compare equal because they have the same constructor and no own enumerable string keys. Use a comparator that checks their contents when those differences matter. + ### Reference ```typescript diff --git a/src/lib/binding/useObserve/useObserve.compare.test.tsx b/src/lib/binding/useObserve/useObserve.compare.test.tsx index e0c89950..470f16b0 100644 --- a/src/lib/binding/useObserve/useObserve.compare.test.tsx +++ b/src/lib/binding/useObserve/useObserve.compare.test.tsx @@ -75,6 +75,62 @@ it("should not return new value when the hook re-render but the value is the sam }) describe("given a custom compareFn", () => { + it("keeps null-prototype emissions live and only renders changed values", () => { + const initial = Object.assign(Object.create(null), { x: 1 }) + const source$ = new BehaviorSubject>(initial) + const values: UseObserveResult< + Record, + Record + >[] = [] + const { result } = renderHook(() => { + const value = useObserve(source$, { compareFn: isShallowEqual }) + useEffect(() => { + values.push(value) + }, [value]) + return value + }) + + act(() => { + source$.next(Object.assign(Object.create(null), { x: 1 })) + }) + expect(values).toHaveLength(1) + expect(result.current.error).toBeUndefined() + expect(result.current.observableState).toBe("live") + + act(() => { + source$.next(Object.assign(Object.create(null), { x: 2 })) + }) + expect(values).toHaveLength(2) + expect(result.current.data?.x).toBe(2) + expect(result.current.error).toBeUndefined() + }) + + it("deduplicates NaN properties while delivering signed-zero changes", () => { + const source$ = new BehaviorSubject({ x: Number.NaN }) + const values: Array<{ x: number } | undefined> = [] + const { result } = renderHook(() => { + const value = useObserve(source$, { compareFn: isShallowEqual }) + useEffect(() => { + values.push(value.data) + }, [value]) + return value + }) + + act(() => { + source$.next({ x: Number.NaN }) + }) + expect(values).toHaveLength(1) + + act(() => { + source$.next({ x: 0 }) + }) + act(() => { + source$.next({ x: -0 }) + }) + expect(values).toHaveLength(3) + expect(result.current.data?.x).toBe(-0) + }) + it("should re-render when the object is same but new reference", async () => { const values: Array<{ a: string } | undefined> = [] const source$ = new BehaviorSubject({ a: "a" }) diff --git a/src/lib/utils/shallowEqual.test.ts b/src/lib/utils/shallowEqual.test.ts index 6974b82f..33648487 100644 --- a/src/lib/utils/shallowEqual.test.ts +++ b/src/lib/utils/shallowEqual.test.ts @@ -66,4 +66,54 @@ describe("shallowEqual", () => { const obj2 = "hello" expect(isShallowEqual(obj1, obj2)).toBe(false) }) + + it("compares null-prototype dictionaries by their own keys", () => { + const a = Object.assign(Object.create(null), { x: 1 }) + const b = Object.assign(Object.create(null), { x: 1 }) + expect(isShallowEqual(a, b)).toBe(true) + b.x = 2 + expect(isShallowEqual(a, b)).toBe(false) + }) + + it("handles an own property named hasOwnProperty", () => { + expect(isShallowEqual({ hasOwnProperty: 1 }, { hasOwnProperty: 1 })).toBe( + true, + ) + expect(isShallowEqual({ hasOwnProperty: 1 }, { hasOwnProperty: 2 })).toBe( + false, + ) + }) + + it("does not mistake inherited keys for own keys", () => { + const b = Object.assign(Object.create({ x: 1 }), { y: 1 }) + expect(isShallowEqual({ x: 1 }, b)).toBe(false) + }) + + it("considers NaN equal to itself and to another NaN property", () => { + expect(isShallowEqual(Number.NaN, Number.NaN)).toBe(true) + expect(isShallowEqual({ x: Number.NaN }, { x: Number.NaN })).toBe(true) + expect( + isShallowEqual({ constructor: Number.NaN }, { constructor: Number.NaN }), + ).toBe(true) + }) + + it("distinguishes positive and negative zero", () => { + expect(isShallowEqual(0, -0)).toBe(false) + expect(isShallowEqual(-0, 0)).toBe(false) + expect(isShallowEqual({ x: 0 }, { x: -0 })).toBe(false) + expect(isShallowEqual({ x: -0 }, { x: -0 })).toBe(true) + }) + + it("keeps different constructors distinct", () => { + expect(isShallowEqual(Object.create(null), {})).toBe(false) + expect(isShallowEqual([], {})).toBe(false) + }) + + 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) + }) }) diff --git a/src/lib/utils/shallowEqual.ts b/src/lib/utils/shallowEqual.ts index 9e7414b8..2b2150b8 100644 --- a/src/lib/utils/shallowEqual.ts +++ b/src/lib/utils/shallowEqual.ts @@ -1,18 +1,21 @@ export function isShallowEqual(objA: A, objB: B): boolean { // Check if both objects are null or undefined - if (objA === null || objA === undefined || objB === undefined) { - // biome-ignore lint/suspicious/noExplicitAny: TODO - return (objA as any) === (objB as any) + if ( + objA === null || + objB === null || + objA === undefined || + objB === undefined + ) { + return Object.is(objA, objB) } // Check if both objects are primitives if (typeof objA !== "object" || typeof objB !== "object") { - // biome-ignore lint/suspicious/noExplicitAny: TODO - return (objA as any) === (objB as any) + return Object.is(objA, objB) } - // Check if both objects have the same prototype - if (objA.constructor !== objB?.constructor) { + // Check if both objects have the same constructor + if (!Object.is(objA.constructor, objB.constructor)) { return false } @@ -26,9 +29,10 @@ export function isShallowEqual(objA: A, objB: B): boolean { // Check if the values of the keys are equal for (const key of keysA) { - // biome-ignore lint/suspicious/noPrototypeBuiltins: TODO - // biome-ignore lint/suspicious/noExplicitAny: TODO - if (!objB.hasOwnProperty(key) || objA[key] !== (objB as any)[key]) { + if ( + !Object.hasOwn(objB, key) || + !Object.is(objA[key], (objB as Record)[key]) + ) { return false } } From 43e1771e5e38a2de6fa888f23f40eba251cc03f1 Mon Sep 17 00:00:00 2001 From: sogav21 <223452947+sogav21@users.noreply.github.com> Date: Fri, 13 Nov 2026 11:33:51 +0200 Subject: [PATCH 2/2] test: cover null comparisons and refine shallow equality docs --- gitbook/reactjrx/bindings/react/useobserve.md | 2 +- gitbook/reactjrx/utils/javascript/isshallowequal.md | 2 +- src/lib/utils/shallowEqual.test.ts | 10 +++++++--- src/lib/utils/shallowEqual.ts | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/gitbook/reactjrx/bindings/react/useobserve.md b/gitbook/reactjrx/bindings/react/useobserve.md index 00a681f0..5b94f9cb 100644 --- a/gitbook/reactjrx/bindings/react/useobserve.md +++ b/gitbook/reactjrx/bindings/react/useobserve.md @@ -43,7 +43,7 @@ interface Options { * `source`: An observable, a function returning one, or `undefined`. Pass a stable observable directly: the hook subscribes to it again when its reference changes. Otherwise pass a function with `deps`, so the observable is only created again when a dependency changes. With `undefined`, nothing is observed and the default value is returned. * **optional** `options`: * `defaultValue`: returned until the source emits its first value. A `BehaviorSubject`, or any source that emits synchronously when subscribed, doesn't need it: its value is there on the first render. - * `compareFn`: tells whether a new value equals the previous one. An equal value doesn't re-render. Defaults to strict equality (`===`). Pass [isShallowEqual](../../utils/javascript/isshallowequal.md) (exported by reactjrx) to compare own enumerable properties one level deep with `Object.is`, or your own function for a different equality rule. `isShallowEqual` treats `NaN` as equal and distinguishes positive and negative zero; it does not compare the contents of `Date`, `Map` or `Set` objects. + * `compareFn`: tells whether a new value equals the previous one. An equal value doesn't re-render. Defaults to strict equality (`===`). Pass [isShallowEqual](../../utils/javascript/isshallowequal.md) (exported by reactjrx) to compare own enumerable properties one level deep, or your own function for a different equality rule. `isShallowEqual` does not compare the contents of `Date`, `Map` or `Set` objects. * **optional** `deps`: The values the function depends on. When one of them changes (compared with [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)), the function is called again and its observable replaces the previous one. ### Returns diff --git a/gitbook/reactjrx/utils/javascript/isshallowequal.md b/gitbook/reactjrx/utils/javascript/isshallowequal.md index 48838ded..40f277b3 100644 --- a/gitbook/reactjrx/utils/javascript/isshallowequal.md +++ b/gitbook/reactjrx/utils/javascript/isshallowequal.md @@ -17,7 +17,7 @@ Pass it as the `compareFn` of [useObserve](../../bindings/react/useobserve.md) t ### 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. +`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`. This is a shallow comparison of enumerable properties, not a comparison of built-in object contents. Two `Date` objects with different timestamps or two `Map` or `Set` objects with different entries can compare equal because they have the same constructor and no own enumerable string keys. Use a comparator that checks their contents when those differences matter. diff --git a/src/lib/utils/shallowEqual.test.ts b/src/lib/utils/shallowEqual.test.ts index 33648487..f4642774 100644 --- a/src/lib/utils/shallowEqual.test.ts +++ b/src/lib/utils/shallowEqual.test.ts @@ -19,6 +19,12 @@ describe("shallowEqual", () => { expect(isShallowEqual(null, undefined)).toBe(false) }) + it("compares a null-prototype object with null", () => { + const value = Object.create(null) + expect(isShallowEqual(value, null)).toBe(false) + expect(isShallowEqual(null, value)).toBe(false) + }) + it("returns true for two string values that are equal", () => { expect(isShallowEqual("hello", "hello")).toBe(true) }) @@ -109,11 +115,9 @@ describe("shallowEqual", () => { expect(isShallowEqual([], {})).toBe(false) }) - it("compares nested objects by reference without inspecting Date or Map contents", () => { + it("compares nested objects by reference", () => { 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) }) }) diff --git a/src/lib/utils/shallowEqual.ts b/src/lib/utils/shallowEqual.ts index 2b2150b8..853457d3 100644 --- a/src/lib/utils/shallowEqual.ts +++ b/src/lib/utils/shallowEqual.ts @@ -1,5 +1,5 @@ export function isShallowEqual(objA: A, objB: B): boolean { - // Check if both objects are null or undefined + // Return early if either value is null or undefined if ( objA === null || objB === null ||