feat(max, min): Add support for max & min in es-toolkit/compat

This commit is contained in:
raon0211 2024-07-19 00:00:57 +09:00
parent cd60ee8f6d
commit e1e6e38206
20 changed files with 285 additions and 11 deletions

View File

@ -0,0 +1,13 @@
import { bench, describe } from 'vitest';
import { max as maxToolkit } from 'es-toolkit/compat';
import { max as maxLodash } from 'lodash';
describe('max', () => {
bench('es-toolkit/max', () => {
maxToolkit([1, 2, 3]);
});
bench('lodash/max', () => {
maxLodash([1, 2, 3]);
});
});

View File

@ -0,0 +1,13 @@
import { bench, describe } from 'vitest';
import { min as minToolkit } from 'es-toolkit/compat';
import { min as minLodash } from 'lodash';
describe('min', () => {
bench('es-toolkit/min', () => {
minToolkit([1, 2, 3]);
});
bench('lodash/min', () => {
minLodash([1, 2, 3]);
});
});

View File

@ -74,6 +74,8 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'keyBy', link: '/reference/array/keyBy' },
{ text: 'minBy', link: '/reference/array/minBy' },
{ text: 'maxBy', link: '/reference/array/maxBy' },
{ text: 'min (compat)', link: '/reference/compat/array/min' },
{ text: 'max (compat)', link: '/reference/compat/array/max' },
{ text: 'orderBy', link: '/reference/array/orderBy' },
{ text: 'partition', link: '/reference/array/partition' },
{ text: 'sample', link: '/reference/array/sample' },

View File

@ -82,6 +82,8 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'keyBy', link: '/ko/reference/array/keyBy' },
{ text: 'minBy', link: '/ko/reference/array/minBy' },
{ text: 'maxBy', link: '/ko/reference/array/maxBy' },
{ text: 'min (호환성)', link: '/ko/reference/compat/array/min' },
{ text: 'max (호환성)', link: '/ko/reference/compat/array/max' },
{ text: 'orderBy', link: '/ko/reference/array/orderBy' },
{ text: 'partition', link: '/ko/reference/array/partition' },
{ text: 'sample', link: '/ko/reference/array/sample' },

View File

@ -70,6 +70,8 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'keyBy', link: '/zh_hans/reference/array/keyBy' },
{ text: 'minBy', link: '/zh_hans/reference/array/minBy' },
{ text: 'maxBy', link: '/zh_hans/reference/array/maxBy' },
{ text: 'min (兼容性)', link: '/zh_hans/reference/compat/array/min' },
{ text: 'max (兼容性)', link: '/zh_hans/reference/compat/array/max' },
{ text: 'orderBy', link: '/zh_hans/reference/array/orderBy' },
{ text: 'partition', link: '/zh_hans/reference/array/partition' },
{ text: 'sample', link: '/zh_hans/reference/array/sample' },

View File

@ -2,8 +2,6 @@
함수가 반환하는 값을 기준으로, 배열에서 최솟값을 가지는 요소를 반환해요.
배열이 비어 있다면, `undefined`를 반환해요.
## 인터페이스
```typescript
@ -17,9 +15,9 @@ function minBy<T>(items: T[], getValue: (element: T) => number): T;
### 반환 값
(`T`): `getValue` 함수를 기준으로, 배열에서 최솟값을 가지는 요소. 배열이 비어 있다면 `undefined`를 반환해요.
(`T`): `getValue` 함수를 기준으로, 배열에서 최솟값을 가지는 요소.
### Example
### 예시
```typescript
minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }

View File

@ -0,0 +1,26 @@
# max
배열에서 최댓값을 가지는 요소를 반환해요.
배열이 비어 있다면, `undefined`를 반환해요.
## 인터페이스
```typescript
function max<T>(items: T[]): T;
```
### 파라미터
- `items` (`T[]`): 최댓값을 가지는 요소를 찾을 배열.
### 반환 값
(`T`): 배열에서 최솟값을 가지는 요소. 배열이 비어 있다면 `undefined`를 반환해요.
### 예시
```typescript
max([1, 2, 3]); // Returns: 3
max(['a', 'b']); // Returns: 'b'
```

View File

@ -0,0 +1,26 @@
# min
배열에서 최솟값을 가지는 요소를 반환해요.
배열이 비어 있다면, `undefined`를 반환해요.
## 인터페이스
```typescript
function min<T>(items: T[]): T;
```
### 파라미터
- `items` (`T[]`): 최솟값을 가지는 요소를 찾을 배열.
### 반환 값
(`T`): 배열에서 최솟값을 가지는 요소. 배열이 비어 있다면 `undefined`를 반환해요.
### 예시
```typescript
min([1, 2, 3]); // Returns: 1
min(['a', 'b']); // Returns: 'a'
```

View File

@ -2,8 +2,6 @@
Finds the element in an array that has the maximum value when applying the `getValue` function to each element.
If the list is empty, returns `undefined`.
## Signature
```typescript
@ -17,7 +15,7 @@ function maxBy<T>(items: T[], getValue: (item: T) => number): T;
### Returns
The element with the maximum value as determined by the `getValue` function.
(`T`): The element with the maximum value as determined by the `getValue` function.
### Example

View File

@ -2,8 +2,6 @@
Finds the element in an array that has the minimum value when applying the `getValue` function to each element.
If the list is empty, returns `undefined`.
## Signature
```typescript
@ -17,7 +15,7 @@ function minBy<T>(items: T[], getValue: (item: T) => number): T;
### Returns
The element with the minimum value as determined by the `getValue` function.
(`T`): The element with the minimum value as determined by the `getValue` function.
### Example

View File

@ -0,0 +1,26 @@
# max
Finds the element in an array that has the maximum value.
If the list is empty, returns `undefined`.
## Signature
```typescript
function max<T>(items: T[]): T;
```
### Parameters
- `items` (`T[]`): The array of elements to search.
### Returns
(`T`): The element with the maximum value.
### Example
```typescript
max([1, 2, 3]); // Returns: 3
max(['a', 'b']); // Returns: 'b'
```

View File

@ -0,0 +1,26 @@
# min
Finds the element in an array that has the minimum value.
If the list is empty, returns `undefined`.
## Signature
```typescript
function min<T>(items: T[]): T;
```
### Parameters
- `items` (`T[]`): The array of elements to search.
### Returns
(`T`): The element with the minimum value.
### Example
```typescript
min([1, 2, 3]); // Returns: 1
min(['a', 'b']); // Returns: 'a'
```

View File

@ -0,0 +1,26 @@
# max
找到数组中具有最大值的元素。
如果列表为空,则返回 `undefined`
## 签名
```typescript
function max<T>(items: T[]): T;
```
### 参数
- `items` (`T[]`): 要搜索的元素数组。
### 返回
(`T`): 具有最大值的元素。
### 示例
```typescript
max([1, 2, 3]); // Returns: 3
max(['a', 'b']); // Returns: 'b'
```

View File

@ -0,0 +1,26 @@
# min
找到数组中具有最小值的元素。
如果列表为空,则返回 `undefined`
## 签名
```typescript
function min<T>(items: T[]): T;
```
### 参数
- `items` (`T[]`): 要搜索的元素数组。
### 返回
具有最小值的元素。
### 示例
```typescript
min([1, 2, 3]); // Returns: 1
min(['a', 'b']); // Returns: 'a'
```

View File

@ -1 +1 @@
export const falsey = [, null, undefined, false, 0, NaN, ''];
export const falsey: unknown[] = [, null, undefined, false, 0, NaN, ''];

View File

@ -30,3 +30,5 @@ export { get } from './object/get.ts';
export { set } from './object/set.ts';
export { startsWith } from './string/startsWith.ts';
export { endsWith } from './string/endsWith.ts';
export { max } from './math/max.ts';
export { min } from './math/min.ts';

View File

@ -0,0 +1,18 @@
import { describe, expect, it } from "vitest";
import { max } from "./max";
describe('max', () => {
it('should return the largest value from a collection', () => {
expect(max([1, 2, 3])).toBe(3);
});
it('should return `undefined` for empty collections', () => {
expect(max([])).toBe(undefined);
expect(max()).toBe(undefined);
});
it('should work with non-numeric collection values', () => {
expect(max(['a', 'b'])).toBe('b');
});
});

27
src/compat/math/max.ts Normal file
View File

@ -0,0 +1,27 @@
/**
* Finds the element in an array that has the maximum value.
*
* @template T - The type of elements in the array.
* @param {T[]} items The array of elements to search.
* @returns {T} The element with the maximum value.
* @example
* // Returns 9
* max([3, 1, 4, 1, 5, 9])
*
* @example
* // Returns 8
* max([0, -3, 2, 8, 7])
*/
export function max<T>(items: readonly T[] = []): T {
let maxElement = items[0];
let max = undefined;
for (const element of items) {
if (max == null || element > max) {
max = element;
maxElement = element;
}
}
return maxElement;
}

View File

@ -0,0 +1,18 @@
import { describe, expect, it } from "vitest";
import { min } from "./min";
describe('min', () => {
it('should return the largest value from a collection', () => {
expect(min([1, 2, 3])).toBe(1);
});
it('should return `undefined` for empty collections', () => {
expect(min([])).toBe(undefined);
expect(min()).toBe(undefined);
});
it('should work with non-numeric collection values', () => {
expect(min(['a', 'b'])).toBe('a');
});
});

27
src/compat/math/min.ts Normal file
View File

@ -0,0 +1,27 @@
/**
* Finds the element in an array that has the minimum value.
*
* @template T - The type of elements in the array.
* @param {T[]} items The array of elements to search.
* @returns {T} The element with the minimum value.
* @example
* // Returns 1
* min([3, 1, 4, 1, 5, 9])
*
* @example
* // Returns -3
* min([0, -3, 2, 8, 7])
*/
export function min<T>(items: readonly T[] = []): T {
let minElement = items[0];
let min = undefined;
for (const element of items) {
if (min == null || element < min) {
min = element;
minElement = element;
}
}
return minElement;
}