-
Notifications
You must be signed in to change notification settings - Fork 56
[6팀 정용준] Chapter 1-3. React, Beyond the Basics #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
f8045aa
6b6a524
d313784
a683aa3
40dc996
58e135f
bb47b7c
c2d526d
ec04970
1e41ca7
e0021cf
7f2441f
66fe47b
ce447b9
d650650
e80b884
9a288da
395c38a
062575f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,34 @@ | ||
| import { getObjectKeys, isArray, isObject, isPrimitive } from "./utils"; | ||
|
|
||
| export const deepEquals = (a: unknown, b: unknown) => { | ||
| if (typeof a !== typeof b) return false; | ||
|
|
||
| if (isPrimitive(a) && isPrimitive(b)) { | ||
| return a === b; | ||
| } | ||
|
|
||
| if (isArray(a) && isArray(b)) { | ||
| if (a.length !== b.length) return false; | ||
|
|
||
| for (let i = 0; i < a.length; i++) { | ||
| if (!deepEquals(a[i], b[i])) return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| if (isObject(a) && isObject(b)) { | ||
| const keysA = getObjectKeys(a); | ||
| const keysB = getObjectKeys(b); | ||
|
|
||
| if (keysA.length !== keysB.length) return false; | ||
|
|
||
| for (const key of keysA) { | ||
| if (!deepEquals(a[key], b[key])) return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return a === b; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,34 @@ | ||
| import { getObjectKeys, isArray, isObject, isPrimitive } from "./utils"; | ||
|
|
||
| export const shallowEquals = (a: unknown, b: unknown) => { | ||
| if (typeof a !== typeof b) return false; | ||
|
|
||
| if (isPrimitive(a) && isPrimitive(b)) { | ||
| return a === b; | ||
| } | ||
|
|
||
| if (isArray(a) && isArray(b)) { | ||
| if (a.length !== b.length) return false; | ||
|
|
||
| for (let i = 0; i < a.length; i++) { | ||
| if (a[i] !== b[i]) return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| if (isObject(a) && isObject(b)) { | ||
| const keysA = getObjectKeys(a); | ||
| const keysB = getObjectKeys(b); | ||
|
|
||
| if (keysA.length !== keysB.length) return false; | ||
|
|
||
| for (const key of keysA) { | ||
| if (a[key] !== b[key]) return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return a === b; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export type Primitive = string | number | boolean | null | undefined | bigint | symbol; | ||
|
|
||
| export const isArray = (value: unknown): value is unknown[] => Array.isArray(value); | ||
|
|
||
| export const isObject = (value: unknown): value is object => typeof value === "object" && value !== null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 객체를 판단하는 로직에서 엣지 케이스가 있을 것 같아요!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 민재님 말씀대로 Plain object로 의도하고 작성해야겠네요. 혹시 제가 얘기한게 민재님 리뷰의 의도와 맞을까요? |
||
|
|
||
| export const isPrimitive = (value: unknown): value is Primitive => typeof value !== "object" || value === null; | ||
|
|
||
| export const getObjectKeys = <T extends object>(o: T): (keyof T)[] => Object.keys(o) as (keyof T)[]; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utils로 타입 유틸 함수들을 잘 분리해두셔서 전체적인 가독성과 재사용성이 좋아진 것 같아요!