From 32cfc659f8df1d26bf0420a3a04a78aea496454d Mon Sep 17 00:00:00 2001 From: Sojin Park Date: Wed, 3 Jul 2024 09:58:06 +0900 Subject: [PATCH] Apply suggestions from code review --- docs/ko/reference/array/without.md | 20 ++++++++------------ docs/reference/array/without.md | 14 ++++---------- src/array/without.ts | 13 ++++--------- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/docs/ko/reference/array/without.md b/docs/ko/reference/array/without.md index afb48bfdf..9916b5bc1 100644 --- a/docs/ko/reference/array/without.md +++ b/docs/ko/reference/array/without.md @@ -7,21 +7,17 @@ ## 인터페이스 ```typescript -function without(array: ArrayLike, ...values: T[]): T[]; +function without(array: T[], ...values: T[]): T[]; ``` ### 파라미터 -- `array` (`ArrayLike`): 필터링할 배열. -- `values` (`...T[]`): 제외할 값들. +- `array` (`T[]`): 값을 제거할 배열. +- `values` (`...T[]`): 제거할 값. ### 반환 값 -(`T[]`) 지정된 값을 제외한 새 배열. - -### 에러 - -명시적으로 예외를 발생시키지는 않지만, 입력이 유효한 배열이 아니거나 배열 길이가 유효하지 않은 경우 빈 배열을 반환합니다. +(`T[]`) 주어진 값을 제외한 새 배열. ## Examples @@ -30,17 +26,17 @@ import { without } from 'es-toolkit/array'; // 배열에서 지정된 값을 제거합니다 without([1, 2, 3, 4, 5], 2, 4); -// 반환: [1, 3, 5] +// 결과 값: [1, 3, 5] // 배열에서 지정된 문자열 값을 제거합니다 without(['a', 'b', 'c', 'a'], 'a'); -// 반환: ['b', 'c'] +// 결과 값: ['b', 'c'] // 지정된 값이 배열에 없는 경우를 처리합니다 without([1, 2, 3], 4, 5); -// 반환: [1, 2, 3] +// 결과 값: [1, 2, 3] // 다른 유형의 값을 포함한 경우를 처리합니다 without([1, '2', 3, '4'], 2, '4'); -// 반환: [1, '2', 3] +// 결과 값: [1, '2', 3] ``` diff --git a/docs/reference/array/without.md b/docs/reference/array/without.md index 7fb28cc01..51a920624 100644 --- a/docs/reference/array/without.md +++ b/docs/reference/array/without.md @@ -1,30 +1,24 @@ # without -Creates an array excluding all given values using SameValueZero for equality comparisons. +Creates an array that excludes all specified values. -This function takes an input array and returns a new array that excludes all values -specified in the additional arguments. It uses SameValueZero for equality comparisons, -meaning that it considers -0 and +0 as equal but treats NaN as unequal to itself. +It correctly excludes `NaN`, as it compares values using [SameValueZero](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-samevaluezero). ## Signature ```typescript -function without(array: ArrayLike, ...values: T[]): T[]; +function without(array: T[], ...values: T[]): T[]; ``` ### Parameters -- `array` (`ArrayLike`): The array to filter. +- `array` (`T[]`): The array to exclude values. - `values` (`...T[]`): The values to exclude. ### Returns (`T[]`) A new array without the specified values. -### Throws - -Does not throw explicitly but returns an empty array if the input is not a valid array or has invalid array length. - ## Examples ```typescript diff --git a/src/array/without.ts b/src/array/without.ts index dcbb1e4c7..abdf023a8 100644 --- a/src/array/without.ts +++ b/src/array/without.ts @@ -1,12 +1,10 @@ /** - * Creates an array excluding all given values using SameValueZero for equality comparisons. + * Creates an array that excludes all specified values. * - * This function takes an input array and returns a new array that excludes all values - * specified in the second argument. It uses SameValueZero for equality comparisons, - * meaning that it considers -0 and +0 as equal but treats NaN as unequal to itself. + * It correctly excludes `NaN`, as it compares values using [SameValueZero](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-samevaluezero). * * @template T The type of elements in the array. - * @param {ArrayLike} array - The array to filter. + * @param {T[]} array - The array to filter. * @param {...T[]} values - The values to exclude. * @returns {T[]} A new array without the specified values. * @@ -20,10 +18,7 @@ * without(['a', 'b', 'c', 'a'], 'a'); * // Returns: ['b', 'c'] */ -export function without(array: ArrayLike, ...values: T[]): T[] { - if (!array || typeof array.length !== 'number') { - return []; - } +export function without(array: readonly T[], ...values: T[]): T[] { const valuesSet = new Set(values); return Array.prototype.filter.call(array, item => !valuesSet.has(item)); }