Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/internalize-fast-deep-equal-react-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@knocklabs/react-core": patch
---

Internalize `fast-deep-equal` in `@knocklabs/react-core` with a small inlined deep-equality util and drop the runtime dependency, removing it from consumers' install graphs.
1 change: 0 additions & 1 deletion packages/react-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"@knocklabs/client": "workspace:^",
"@tanstack/react-store": "^0.7.3",
"date-fns": "^4.0.0",
"fast-deep-equal": "^3.1.3",
"swr": "^2.4.1"
},
"devDependencies": {
Expand Down
53 changes: 53 additions & 0 deletions packages/react-core/src/modules/core/deepEqual/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Deep structural equality check. Recursively compares primitives, plain
* objects, arrays, `Date`, and `RegExp` values. Does not special-case Map/Set.
*/
export default function deepEqual(a: unknown, b: unknown): boolean {
if (a === b) return true;

if (a && b && typeof a === "object" && typeof b === "object") {
const objA = a as Record<string, unknown>;
const objB = b as Record<string, unknown>;

if (objA.constructor !== objB.constructor) return false;

if (Array.isArray(a) && Array.isArray(b)) {
const length = a.length;
if (length !== b.length) return false;
for (let i = length; i-- !== 0; ) {
if (!deepEqual(a[i], b[i])) return false;
}
return true;
}

if (a.constructor === RegExp) {
const reA = a as RegExp;
const reB = b as RegExp;
return reA.source === reB.source && reA.flags === reB.flags;
}
if (objA.valueOf !== Object.prototype.valueOf) {
return objA.valueOf() === objB.valueOf();
}
if (objA.toString !== Object.prototype.toString) {
return objA.toString() === objB.toString();
}

const keys = Object.keys(objA);
const length = keys.length;
if (length !== Object.keys(objB).length) return false;

for (let i = length; i-- !== 0; ) {
if (!Object.prototype.hasOwnProperty.call(objB, keys[i]!)) return false;
}

for (let i = length; i-- !== 0; ) {
const key = keys[i]!;
if (!deepEqual(objA[key], objB[key])) return false;
}

return true;
}

// true if both NaN, false otherwise
return a !== a && b !== b;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import fastDeepEqual from "fast-deep-equal";
import { useMemo, useRef } from "react";

import deepEqual from "../deepEqual";

export default function useStableOptions<T>(options: T): T {
const optionsRef = useRef<T>(undefined);

return useMemo(() => {
const currentOptions = optionsRef.current;

if (currentOptions && fastDeepEqual(options, currentOptions)) {
if (currentOptions && deepEqual(options, currentOptions)) {
return currentOptions;
}

Expand Down
65 changes: 65 additions & 0 deletions packages/react-core/test/core/deepEqual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, test } from "vitest";

import deepEqual from "../../src/modules/core/deepEqual";

describe("deepEqual", () => {
test("treats identical references and equal primitives as equal", () => {
const obj = { a: 1 };
expect(deepEqual(obj, obj)).toBe(true);
expect(deepEqual(1, 1)).toBe(true);
expect(deepEqual("a", "a")).toBe(true);
expect(deepEqual(true, true)).toBe(true);
expect(deepEqual(null, null)).toBe(true);
expect(deepEqual(undefined, undefined)).toBe(true);
});

test("distinguishes differing primitives", () => {
expect(deepEqual(1, 2)).toBe(false);
expect(deepEqual("a", "b")).toBe(false);
expect(deepEqual(null, undefined)).toBe(false);
expect(deepEqual(0, false)).toBe(false);
});

test("treats NaN as equal to NaN", () => {
expect(deepEqual(NaN, NaN)).toBe(true);
});

test("compares nested objects structurally", () => {
expect(deepEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } })).toBe(true);
expect(deepEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 3 } })).toBe(false);
});

test("is independent of object key order", () => {
expect(deepEqual({ a: 1, b: 2 }, { b: 2, a: 1 })).toBe(true);
});

test("distinguishes objects with differing key counts", () => {
expect(deepEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false);
expect(deepEqual({ a: 1, b: 2 }, { a: 1 })).toBe(false);
});

test("compares arrays by length and element", () => {
expect(deepEqual([1, 2, 3], [1, 2, 3])).toBe(true);
expect(deepEqual([1, 2], [1, 2, 3])).toBe(false);
expect(deepEqual([{ a: 1 }], [{ a: 1 }])).toBe(true);
});

test("does not treat an array and an object as equal", () => {
expect(deepEqual([], {})).toBe(false);
});

test("compares Date values by time", () => {
expect(deepEqual(new Date("2020-01-01"), new Date("2020-01-01"))).toBe(
true,
);
expect(deepEqual(new Date("2020-01-01"), new Date("2021-01-01"))).toBe(
false,
);
});

test("compares RegExp values by source and flags", () => {
expect(deepEqual(/abc/gi, /abc/gi)).toBe(true);
expect(deepEqual(/abc/g, /abc/i)).toBe(false);
expect(deepEqual(/abc/, /abd/)).toBe(false);
});
});
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5044,7 +5044,6 @@ __metadata:
eslint: "npm:^8.56.0"
eslint-plugin-react-hooks: "npm:^5.2.0"
eslint-plugin-react-refresh: "npm:^0.5.2"
fast-deep-equal: "npm:^3.1.3"
jsdom: "npm:^29.1.0"
react: "npm:^19.2.5"
react-dom: "npm:^19.2.5"
Expand Down
Loading