mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-27 14:57:44 +03:00
docs: Add docs for uniqBy and uniqWith
This commit is contained in:
parent
44e09c0b73
commit
fde86f7f4b
@ -70,6 +70,8 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{ text: 'unionBy', link: '/reference/array/unionBy' },
|
||||
{ text: 'unionWith', link: '/reference/array/unionWith' },
|
||||
{ text: 'uniq', link: '/reference/array/uniq' },
|
||||
{ text: 'uniqBy', link: '/reference/array/uniqBy' },
|
||||
{ text: 'uniqWith', link: '/reference/array/uniqWith' },
|
||||
{ text: 'xor', link: '/reference/array/xor' },
|
||||
{ text: 'xorBy', link: '/reference/array/xorBy' },
|
||||
{ text: 'xorWith', link: '/reference/array/xorWith' },
|
||||
|
@ -69,6 +69,8 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{ text: 'unionBy', link: '/ko/reference/array/unionBy' },
|
||||
{ text: 'unionWith', link: '/ko/reference/array/unionWith' },
|
||||
{ text: 'uniq', link: '/ko/reference/array/uniq' },
|
||||
{ text: 'uniqBy', link: '/ko/reference/array/uniqBy' },
|
||||
{ text: 'uniqWith', link: '/ko/reference/array/uniqWith' },
|
||||
{ text: 'xor', link: '/ko/reference/array/xor' },
|
||||
{ text: 'xorBy', link: '/ko/reference/array/xorBy' },
|
||||
{ text: 'xorWith', link: '/ko/reference/array/xorWith' },
|
||||
|
25
docs/ko/reference/array/uniqBy.md
Normal file
25
docs/ko/reference/array/uniqBy.md
Normal file
@ -0,0 +1,25 @@
|
||||
# uniqBy
|
||||
|
||||
`mapper` 함수가 반환하는 값을 기준으로, 배열 내 요소들의 중복을 제거해요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[]
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `arr` (`T[]`): 중복을 제거할 배열.
|
||||
- `mapper` (`(item: T) => U`): 비교하기 위해 요소를 새로운 값으로 변환할 함수.
|
||||
|
||||
### 반환 값
|
||||
|
||||
(`T[]`): `mapper` 함수가 반환하는 값을 기준으로 중복이 제거된 배열.
|
||||
|
||||
## 예시
|
||||
|
||||
```typescript
|
||||
uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
||||
// [1.2, 2.1, 3.3, 5.7, 7.19]
|
||||
```
|
25
docs/ko/reference/array/uniqWith.md
Normal file
25
docs/ko/reference/array/uniqWith.md
Normal file
@ -0,0 +1,25 @@
|
||||
# uniqWith
|
||||
|
||||
두 요소가 일치하는지 여부를 판단하는 커스텀 함수를 기준으로, 배열 내 요소들의 중복을 제거해요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function uniqWith<T>(arr: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[]
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `arr` (`T[]`): 중복을 제거할 배열.
|
||||
- `areItemsEqual` (`(x: T, y: T) => boolean`): 두 요소가 일치하는지 판단하는 일치 함수예요. 두 요소가 일치한다면 `true`를, 일치하지 않는다면 `false`를 반환하게 해주세요.
|
||||
|
||||
### 반환 값
|
||||
|
||||
(`T[]`): 커스텀 일치 함수의 반환 값을 기준으로, 중복이 제거된 새로운 배열.
|
||||
|
||||
## 예시
|
||||
|
||||
```typescript
|
||||
uniqWith([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], (a, b) => Math.abs(a - b) < 1);
|
||||
// [1.2, 3.2, 5.7, 7.19]
|
||||
```
|
@ -1,5 +1,3 @@
|
||||
===
|
||||
|
||||
# uniq
|
||||
|
||||
Creates a duplicate-free version of an array.
|
||||
|
25
docs/reference/array/uniqBy.md
Normal file
25
docs/reference/array/uniqBy.md
Normal file
@ -0,0 +1,25 @@
|
||||
# uniqBy
|
||||
|
||||
Returns a new array containing only the unique elements from the original array, based on the values returned by the `mapper` function.
|
||||
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function uniqBy<T, U>(arr: T[], mapper: (item: T) => U): T[]
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- `arr` (`T[]`): The array to process.
|
||||
- `mapper` (`(item: T) => U`): The function used to convert the array elements.
|
||||
|
||||
### Returns
|
||||
|
||||
(`T[]`): A new array containing only the unique elements from the original array, based on the values returned by the `mapper` function.
|
||||
|
||||
## Examples
|
||||
|
||||
```typescript
|
||||
uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
||||
// [1.2, 2.1, 3.3, 5.7, 7.19]
|
||||
```
|
25
docs/reference/array/uniqWith.md
Normal file
25
docs/reference/array/uniqWith.md
Normal file
@ -0,0 +1,25 @@
|
||||
# uniqWith
|
||||
|
||||
Returns a new array containing only the unique elements from the original array, based on the values returned by the comparator function.
|
||||
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function uniqWith<T>(arr: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[]
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- `arr` (`T[]`): The array to process.
|
||||
- `areItemsEqual` (`(x: T, y: T) => boolean`): A custom function to determine if two elements are equal. This function takes two arguments, one from each array, and returns true if the elements are considered equal, and false otherwise.
|
||||
|
||||
### Returns
|
||||
|
||||
(`T[]`): A new array containing only the unique elements from the original array, based on the values returned by the comparator function.
|
||||
|
||||
## Examples
|
||||
|
||||
```typescript
|
||||
uniqWith([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], (a, b) => Math.abs(a - b) < 1);
|
||||
// [1.2, 3.2, 5.7, 7.19]
|
||||
```
|
@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest';
|
||||
import { unionBy } from './unionBy';
|
||||
|
||||
describe('unionBy', () => {
|
||||
it('should work with a `converter`', () => {
|
||||
it('should work with a `mapper`', () => {
|
||||
expect(unionBy([2.1], [1.2, 2.3], Math.floor)).toEqual([2.1, 1.2]);
|
||||
expect(unionBy([{ x: 1 }], [{ x: 2 }, { x: 1 }], ({ x }) => x)).toEqual([{ x: 1 }, { x: 2 }]);
|
||||
});
|
||||
|
@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest';
|
||||
import { uniqBy } from './uniqBy';
|
||||
|
||||
describe('uniqBy', () => {
|
||||
it('should work with a `converter`', () => {
|
||||
it('should work with a `mapper`', () => {
|
||||
expect(uniqBy([2.1, 1.2, 2.3], Math.floor)).toEqual([2.1, 1.2]);
|
||||
});
|
||||
});
|
||||
|
@ -1,13 +1,12 @@
|
||||
import { uniqWith } from './uniqWith';
|
||||
|
||||
/**
|
||||
* The `uniqBy` function takes an array as its first argument and a 'converter' function as the second. It maps the array elements using the converter function, then removes any duplicates.
|
||||
*
|
||||
* It filters out elements with the same value, meaning it does not check for duplicates in data types like Objects.
|
||||
* Returns a new array containing only the unique elements from the original array,
|
||||
* based on the values returned by the mapper function.
|
||||
*
|
||||
* @param {T[]} arr - The array to process.
|
||||
* @param {(item: T) => U} converter - The function used to convert the array elements.
|
||||
* @returns {T[]} A new array containing only the unique elements from the original array, based on the values returned by the converter function.
|
||||
* @param {(item: T) => U} mapper - The function used to convert the array elements.
|
||||
* @returns {T[]} A new array containing only the unique elements from the original array, based on the values returned by the mapper function.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
@ -15,6 +14,6 @@ import { uniqWith } from './uniqWith';
|
||||
* // [1.2, 2.1, 3.3, 5.7, 7.19]
|
||||
* ```
|
||||
*/
|
||||
export function uniqBy<T, U>(arr: readonly T[], converter: (item: T) => U): T[] {
|
||||
return uniqWith(arr, (item1, item2) => converter(item1) === converter(item2));
|
||||
export function uniqBy<T, U>(arr: readonly T[], mapper: (item: T) => U): T[] {
|
||||
return uniqWith(arr, (item1, item2) => mapper(item1) === mapper(item2));
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
/**
|
||||
* The `uniqWith` function takes an array as its first argument and a 'comparator' function as the second.
|
||||
*
|
||||
* It evaluates the elements of the array using the comparator function, and if true is returned, it considers those elements as duplicates and removes them.
|
||||
* Returns a new array containing only the unique elements from the original array,
|
||||
* based on the values returned by the comparator function.
|
||||
*
|
||||
* @param {T[]} arr - The array to process.
|
||||
* @param {(item1: T, item2: T) => boolean} areItemsEqual - The function used to compare the array elements.
|
||||
|
Loading…
Reference in New Issue
Block a user