Skip to content

Commit c977d89

Browse files
committed
feat: shallowEquals 개발
1 parent d8192aa commit c977d89

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1-
export const shallowEquals = (a: unknown, b: unknown) => {
2-
return a === b;
3-
};
1+
export function shallowEquals(objA: unknown, objB: unknown): boolean {
2+
if (objA === objB) return true;
3+
4+
if (objA == null || objB == null || typeof objA !== "object" || typeof objB !== "object") {
5+
return false;
6+
}
7+
8+
const keysOfA = Object.keys(objA);
9+
const keysOfB = Object.keys(objB);
10+
11+
if (keysOfA.length !== keysOfB.length) return false;
12+
13+
for (const key of keysOfA) {
14+
if ((objA as Record<string, unknown>)[key] !== (objB as Record<string, unknown>)[key]) {
15+
return false;
16+
}
17+
}
18+
19+
return true;
20+
}

0 commit comments

Comments
 (0)