-
Notifications
You must be signed in to change notification settings - Fork 8
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): deepFlatThenMap 신규 유틸 함수 추가 #294
Conversation
🦋 Changeset detectedLatest commit: 668ae5a The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 #294 +/- ##
==========================================
+ Coverage 96.91% 96.92% +0.01%
==========================================
Files 124 125 +1
Lines 1296 1301 +5
Branches 315 316 +1
==========================================
+ Hits 1256 1261 +5
Misses 34 34
Partials 6 6
|
3d23f8a
to
0d2d485
Compare
if (!iteratee) { | ||
return arr.flat(Infinity); | ||
} |
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.
iteratee가 없다면 그냥 flat을 호출합니다.
const recursiveFlatten = (arr: T[] | readonly T[]): U[] => { | ||
return arr.flatMap((item) => { | ||
if (isArray(item)) { | ||
return recursiveFlatten(item); | ||
} | ||
return iteratee(item as ExtractNestedArrayType<T>); | ||
}); | ||
}; | ||
|
||
return recursiveFlatten(arr); |
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.
@@ -0,0 +1,37 @@ | |||
import { ExtractNestedArrayType } from '@modern-kit/types'; |
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.
flatMapDeep에 대한 index파일은 jsx가 없어서 ts로 되어도 될것같습니다! 😄
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.
꼼꼼한 확인 감사합니다! 수정 반영했습니다 :)
expect(flattenArray).toEqual([1, 2, 3, 4, 5, 6]); | ||
expectTypeOf(flattenArray).toEqualTypeOf<number[]>(); | ||
}); | ||
|
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.
테스트코드 내에서 여러 타입을 가진 중첩배열에서도 ExtractNestedArrayType이 유효하게 타입을 추론해주었다는걸 검증하면 더 좋을것같다는 생각이 들었는데요!
const arr = [1, ['asdf', [true]]]
이런 형태처럼 중첩된 배열케이스를 만들고 타입추론이 잘 되는지 확인하는 테스트코드가 추가되면 어떨까요! 😄
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.
관련 테스트 코드도 추가했으며 동시에 ExtractNestedArrayType의 타입 테스트 코드도 함께 추가했습니다 :)
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.
전반적인 코드가 많이 간결해졌네요! 😂
이전 코드보다 간결해졌다보니 이해하는데에 오래걸리지 않고 기능이 직관적이라 더 좋아보입니다!
작업하시느라 너무 고생하셨습니다! 👍
@Sangminnn 1. 네이밍 변경이번에 제가 제안하는 함수는 사용 예제와 비교(vs lodash flatMapDeep)
const arr = [1, 2, [3, 4, [5, 6]]];
deepFlatThenMap(arr);
// type: number[]
// value: [1, 2, 3, 4, 5, 6]
const arr = [1, 2, [3, 4, [5, 6]]];
_.flatMapDeep(arr);
// type: number[]
// value: [1, 2, 3, 4, 5, 6]
const arr = [1, 2, [3, 4, [5, 6]]];
deepFlatThenMap(arr, (item) => ({ id: item }));
// type: { id: number }[]
// value: [{ id: 1}, { id: 2}, { id: 3}, { id: 4}, { id: 5}, { id: 6}];
const arr = [1, 2, [3, 4, [5, 6]]];
_.flatMapDeep(arr, (item) => ({ id: item }));
// type: { id: number | (number | number[])[] }[];
// value: [{ id: 1}, { id: 2}, { id: [3, 4, [5, 6]]}]; 위의 예제처럼 lodash의 flatMapDeep과는 동작의 차이가 있고, 이름이 같다면 혼란이 있을 수 있습니다.
2. 로직 변경기존 재귀를 돌면서 구현했던 로직을 벤치 마크 테스트 결과 해당 로직이 재귀를 도는 것보다 |
7427c79
to
668ae5a
Compare
Overview
Issue: #289
deepFlatThenMap은 모든 깊이의 배열을 평탄화 한 후 각 요소에 콜백 함수를 적용하는 함수입니다.
반면에 lodash의 flatMapDeep 은 먼저 각 요소에
map
을 적용한 후, 평탄화하는 함수로 동작상의 차이가 있습니다.Default
Iteratee
PR Checklist
Contributing Guide