diff --git a/docs/.vitepress/en.mts b/docs/.vitepress/en.mts index d022b384..41f5edf2 100644 --- a/docs/.vitepress/en.mts +++ b/docs/.vitepress/en.mts @@ -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' }, diff --git a/docs/.vitepress/ko.mts b/docs/.vitepress/ko.mts index 8d77a295..ef907c58 100644 --- a/docs/.vitepress/ko.mts +++ b/docs/.vitepress/ko.mts @@ -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' }, diff --git a/docs/ko/reference/array/uniqBy.md b/docs/ko/reference/array/uniqBy.md new file mode 100644 index 00000000..c6f72411 --- /dev/null +++ b/docs/ko/reference/array/uniqBy.md @@ -0,0 +1,25 @@ +# uniqBy + +`mapper` 함수가 반환하는 값을 기준으로, 배열 내 요소들의 중복을 제거해요. + +## 인터페이스 + +```typescript +function uniqBy(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] +``` diff --git a/docs/ko/reference/array/uniqWith.md b/docs/ko/reference/array/uniqWith.md new file mode 100644 index 00000000..2b745dba --- /dev/null +++ b/docs/ko/reference/array/uniqWith.md @@ -0,0 +1,25 @@ +# uniqWith + +두 요소가 일치하는지 여부를 판단하는 커스텀 함수를 기준으로, 배열 내 요소들의 중복을 제거해요. + +## 인터페이스 + +```typescript +function uniqWith(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] +``` diff --git a/docs/reference/array/uniq.md b/docs/reference/array/uniq.md index 5cc2aac3..85d06e4b 100644 --- a/docs/reference/array/uniq.md +++ b/docs/reference/array/uniq.md @@ -1,5 +1,3 @@ -=== - # uniq Creates a duplicate-free version of an array. diff --git a/docs/reference/array/uniqBy.md b/docs/reference/array/uniqBy.md new file mode 100644 index 00000000..2292f4e5 --- /dev/null +++ b/docs/reference/array/uniqBy.md @@ -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(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] +``` diff --git a/docs/reference/array/uniqWith.md b/docs/reference/array/uniqWith.md new file mode 100644 index 00000000..8b855e88 --- /dev/null +++ b/docs/reference/array/uniqWith.md @@ -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(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] +``` diff --git a/src/array/unionBy.spec.ts b/src/array/unionBy.spec.ts index a9a6fe18..95a5c8d6 100644 --- a/src/array/unionBy.spec.ts +++ b/src/array/unionBy.spec.ts @@ -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 }]); }); diff --git a/src/array/uniqBy.spec.ts b/src/array/uniqBy.spec.ts index 49bee324..a2821b9a 100644 --- a/src/array/uniqBy.spec.ts +++ b/src/array/uniqBy.spec.ts @@ -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]); }); }); diff --git a/src/array/uniqBy.ts b/src/array/uniqBy.ts index 96d80e0e..f2d2fb51 100644 --- a/src/array/uniqBy.ts +++ b/src/array/uniqBy.ts @@ -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(arr: readonly T[], converter: (item: T) => U): T[] { - return uniqWith(arr, (item1, item2) => converter(item1) === converter(item2)); +export function uniqBy(arr: readonly T[], mapper: (item: T) => U): T[] { + return uniqWith(arr, (item1, item2) => mapper(item1) === mapper(item2)); } diff --git a/src/array/uniqWith.ts b/src/array/uniqWith.ts index 0b7c8239..6c467a83 100644 --- a/src/array/uniqWith.ts +++ b/src/array/uniqWith.ts @@ -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.