Skip to content

Commit

Permalink
docs: Add docs for uniqBy and uniqWith
Browse files Browse the repository at this point in the history
  • Loading branch information
raon0211 committed Jun 13, 2024
1 parent 44e09c0 commit fde86f7
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
2 changes: 2 additions & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
25 changes: 25 additions & 0 deletions docs/ko/reference/array/uniqBy.md
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]
```
25 changes: 25 additions & 0 deletions docs/ko/reference/array/uniqWith.md
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]
```
2 changes: 0 additions & 2 deletions docs/reference/array/uniq.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
===

# uniq

Creates a duplicate-free version of an array.
Expand Down
25 changes: 25 additions & 0 deletions docs/reference/array/uniqBy.md
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]
```
25 changes: 25 additions & 0 deletions docs/reference/array/uniqWith.md
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]
```
2 changes: 1 addition & 1 deletion src/array/unionBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]);
});
Expand Down
2 changes: 1 addition & 1 deletion src/array/uniqBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});
13 changes: 6 additions & 7 deletions src/array/uniqBy.ts
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));
}
5 changes: 2 additions & 3 deletions src/array/uniqWith.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit fde86f7

Please sign in to comment.