-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat(utils): xor 유틸 함수 추가 #500
Conversation
🦋 Changeset detectedLatest commit: 7adb273 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #500 +/- ##
==========================================
+ Coverage 97.41% 97.54% +0.13%
==========================================
Files 164 170 +6
Lines 1470 1508 +38
Branches 361 381 +20
==========================================
+ Hits 1432 1471 +39
+ Misses 34 33 -1
Partials 4 4
|
export function xor<T>( | ||
array1: T[] | readonly T[], | ||
array2: T[] | readonly T[], | ||
iteratee?: (item: T) => string | number | ||
): T[] { | ||
const set = new Set<string | number>(); | ||
const result: T[] = []; | ||
|
||
function processArray(array: T[] | readonly T[]) { | ||
for (const item of array) { | ||
const key = iteratee ? iteratee(item) : (item as string | number); | ||
|
||
if (set.has(key)) { | ||
set.delete(key); | ||
const index = result.findIndex( | ||
(r) => (iteratee ? iteratee(r) : (r as string | number)) === key | ||
); | ||
if (index !== -1) { | ||
result.splice(index, 1); | ||
} | ||
} else { | ||
set.add(key); | ||
result.push(item); | ||
} | ||
} | ||
} | ||
|
||
processArray(array1); | ||
processArray(array2); | ||
|
||
return result; | ||
} |
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.
export function xor<T>( | |
array1: T[] | readonly T[], | |
array2: T[] | readonly T[], | |
iteratee?: (item: T) => string | number | |
): T[] { | |
const set = new Set<string | number>(); | |
const result: T[] = []; | |
function processArray(array: T[] | readonly T[]) { | |
for (const item of array) { | |
const key = iteratee ? iteratee(item) : (item as string | number); | |
if (set.has(key)) { | |
set.delete(key); | |
const index = result.findIndex( | |
(r) => (iteratee ? iteratee(r) : (r as string | number)) === key | |
); | |
if (index !== -1) { | |
result.splice(index, 1); | |
} | |
} else { | |
set.add(key); | |
result.push(item); | |
} | |
} | |
} | |
processArray(array1); | |
processArray(array2); | |
return result; | |
} | |
export function xor<T, U>( | |
arr1: T[] | readonly T[], | |
arr2: T[] | readonly T[], | |
iteratee?: (item: T) => U | |
): T[] { | |
return difference( | |
union(arr1, arr2, iteratee), | |
intersection(arr1, arr2, iteratee), | |
iteratee | |
); | |
} |
성능적으로는 좀 떨어질 수 있지만 기존에 구현된 함수를 기반으로 깔끔하게 구현할 수 있을 것 같은데 어떻게 생각하실까요 🤗
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.
좋아요, 수정해서 반영해 놓을게요!
@Gaic4o 혹시 useHover에 있는usePreservedCallback 경로 수정을 함께 진행해주실 수 있으실까요...!? 🙏 // as-is
import { usePreservedCallback } from 'hooks/usePreservedCallback';
// to-be
import { usePreservedCallback } from '../../hooks/usePreservedCallback'; |
@ssi02014 수정해서 반영했어요! |
Overview
헬퍼 함수(
processArray
)와 사용자 지정 매핑 함수(iteratee
)를 사용하여 두 배열의 대칭 차이(xor) 계산 함수 구현했습니다.Issue: #438
PR Checklist