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(minBy): Add minBy function that select element that have min value by given condition in array #71

Merged
merged 6 commits into from
Jun 17, 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
23 changes: 23 additions & 0 deletions benchmarks/minBy.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { bench, describe } from 'vitest';
import { minBy as minByToolkit } from 'es-toolkit';
import { minBy as minByLodash } from 'lodash';

describe('minBy', () => {
bench('es-toolkit', () => {
const people = [
{ name: 'Mark', age: 30 },
{ name: 'Nunu', age: 20 },
{ name: 'Overmars', age: 35 },
];
minByToolkit(people, person => person.age);
});

bench('lodash', () => {
const people = [
{ name: 'Mark', age: 30 },
{ name: 'Nunu', age: 20 },
{ name: 'Overmars', age: 35 },
];
minByLodash(people, person => person.age);
});
});
27 changes: 27 additions & 0 deletions docs/ko/reference/math/minBy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# maxBy

주어진 배열 내의 요소들 중에서 조건에 따라 최솟값을 가지는 첫 번째 요소를 선택하는 함수에요.

배열이 비어있지 않다면 조건에 따라 최솟값을 가지는 첫 번째 요소를 반환하고, 비어있다면 `undefined`를 반환해요.

## 인터페이스

```typescript
function minBy<T>(elements: T[], selector: (element: T) => number): T
```

### 파라미터

- `elements`: 검색할 요소들의 배열
- `selector`: 요소를 받아서 객체의 속성을 반환하는 함수

### 반환값

함수의 최솟값을 가지는 배열의 첫 번째 요소. 만약 배열이 비어있다면 `undefined`를 반환해요.

### 예시

```typescript
maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a) // 결과: { a: 3 }
maxBy([], x => x.a) // 결과: undefined
```
27 changes: 27 additions & 0 deletions docs/reference/math/minBy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# minBy

Selects the first element of a list that has the minimum value of a function.

If the list is empty, returns `undefined`.

## Signature

```typescript
function minBy<T>(elements: T[], selector: (element: T) => number): T
```

### Parameters

- `elements`: an array of elements to search through.
- `selector`: a function that takes an element and returns a number that the property of the object.

### Returns

The first element of the list that has the minimum value of the function. If the list is empty, returns `undefined`.

### Example

```typescript
minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a) // Returns: { a: 1 }
minBy([], x => x.a) // Returns: undefined
```
1 change: 1 addition & 0 deletions src/math/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { random } from './random';
export { randomInt } from './randomInt';
export { round } from './round';
export { sum } from './sum';
export { minBy } from './minBy';
39 changes: 39 additions & 0 deletions src/math/minBy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from "vitest";
import { minBy } from './minBy';

describe('minBy', () => {
it('minBy selects one max value in array', () => {
const people = [
{ name: 'Mark', age: 30 },
{ name: 'Nunu', age: 20 },
{ name: 'Overmars', age: 35 },
];
const result = minBy(people, person => person.age);
expect(result).toEqual({ name: 'Nunu', age: 20 });
});

it('if there are two max values, first one is selected', () => {
const people = [
{ name: 'Mark', age: 30 },
{ name: 'Nunu', age: 20 },
{ name: 'Overmars', age: 20 },
];
const result = minBy(people, person => person.age);
expect(result).toEqual({ name: 'Nunu', age: 20 });
});

it('if array is single-element, return unique element of array', () => {
const people = [
{ name: 'Mark', age: 25 },
];
const result = minBy(people, person => person.age);
expect(result).toEqual({ name: 'Mark', age: 25 });
});

it('if array is empty, return undefined', () => {
type Person = { name: string, age: number };
const people: Person[] = [];
const result = minBy(people, person => person.age);
expect(result).toBeUndefined();
});
});
25 changes: 25 additions & 0 deletions src/math/minBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Returns the minimum value in an array of numbers.
*
* This function takes an array of elements and a selector function, and returns the element with the minimum value according to the selector.
*
* If the array is empty, the function returns `undefined`.
*
* @param elements An array of elements.
* @param selector A function that selects a number from an element.
*/
export function minBy<T>(elements: T[], selector: (element: T) => number): T {

let minElement = elements[0];
let min = Infinity;

for (const element of elements) {
const value = selector(element);
if (value < min) {
min = value;
minElement = element;
}
}

return minElement;
}