Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(isDefinedArray): add isDefinedArray #928

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/ko/reference/predicate/isDefinedArray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# isDefinedArray

주어진 배열의 모든 요소가 `undefined`나 `null`이 아닌지 확인해요.

TypeScript의 타입 가드로 사용할 수 있어요. 파라미터로 주어진 배열의 타입을 넓은 범위 `T | undefined | null`에서 정의된 타입 `T`으로 좁혀요.

## 시그니처

```typescript
function isDefinedArray<T>(array: Array<T | undefined | null>): array is T[];
```

### 매개변수

- `array` (`Array<T | undefined | null>`): 모든 요소가 정의되어 있는지 확인할 배열.

### 반환값

(`array is T[]`): 모든 요소가 정의되어 있으면 `true`, 그렇지 않으면 `false`.

## 예시

```typescript
// 모든 요소가 정의되어 있어 true를 반환해요.
console.log(isDefinedArray([1, 2, 3])); // true

// 요소 중에 undefined나 null이 있으면 false를 반환해요.
console.log(isDefinedArray([undefined, 1, 2, 3])); // false
console.log(isDefinedArray([null, 1, 2, 3])); // false

// 빈 배열은 모든 요소가 정의되어 있다고 간주되어 true를 반환해요.
console.log(isDefinedArray([])); // true
```
33 changes: 33 additions & 0 deletions docs/reference/predicate/isDefinedArray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# isDefinedArray

Checks whether all elements in the given array are neither `undefined` nor `null`.

You can use it as a TypeScript type guard, narrowing the type of the array from `T | undefined | null` to `T`.

## Signature

```typescript
function isDefinedArray<T>(array: Array<T | undefined | null>): array is T[];
```

### Parameters

- `array` (`Array<T | undefined | null>`): The array to check if all elements are defined.

### Returns

(`array is T[]`): `true` if all elements are defined; otherwise, `false`.

## Examples

```typescript
// All elements are defined, so it returns true.
console.log(isDefinedArray([1, 2, 3])); // true

// If there's an undefined or null element, it returns false.
console.log(isDefinedArray([undefined, 1, 2, 3])); // false
console.log(isDefinedArray([null, 1, 2, 3])); // false

// An empty array is considered to have all elements defined, so it returns true.
console.log(isDefinedArray([])); // true
```
18 changes: 18 additions & 0 deletions src/predicate/isDefinedArray.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest';
import { isDefinedArray } from './isDefinedArray';

describe('isDefinedArray', () => {
it('should return `true` for arrays with only defined values', () => {
expect(isDefinedArray([1, 2, 3])).toBe(true);
expect(isDefinedArray([undefined, 1, 2, 3])).toBe(false);
expect(isDefinedArray([null, 1, 2, 3])).toBe(false);
expect(isDefinedArray([undefined, null, 1, 2, 3])).toBe(false);
expect(isDefinedArray([1, 2, 3, undefined])).toBe(false);
expect(isDefinedArray([1, 2, 3, null])).toBe(false);
expect(isDefinedArray([1, 2, 3, undefined, null])).toBe(false);
});

it('should return `true` for empty arrays', () => {
expect(isDefinedArray([])).toBe(true);
});
});
20 changes: 20 additions & 0 deletions src/predicate/isDefinedArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Check if all items in the array are defined.
*
* @param array The array to check.
* @returns `true` if all items in the array are defined, `false` otherwise.
*
* @example
* ```ts
* isDefinedArray([1, 2, 3]); // true
* isDefinedArray([undefined, 1, 2, 3]); // false
* isDefinedArray([null, 1, 2, 3]); // false
* isDefinedArray([undefined, null, 1, 2, 3]); // false
* isDefinedArray([1, 2, 3, undefined]); // false
* isDefinedArray([1, 2, 3, null]); // false
* isDefinedArray([1, 2, 3, undefined, null]); // false
* ```
*/
export function isDefinedArray<T>(array: Array<T | undefined | null>): array is T[] {
return array.every(item => item !== undefined && item !== null);
}