Skip to content

Commit

Permalink
최초 과제 수행
Browse files Browse the repository at this point in the history
  • Loading branch information
BongjoonKim committed Dec 31, 2024
1 parent c023174 commit 354fb53
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 57 deletions.
70 changes: 68 additions & 2 deletions src/@lib/equalities/deepEquals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,69 @@
// export function deepEquals<T>(objA: T, objB: T): boolean {
// if (typeof objA === "object" && typeof objB === "object") {
// if (objA == null && objB == null) {
// return true;
// }
// if (Object.keys(objA).length === Object.keys(objB).length) {
// for (const key of Object.keys(objA)) {
// if (typeof objA[key] === "object" && typeof objB[key] === "object") {
// return deepEquals(objA[key], objB[key]);
// }
// if (objA[key] !== objB[key]) {
// return false;
// }
// }
// return true;
// } else {
// return false;
// }
// } else {
// return objA === objB;
// }
// }

export function deepEquals<T>(objA: T, objB: T): boolean {
return objA === objB;
}
// 기본 타입이거나 null/undefined인 경우
if (objA === objB) {
return true;
}

// null/undefined 체크
if (objA == null || objB == null) {
return false;
}

// 배열 체크
if (Array.isArray(objA) && Array.isArray(objB)) {
if (objA.length !== objB.length) {
return false;
}
for (let i = 0; i < objA.length; i++) {
if (!deepEquals(objA[i], objB[i])) {
return false;
}
}
return true;
}

// 객체 체크
if (typeof objA === "object" && typeof objB === "object") {
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);

if (keysA.length !== keysB.length) {
return false;
}

for (const key of keysA) {
if (!keysB.includes(key)) {
return false;
}
if (!deepEquals(objA[key], objB[key])) {
return false;
}
}
return true;
}

return false;
}
25 changes: 24 additions & 1 deletion src/@lib/equalities/shallowEquals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
export function shallowEquals<T>(objA: T, objB: T): boolean {
return objA === objB;

// 두 값이 정확히 같은지 확인(참조가 같은 경우)
if (objA === objB) {
return true;
}

if (objA === null || objB === null) {
return objA === objB;
}

if (typeof objA === "object" && typeof objB === "object") {
if (Object.keys(objA).length !== Object.keys(objB).length) {
return false;
} else {
for (const key of Object.keys(objA)) {
if (objA[key] !== objB[key]) {
return false;
}
}
return true;
}
} else {
return objA === objB;
}
}
14 changes: 13 additions & 1 deletion src/@lib/hocs/memo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,17 @@ export function memo<P extends object>(
Component: ComponentType<P>,
_equals = shallowEquals,
) {
return Component;
let oldProps = null;
let prevResult = null;
return (props) => {
if (!oldProps || !_equals(oldProps, props)) {
prevResult = Component(props);
}

// 현재 props를 이전 props로 저장
oldProps = props;

// 메모이제이션된 결과 반환
return prevResult;
}
}
6 changes: 4 additions & 2 deletions src/@lib/hooks/useCallback.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable @typescript-eslint/no-unused-vars,@typescript-eslint/no-unsafe-function-type */
import { DependencyList } from "react";
import {useMemo} from "./useMemo.ts";
import {deepEquals, shallowEquals} from "../equalities";
import {useRef} from "./useRef.ts";

export function useCallback<T extends Function>(
factory: T,
_deps: DependencyList,
) {
// 직접 작성한 useMemo를 통해서 만들어보세요.
return factory as T;
return useMemo(() => factory, _deps);
}
9 changes: 8 additions & 1 deletion src/@lib/hooks/useMemo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { DependencyList } from "react";
import { shallowEquals } from "../equalities";
import {useRef} from "./useRef.ts";

export function useMemo<T>(
factory: () => T,
_deps: DependencyList,
_equals = shallowEquals,
): T {
// 직접 작성한 useRef를 통해서 만들어보세요.
return factory();
const ref = useRef(0);
const result = useRef(0);
if (!_equals(ref.current, _deps)) {
ref.current = _deps;
result.current = factory();
}
return result.current;
}
5 changes: 4 additions & 1 deletion src/@lib/hooks/useRef.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {useState} from "react";

export function useRef<T>(initialValue: T): { current: T } {
// React의 useState를 이용해서 만들어보세요.
return { current: initialValue };
const [data, setData] = useState<any>({current : initialValue});
return data;
}
Loading

0 comments on commit 354fb53

Please sign in to comment.