-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): invert 한글 화 및 스펙 변경 (#573)
* fix(utils): groupBy 한글화 * fix(utils): invert 한글 화 및 스펙 변경
- Loading branch information
Showing
13 changed files
with
229 additions
and
189 deletions.
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,5 @@ | ||
--- | ||
'@modern-kit/utils': patch | ||
--- | ||
|
||
fix(utils): invert 스펙 변경 - @ssi02014 |
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
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 { bench, describe } from 'vitest'; | ||
import { groupBy } from '.'; | ||
import { groupBy as groupByLodash } from 'lodash-es'; | ||
|
||
describe('groupBy', () => { | ||
const array = [ | ||
{ category: 'fruit', name: 'apple' }, | ||
{ category: 'fruit', name: 'banana' }, | ||
{ category: 'vegetable', name: 'carrot' }, | ||
{ category: 'fruit', name: 'pear' }, | ||
{ category: 'vegetable', name: 'broccoli' }, | ||
]; | ||
|
||
bench('modern-kit/groupBy', () => { | ||
groupBy(array, (item) => item.category); | ||
}); | ||
|
||
bench('lodash/groupBy', () => { | ||
groupByLodash(array, (item) => item.category); | ||
}); | ||
}); |
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,55 @@ | ||
/** | ||
* @description 배열을 주어진 기준에 따라 그룹화합니다. | ||
* | ||
* `iteratee` 함수를 전달하여 반환된 키를 기준으로 항목들을 그룹화합니다. 각 키는 그룹화된 항목 배열을 포함하는 객체의 속성으로 할당됩니다. | ||
* | ||
* 24년 3월 js 신규 스펙으로 groupBy가 제공되지만 버전 호환성을 위해 해당 함수가 사용 될 수 있습니다. | ||
* @see https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy | ||
* | ||
* @template T - 배열 항목의 타입을 나타냅니다. | ||
* @template K - 그룹화 기준으로 사용할 키의 타입입니다. | ||
* @param {T[] | readonly T[]} arr - 그룹화를 진행할 항목들의 배열입니다. | ||
* @param {(item: T) => K} iteratee - 각 항목에 대해 그룹화 키를 반환하는 함수입니다. | ||
* @returns {Record<K, T[]>} - 각 키가 그룹화된 항목 배열을 가지는 객체를 반환합니다. | ||
* | ||
* @example | ||
* const array = [ | ||
* { category: 'fruit', name: 'apple' }, | ||
* { category: 'fruit', name: 'banana' }, | ||
* { category: 'vegetable', name: 'carrot' }, | ||
* { category: 'fruit', name: 'pear' }, | ||
* { category: 'vegetable', name: 'broccoli' }, | ||
* ]; | ||
* | ||
* const group = groupBy(array, (item) => item.category); | ||
* // { | ||
* // fruit: [ | ||
* // { category: 'fruit', name: 'apple' }, | ||
* // { category: 'fruit', name: 'banana' }, | ||
* // { category: 'fruit', name: 'pear' }, | ||
* // ], | ||
* // vegetable: [ | ||
* // { category: 'vegetable', name: 'carrot' }, | ||
* // { category: 'vegetable', name: 'broccoli' }, | ||
* // ], | ||
* // } | ||
*/ | ||
export function groupBy<T, K extends PropertyKey>( | ||
arr: T[] | readonly T[], | ||
iteratee: (item: T) => K | ||
): Record<K, T[]> { | ||
const group = {} as Record<K, T[]>; | ||
|
||
for (let i = 0; i < arr.length; i++) { | ||
const item = arr[i]; | ||
const key = iteratee(item); | ||
|
||
if (group[key] == null) { | ||
group[key] = []; | ||
} | ||
|
||
group[key].push(item); | ||
} | ||
|
||
return group; | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.