-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add docs for uniqBy and uniqWith
- Loading branch information
Showing
11 changed files
with
114 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
=== | ||
|
||
# uniq | ||
|
||
Creates a duplicate-free version of an array. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
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 | ||
* 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] | ||
* ``` | ||
*/ | ||
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters