Skip to content
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

Merged
merged 5 commits into from
Oct 8, 2024
Merged

Conversation

Gaic4o
Copy link
Contributor

@Gaic4o Gaic4o commented Oct 6, 2024

Overview

헬퍼 함수(processArray)와 사용자 지정 매핑 함수(iteratee)를 사용하여 두 배열의 대칭 차이(xor) 계산 함수 구현했습니다.

Issue: #438

PR Checklist

  • All tests pass.
  • All type checks pass.
  • I have read the Contributing Guide document.

@Gaic4o Gaic4o requested a review from ssi02014 as a code owner October 6, 2024 14:54
Copy link

changeset-bot bot commented Oct 6, 2024

🦋 Changeset detected

Latest commit: 7adb273

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modern-kit/utils Minor

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

Copy link

codecov bot commented Oct 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.54%. Comparing base (33cf068) to head (7adb273).
Report is 24 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            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              
Components Coverage Δ
@modern-kit/react 95.49% <91.20%> (+0.27%) ⬆️
@modern-kit/utils 100.00% <100.00%> (ø)

Comment on lines 12 to 27
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;
}
Copy link
Contributor

@ssi02014 ssi02014 Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
);
}

성능적으로는 좀 떨어질 수 있지만 기존에 구현된 함수를 기반으로 깔끔하게 구현할 수 있을 것 같은데 어떻게 생각하실까요 🤗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋아요, 수정해서 반영해 놓을게요!

@ssi02014 ssi02014 added feature 새로운 기능 추가 @modern-kit/utils @modern-kit/utils labels Oct 7, 2024
@Gaic4o Gaic4o requested a review from ssi02014 October 7, 2024 13:00
@ssi02014
Copy link
Contributor

ssi02014 commented Oct 7, 2024

@Gaic4o 혹시 useHover에 있는usePreservedCallback 경로 수정을 함께 진행해주실 수 있으실까요...!? 🙏
제가 다른 작업하면서 실수로 반영된 부분입니다 🥲

// as-is
import { usePreservedCallback } from 'hooks/usePreservedCallback';

// to-be
import { usePreservedCallback } from '../../hooks/usePreservedCallback';

@Gaic4o
Copy link
Contributor Author

Gaic4o commented Oct 7, 2024

@ssi02014 수정해서 반영했어요!

@ssi02014 ssi02014 merged commit a60dad3 into modern-agile-team:main Oct 8, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 새로운 기능 추가 @modern-kit/utils @modern-kit/utils
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants