fix(countBy): Support nemeric and symbol keys in countBy (#433)

* fix(countBy): Support readonly array in `countBy`

* fix(countBy): Support numeric and symbol keys in `countBy`
This commit is contained in:
Dongho Kim 2024-08-28 23:20:44 +09:00 committed by GitHub
parent 8ffb6d1710
commit f85579bc39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 15 deletions

View File

@ -5,17 +5,17 @@
## 인터페이스 ## 인터페이스
```typescript ```typescript
function countBy<T>(arr: T[], mapper: (item: T) => string): Record<string, number>; function countBy<T, K extends PropertyKey>(arr: T[], mapper: (item: T) => K): Record<K, number>;
``` ```
### 파라미터 ### 파라미터
- `arr` (`T[]`): 요소의 갯수를 세고자 하는 배열. - `arr` (`T[]`): 요소의 갯수를 세고자 하는 배열.
- `mapper` (`(item: T) => string`): 요소를 분류할 기준이 되는 값을 반환하는 함수. - `mapper` (`(item: T) => K`): 요소를 분류할 기준이 되는 값을 반환하는 함수.
### 반환 값 ### 반환 값
(`Record<string, number>`) 각 요소가 분류별로 몇 개 있는지를 계산한 객체. (`Record<K, number>`) 각 요소가 분류별로 몇 개 있는지를 계산한 객체.
## 예시 ## 예시

View File

@ -5,17 +5,17 @@ Count the occurrences of each item in an array based on a `mapper` function.
## Signature ## Signature
```typescript ```typescript
function countBy<T>(arr: T[], mapper: (item: T) => string): Record<string, number>; function countBy<T, K extends PropertyKey>(arr: T[], mapper: (item: T) => K): Record<K, number>;
``` ```
### Parameters ### Parameters
- `arr` (`T[]`): The input array to count occurrences. - `arr` (`T[]`): The input array to count occurrences.
- `mapper` (`(item: T) => string`): The transformation function that maps each item to a string key. - `mapper` (`(item: T) => K`): The transformation function that maps each item to a key.
### Returns ### Returns
(`Record<string, number>`) An object containing the count of each item based on the transformation function. (`Record<K, number>`) An object containing the count of each item based on the transformation function.
## Examples ## Examples

View File

@ -5,17 +5,17 @@
## 签名 ## 签名
```typescript ```typescript
function countBy<T>(arr: T[], mapper: (item: T) => string): Record<string, number>; function countBy<T, K extends PropertyKey>(arr: T[], mapper: (item: T) => K): Record<K, number>;
``` ```
### 参数 ### 参数
- `arr` (`T[]`): 输入数组,用于统计每个项目的出现次数。 - `arr` (`T[]`): 输入数组,用于统计每个项目的出现次数。
- `mapper` (`(item: T) => string`): 将每个项目映射到字符串键的转换函数。 - `mapper` (`(item: T) => K`): 将每个项目映射到字符串键的转换函数。
### 返回值 ### 返回值
(`Record<string, number>`) 包含基于转换函数的每个项目计数的对象。 (`Record<K, number>`) 包含基于转换函数的每个项目计数的对象。
## 示例 ## 示例

View File

@ -3,20 +3,20 @@
* based on a transformation function. * based on a transformation function.
* *
* This function takes an array and a transformation function * This function takes an array and a transformation function
* that converts each item in the array to a string. It then * that converts each item in the array to a key. It then
* counts the occurrences of each transformed item and returns * counts the occurrences of each transformed item and returns
* an object with the transformed items as keys and the counts * an object with the transformed items as keys and the counts
* as values. * as values.
* *
* @template T - The type of the items in the input array. * @template T - The type of the items in the input array.
* * @template K - The type of keys.
* @param {T[]} arr - The input array to count occurrences. * @param {T[]} arr - The input array to count occurrences.
* @param {(item: T) => string} mapper - The transformation function that maps each item to a string key. * @param {(item: T) => K} mapper - The transformation function that maps each item to a key.
* @returns {Record<string, number>} An object containing the transformed items as keys and the * @returns {Record<K, number>} An object containing the transformed items as keys and the
* counts as values. * counts as values.
*/ */
export function countBy<T>(arr: T[], mapper: (item: T) => string): Record<string, number> { export function countBy<T, K extends PropertyKey>(arr: readonly T[], mapper: (item: T) => K): Record<K, number> {
const result: Record<string, number> = {}; const result = {} as Record<K, number>;
for (const item of arr) { for (const item of arr) {
const key = mapper(item); const key = mapper(item);