Skip to content
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(countBy): Add countBy function #117

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions benchmarks/countBy.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { bench, describe } from 'vitest';
import { countBy as countByToolkit } from 'es-toolkit';
import { countBy as countByLodash } from 'lodash';

describe('countBy', () => {
bench('es-toolkit/countBy', () => {
countByToolkit([1.2, 2.4, 3.6, 2.2, 3.4, 3.6], (item: number) => {
return Math.floor(item).toString();
});
});

bench('lodash/countBy', () => {
countByLodash([1.2, 2.4, 3.6, 2.2, 3.4, 3.6], (item: number) => {
return Math.floor(item).toString()
raon0211 marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
30 changes: 30 additions & 0 deletions docs/ko/reference/array/countBy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# countBy

배열에 속해 있는 요소를 `mapper` 함수가 반환하는 값 기준으로 분류하고, 개수를 세요.

## 인터페이스

```typescript
function countBy<T>(arr: T[], transformer: (item: T) => string): Record<string, number>
raon0211 marked this conversation as resolved.
Show resolved Hide resolved
```

### 파라미터

- `arr` (`T[]`): 각 요소의 갯수를 세고자 하는 배열
- `transformer` (`(item: T) => string`): 각 요소를 key로 변환하는 함수
raon0211 marked this conversation as resolved.
Show resolved Hide resolved

### 반환 값

(`Record<string, number>`) 각 요소와 그 갯수를 key-value로 가지는 객체
raon0211 marked this conversation as resolved.
Show resolved Hide resolved

## 예시

```javascript
import {countBy} from 'es-toolkit/array';
raon0211 marked this conversation as resolved.
Show resolved Hide resolved

const array = [1, 2, 3, 4, 5, 6];
const result = countBy(array, x => x % 2 === 0 ? 'even' : 'odd');

console.log(result);
// 출력: { 'odd': 3, 'even': 3 }
```
31 changes: 31 additions & 0 deletions docs/reference/array/countBy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# countBy

Count the occurrences of each item in an array based on a transformation function.
raon0211 marked this conversation as resolved.
Show resolved Hide resolved

## Signature

```typescript
function countBy<T>(arr: T[], transformer: (item: T) => string): Record<string, number>
raon0211 marked this conversation as resolved.
Show resolved Hide resolved
```

### Parameters

- `arr` (`T[]`): The input array to count occurrences.
- `transformer` (`(item: T) => string`): The transformation function that maps each item to a string key.
raon0211 marked this conversation as resolved.
Show resolved Hide resolved

### Returns

(`Record<string, number>`) An object containing the count of each item based on the transformation function.

## Examples

```javascript
import {countBy} from 'es-toolkit/array';
raon0211 marked this conversation as resolved.
Show resolved Hide resolved

const array = [1, 2, 3, 4, 5, 6];
const result = countBy(array, x => x % 2 === 0 ? 'even' : 'odd');

console.log(result);
// Output: { 'odd': 3, 'even': 3 }
```

26 changes: 26 additions & 0 deletions src/array/countBy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from 'vitest';
import {countBy} from "./countBy.ts";

describe('countBy', () => {
it('should count the occurrences of each item in an array', () => {
const arr = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
const result = countBy(arr, String);

expect(result).toEqual({
'1': 2,
'2': 2,
'3': 2,
'4': 2,
'5': 2,
});
});
it('should count the occurrences of each item in an array that applied transformer', () => {
const arr = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
const result = countBy(arr, (item) => item % 2 === 0 ? 'even' : 'odd');

expect(result).toEqual({
'odd': 6,
'even': 4,
});
})
});
24 changes: 24 additions & 0 deletions src/array/countBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Count the occurrences of each item in an array
* 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
* 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.
*
* @param {T[]} arr - The input array to count occurrences.
* @param {(item: T) => string} transformer - The transformation function that maps each item to a string key.
* @returns {Record<string, number>} An object containing the transformed items as keys and the
* counts as values.
*/
export function countBy<T>(arr: T[], transformer: (item: T) => string): Record<string, number> {
raon0211 marked this conversation as resolved.
Show resolved Hide resolved
return arr.reduce((acc, item) => {
const key = transformer(item);
acc[key] = (acc[key] || 0) + 1;
return acc;
}, {} as Record<string, number>);
raon0211 marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions src/array/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { chunk } from './chunk.ts';
export { compact } from './compact.ts';
export { countBy } from './countBy.ts';
export { difference } from './difference.ts';
export { differenceBy } from './differenceBy.ts';
export { differenceWith } from './differenceWith.ts';
Expand Down
Loading