From f85579bc394f47faa4a7b37d9d69d9722db56bf0 Mon Sep 17 00:00:00 2001 From: Dongho Kim <70563791+mass2527@users.noreply.github.com> Date: Wed, 28 Aug 2024 23:20:44 +0900 Subject: [PATCH] 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` --- docs/ko/reference/array/countBy.md | 6 +++--- docs/reference/array/countBy.md | 6 +++--- docs/zh_hans/reference/array/countBy.md | 6 +++--- src/array/countBy.ts | 12 ++++++------ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/docs/ko/reference/array/countBy.md b/docs/ko/reference/array/countBy.md index f9c4e5ae..d2d5e700 100644 --- a/docs/ko/reference/array/countBy.md +++ b/docs/ko/reference/array/countBy.md @@ -5,17 +5,17 @@ ## 인터페이스 ```typescript -function countBy(arr: T[], mapper: (item: T) => string): Record; +function countBy(arr: T[], mapper: (item: T) => K): Record; ``` ### 파라미터 - `arr` (`T[]`): 요소의 갯수를 세고자 하는 배열. -- `mapper` (`(item: T) => string`): 요소를 분류할 기준이 되는 값을 반환하는 함수. +- `mapper` (`(item: T) => K`): 요소를 분류할 기준이 되는 값을 반환하는 함수. ### 반환 값 -(`Record`) 각 요소가 분류별로 몇 개 있는지를 계산한 객체. +(`Record`) 각 요소가 분류별로 몇 개 있는지를 계산한 객체. ## 예시 diff --git a/docs/reference/array/countBy.md b/docs/reference/array/countBy.md index 986b7a38..10339120 100644 --- a/docs/reference/array/countBy.md +++ b/docs/reference/array/countBy.md @@ -5,17 +5,17 @@ Count the occurrences of each item in an array based on a `mapper` function. ## Signature ```typescript -function countBy(arr: T[], mapper: (item: T) => string): Record; +function countBy(arr: T[], mapper: (item: T) => K): Record; ``` ### Parameters - `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 -(`Record`) An object containing the count of each item based on the transformation function. +(`Record`) An object containing the count of each item based on the transformation function. ## Examples diff --git a/docs/zh_hans/reference/array/countBy.md b/docs/zh_hans/reference/array/countBy.md index a88ac5a2..1bccee28 100644 --- a/docs/zh_hans/reference/array/countBy.md +++ b/docs/zh_hans/reference/array/countBy.md @@ -5,17 +5,17 @@ ## 签名 ```typescript -function countBy(arr: T[], mapper: (item: T) => string): Record; +function countBy(arr: T[], mapper: (item: T) => K): Record; ``` ### 参数 - `arr` (`T[]`): 输入数组,用于统计每个项目的出现次数。 -- `mapper` (`(item: T) => string`): 将每个项目映射到字符串键的转换函数。 +- `mapper` (`(item: T) => K`): 将每个项目映射到字符串键的转换函数。 ### 返回值 -(`Record`) 包含基于转换函数的每个项目计数的对象。 +(`Record`) 包含基于转换函数的每个项目计数的对象。 ## 示例 diff --git a/src/array/countBy.ts b/src/array/countBy.ts index 60bd2c34..cbd13e17 100644 --- a/src/array/countBy.ts +++ b/src/array/countBy.ts @@ -3,20 +3,20 @@ * based on 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 * an object with the transformed items as keys and the counts * as values. * * @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 {(item: T) => string} mapper - The transformation function that maps each item to a string key. - * @returns {Record} An object containing the transformed items as keys and the + * @param {(item: T) => K} mapper - The transformation function that maps each item to a key. + * @returns {Record} An object containing the transformed items as keys and the * counts as values. */ -export function countBy(arr: T[], mapper: (item: T) => string): Record { - const result: Record = {}; +export function countBy(arr: readonly T[], mapper: (item: T) => K): Record { + const result = {} as Record; for (const item of arr) { const key = mapper(item);