Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
raon0211 authored Jul 3, 2024
1 parent 6e972f0 commit 32cfc65
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
20 changes: 8 additions & 12 deletions docs/ko/reference/array/without.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@
## 인터페이스

```typescript
function without<T>(array: ArrayLike<T>, ...values: T[]): T[];
function without<T>(array: T[], ...values: T[]): T[];
```

### 파라미터

- `array` (`ArrayLike<T>`): 필터링할 배열.
- `values` (`...T[]`): 제외할 값들.
- `array` (`T[]`): 값을 제거할 배열.
- `values` (`...T[]`): 제거할 값.

### 반환 값

(`T[]`) 지정된 값을 제외한 새 배열.

### 에러

명시적으로 예외를 발생시키지는 않지만, 입력이 유효한 배열이 아니거나 배열 길이가 유효하지 않은 경우 빈 배열을 반환합니다.
(`T[]`) 주어진 값을 제외한 새 배열.

## Examples

Expand All @@ -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]
```
14 changes: 4 additions & 10 deletions docs/reference/array/without.md
Original file line number Diff line number Diff line change
@@ -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<T>(array: ArrayLike<T>, ...values: T[]): T[];
function without<T>(array: T[], ...values: T[]): T[];
```

### Parameters

- `array` (`ArrayLike<T>`): 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
Expand Down
13 changes: 4 additions & 9 deletions src/array/without.ts
Original file line number Diff line number Diff line change
@@ -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<T>} 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.
*
Expand All @@ -20,10 +18,7 @@
* without(['a', 'b', 'c', 'a'], 'a');
* // Returns: ['b', 'c']
*/
export function without<T>(array: ArrayLike<T>, ...values: T[]): T[] {
if (!array || typeof array.length !== 'number') {
return [];
}
export function without<T>(array: readonly T[], ...values: T[]): T[] {
const valuesSet = new Set(values);
return Array.prototype.filter.call(array, item => !valuesSet.has(item));
}

0 comments on commit 32cfc65

Please sign in to comment.