Skip to content

Commit 9c15313

Browse files
committed
refactor(ui): replace fast-deep-equal with custom isEqual implementation
- Remove dependency on fast-deep-equal package - Create custom isEqual helper function with comprehensive deep comparison - Add extensive test suite for isEqual function - Update import and usage across project files
1 parent fb7e918 commit 9c15313

File tree

6 files changed

+343
-461
lines changed

6 files changed

+343
-461
lines changed

bun.lock

Lines changed: 191 additions & 458 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ui/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@
191191
"comment-json": "4.2.5",
192192
"debounce": "2.2.0",
193193
"deepmerge-ts": "7.1.4",
194-
"fast-deep-equal": "3.1.3",
195194
"klona": "2.0.6",
196195
"package-manager-detector": "0.2.9",
197196
"recast": "0.23.11",

packages/ui/src/cli/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import path from "path";
44
import toml from "@iarna/toml";
55
import chokidar from "chokidar";
66
import cjson from "comment-json";
7-
import isEqual from "fast-deep-equal";
87
import { resolveCommand } from "package-manager-detector/commands";
98
import { detect } from "package-manager-detector/detect";
9+
import { isEqual } from "../helpers/is-equal";
1010
import {
1111
automaticClassGenerationMessage,
1212
classListFile,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { describe, expect, it } from "vitest";
2+
import { isEqual } from "./is-equal";
3+
4+
describe("isEqual helper", () => {
5+
it("should return true for identical primitive values", () => {
6+
expect(isEqual(1, 1)).toBe(true);
7+
expect(isEqual("string", "string")).toBe(true);
8+
expect(isEqual(true, true)).toBe(true);
9+
expect(isEqual(null, null)).toBe(true);
10+
expect(isEqual(undefined, undefined)).toBe(true);
11+
});
12+
13+
it("should return false for different primitive values", () => {
14+
expect(isEqual(1, 2)).toBe(false);
15+
expect(isEqual("string", "different")).toBe(false);
16+
expect(isEqual(true, false)).toBe(false);
17+
expect(isEqual(null, undefined)).toBe(false);
18+
expect(isEqual(0, null)).toBe(false);
19+
});
20+
21+
it("should handle NaN values correctly", () => {
22+
expect(isEqual(NaN, NaN)).toBe(true);
23+
expect(isEqual(NaN, 1)).toBe(false);
24+
expect(isEqual(1, NaN)).toBe(false);
25+
});
26+
27+
it("should compare arrays correctly", () => {
28+
expect(isEqual([1, 2, 3], [1, 2, 3])).toBe(true);
29+
expect(isEqual([1, 2, 3], [1, 2, 4])).toBe(false);
30+
expect(isEqual([1, 2, 3], [1, 2])).toBe(false);
31+
expect(isEqual([], [])).toBe(true);
32+
expect(isEqual([1, [2, 3]], [1, [2, 3]])).toBe(true);
33+
expect(isEqual([1, [2, 3]], [1, [2, 4]])).toBe(false);
34+
});
35+
36+
it("should compare objects correctly", () => {
37+
expect(isEqual({}, {})).toBe(true);
38+
expect(isEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true);
39+
expect(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 })).toBe(true);
40+
expect(isEqual({ a: 1, b: 2 }, { a: 1, b: 3 })).toBe(false);
41+
expect(isEqual({ a: 1, b: 2 }, { a: 1 })).toBe(false);
42+
expect(isEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false);
43+
});
44+
45+
it("should handle nested objects", () => {
46+
expect(isEqual({ a: { b: 1 } }, { a: { b: 1 } })).toBe(true);
47+
expect(isEqual({ a: { b: 1 } }, { a: { b: 2 } })).toBe(false);
48+
expect(isEqual({ a: { b: { c: 3 } } }, { a: { b: { c: 3 } } })).toBe(true);
49+
expect(isEqual({ a: { b: { c: 3 } } }, { a: { b: { c: 4 } } })).toBe(false);
50+
});
51+
52+
it("should handle objects with array values", () => {
53+
expect(isEqual({ a: [1, 2, 3] }, { a: [1, 2, 3] })).toBe(true);
54+
expect(isEqual({ a: [1, 2, 3] }, { a: [1, 2, 4] })).toBe(false);
55+
expect(isEqual({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] })).toBe(true);
56+
expect(isEqual({ a: [1, { b: 2 }] }, { a: [1, { b: 3 }] })).toBe(false);
57+
});
58+
59+
it("should handle arrays with object elements", () => {
60+
expect(isEqual([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 2 }])).toBe(true);
61+
expect(isEqual([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 3 }])).toBe(false);
62+
});
63+
64+
it("should handle RegExp objects", () => {
65+
expect(isEqual(/abc/g, /abc/g)).toBe(true);
66+
expect(isEqual(/abc/g, /abc/i)).toBe(false);
67+
expect(isEqual(/abc/g, /def/g)).toBe(false);
68+
});
69+
70+
it("should handle objects with custom valueOf method", () => {
71+
const date1 = new Date("2023-01-01");
72+
const date2 = new Date("2023-01-01");
73+
const date3 = new Date("2023-01-02");
74+
75+
expect(isEqual(date1, date2)).toBe(true);
76+
expect(isEqual(date1, date3)).toBe(false);
77+
});
78+
79+
it("should handle objects with custom toString method", () => {
80+
const obj1 = {
81+
toString() {
82+
return "custom string";
83+
},
84+
};
85+
86+
const obj2 = {
87+
toString() {
88+
return "custom string";
89+
},
90+
};
91+
92+
const obj3 = {
93+
toString() {
94+
return "different string";
95+
},
96+
};
97+
98+
expect(isEqual(obj1, obj2)).toBe(true);
99+
expect(isEqual(obj1, obj3)).toBe(false);
100+
});
101+
102+
it("should handle objects with different constructors", () => {
103+
class A {}
104+
class B {}
105+
106+
expect(isEqual(new A(), new A())).toBe(true);
107+
expect(isEqual(new A(), new B())).toBe(false);
108+
});
109+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2+
export function isEqual(a: any, b: any): boolean {
3+
if (a === b) {
4+
return true;
5+
}
6+
7+
if (a && b && typeof a === "object" && typeof b === "object") {
8+
if (a.constructor !== b.constructor) {
9+
return false;
10+
}
11+
12+
if (Array.isArray(a)) {
13+
if (a.length !== b.length) {
14+
return false;
15+
}
16+
return a.every((item, index) => isEqual(item, b[index]));
17+
}
18+
19+
if (a.constructor === RegExp) {
20+
return a.source === b.source && a.flags === b.flags;
21+
}
22+
23+
if (a.valueOf !== Object.prototype.valueOf) {
24+
return a.valueOf() === b.valueOf();
25+
}
26+
27+
if (a.toString !== Object.prototype.toString) {
28+
return a.toString() === b.toString();
29+
}
30+
const aKeys = Object.keys(a);
31+
32+
if (aKeys.length !== Object.keys(b).length) {
33+
return false;
34+
}
35+
36+
return aKeys.every((key) => Object.prototype.hasOwnProperty.call(b, key) && isEqual(a[key], b[key]));
37+
}
38+
39+
// true if both NaN, false otherwise
40+
return a !== a && b !== b;
41+
}

packages/ui/src/helpers/resolve-theme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { deepmerge } from "deepmerge-ts";
2-
import isEqual from "fast-deep-equal";
32
import { klona } from "klona/json";
43
import { useRef } from "react";
54
import { getPrefix } from "../store";
@@ -9,6 +8,7 @@ import { applyPrefixV3 } from "./apply-prefix-v3";
98
import { convertUtilitiesToV4 } from "./convert-utilities-to-v4";
109
import { deepMergeStrings } from "./deep-merge";
1110
import { getTailwindVersion } from "./get-tailwind-version";
11+
import { isEqual } from "./is-equal";
1212
import { twMerge } from "./tailwind-merge";
1313

1414
/**

0 commit comments

Comments
 (0)