-
Notifications
You must be signed in to change notification settings - Fork 14
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): chunk 유틸 함수 구현 #184
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8aa958e
feat: 정수, 자연수 타입 추가
Collection50 d815021
feat: chunk 유틸 함수 구현
Collection50 bf4b603
fix: chunk의 2번째 인수의 제네릭 타입 추가
Collection50 0599eb9
feat: 범자연수 타입인 WholeNumber 추가
Collection50 c48fcf0
fix: array.length, size가 0인 경우, size >= array.length인 경우의 early retur…
Collection50 229b129
test: 테스트 코드를 한글에서 영어로 변경
Collection50 338406f
fix: size가 NaN인지 확인하는 early return 구문 추가
Collection50 4fc45df
fix: size가 NaN인 경우 빈 배열을 반환하도록 변경
Collection50 471f39c
chore: Create loud-zebras-shave.md
ssi02014 b48d5c8
docs: chunk 유틸 함수의 문서 작성
Collection50 e61b7c0
fix: 불필요한 NaN 타입 제거
Collection50 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@modern-kit/types": minor | ||
"@modern-kit/utils": minor | ||
--- | ||
|
||
feat(utils): chunk 유틸 함수 추가 및 Interger, NaturalNumber WholeNumber 유틸 타입 추가 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# chunk | ||
|
||
주어진 배열을 지정된 `size`만큼의 작은 배열로 나누어 반환하는 유틸리티 함수입니다. | ||
`size === 0` 이거나 배열이 비어있는 경우 빈 배열을 반환하며, `size <= array.length`인 경우 전체 배열을 하나의 배열로 감싸서 반환합니다. | ||
|
||
## Code | ||
|
||
[🔗 실제 구현 코드 확인](https://github.com/modern-agile-team/modern-kit/blob/main/packages/utils/src/array/chunk/index.ts) | ||
|
||
## Interface | ||
|
||
```ts title="typescript" | ||
const chunk = <T, U extends number>( | ||
array: T[], | ||
size: NaturalNumber<U> | ||
): T[][] | ||
``` | ||
|
||
## Usage | ||
|
||
```ts title="typescript" | ||
import { chunk } from '@modern-kit/utils'; | ||
|
||
const array1 = [1, 2, 3, 4, 5]; | ||
const size1 = 2; | ||
chunk(array1, size1); // [[1, 2], [3, 4], [5]] | ||
|
||
const array2 = ['a', 'b', 'c', 'd']; | ||
const size2 = 3; | ||
chunk(array2, size2); // [['a', 'b', 'c'], ['d']] | ||
|
||
const array3 = [1, 2, 3, 4]; | ||
const size3 = 5; | ||
chunk(array3, size3); // [[1, 2, 3, 4]] | ||
|
||
const array4 = []; | ||
const size4 = 2; | ||
chunk(array4, size4); // [] | ||
``` | ||
|
||
## Caveats | ||
`size`는 `1` 이상의 자연수여야 합니다. `NaturalNumber<U>` 타입을 사용하여 이를 강제합니다. | ||
`size === 0`이거나 `size === NaN`일 경우 빈 배열을 반환합니다. | ||
`size < array.length`인 경우 전체 배열을 하나의 배열로 감싸서 반환합니다. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type Integer<T extends number> = `${T}` extends `${string}.${string}` | ||
? never | ||
: T; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Integer } from 'Integer'; | ||
|
||
export type NaturalNumber<T extends number> = Integer<T> extends never | ||
? never | ||
: `${T}` extends `-${string}` | '0' | ||
? never | ||
: T; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Integer } from 'Integer'; | ||
|
||
export type WholeNumber<T extends number> = Integer<T> extends never | ||
? never | ||
: `${T}` extends `-${string}` | ||
? never | ||
: T; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { chunk } from '../chunk'; | ||
|
||
describe('chunk', () => { | ||
it('splits the array according to the second parameter', () => { | ||
const arr1 = [1, 2, 3, 4, 5, 6]; | ||
expect(chunk(arr1, 1)).toEqual([[1], [2], [3], [4], [5], [6]]); | ||
expect(chunk(arr1, 2)).toEqual([ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
]); | ||
|
||
const arr2 = [1, 2, 3, 4, 5]; | ||
expect(chunk(arr2, 3)).toEqual([ | ||
[1, 2, 3], | ||
[4, 5], | ||
]); | ||
}); | ||
|
||
it('returns an array of each element when the second parameter is 1', () => { | ||
const arr = [1, 2, 3, 4, 5, 6]; | ||
expect(chunk(arr, 1)).toEqual([[1], [2], [3], [4], [5], [6]]); | ||
}); | ||
|
||
it('returns an empty array when given an empty array', () => { | ||
const arr = [] as []; | ||
expect(chunk(arr, 3)).toEqual([]); | ||
}); | ||
|
||
it('returns the array as is if the chunk size is greater than the length of the array', () => { | ||
const arr = [1, 2]; | ||
expect(chunk(arr, 3)).toEqual([arr]); | ||
}); | ||
|
||
it('returns an empty array if the second parameter is 0', () => { | ||
const arr = [1, 2]; | ||
expect(chunk(arr, 0 as number)).toEqual([]); | ||
}); | ||
|
||
it('returns an empty array if the second parameter is NaN', () => { | ||
const arr = [1, 2]; | ||
expect(chunk(arr, NaN)).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NaturalNumber } from '@modern-kit/types'; | ||
|
||
export const chunk = <T, U extends number>( | ||
array: T[], | ||
size: NaturalNumber<U> | ||
): T[][] => { | ||
if (array.length === 0 || Number.isNaN(size) || size === 0) { | ||
return []; | ||
} | ||
|
||
if (size >= array.length) { | ||
return [array]; | ||
} | ||
|
||
return array.reduce((result, _, index) => { | ||
if (index % size === 0) { | ||
result.push(array.slice(index, index + size)); | ||
} | ||
return result; | ||
}, [] as T[][]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './countOccurrencesInArray'; | ||
export * from './excludeElements'; | ||
export * from './chunk'; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Integer, NaturalNumber 모두 멋진 타입이네요!
0
을 포함하는 범 자연수 Whole Number도 있으면 좋겠네요 🤗