Skip to content

Commit

Permalink
feat(utils): isPromise 함수 추가 (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 authored May 1, 2024
1 parent 6b86655 commit 8f039d3
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-ads-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modern-kit/utils': patch
---

feat: isPromise 함수 및 테스트 코드 추가
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
23 changes: 23 additions & 0 deletions docs/docs/utils/validator/isPromise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# isPromise

주어진 인자가 `Promise`인지 검사하는 함수입니다. Promise가 맞다면 타입을 좁혀줍니다.

<br />

## Interface
```tsx title="typescript"
const isPromise: <T = any>(value: any) => value is Promise<T>
```
## Usage
```ts
import { isPromise } from '@modern-kit/utils';

isPromise(Promise.resolve()); // true

isPromise(() => {}); // false
isPromise('123'); // false
isPromise(true); // false
isPromise({}); // false
isPromise(null); // false
```
1 change: 1 addition & 0 deletions packages/utils/src/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './isFunction';
export * from './isNotNullish';
export * from './isNullish';
export * from './isNumber';
export * from './isPromise';
export * from './isValidEmail';
export * from './isValidPhoneNumberFormat';
3 changes: 3 additions & 0 deletions packages/utils/src/validator/isPromise/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isPromise = <T = any>(value: any): value is Promise<T> => {
return value instanceof Promise;
};
23 changes: 23 additions & 0 deletions packages/utils/src/validator/isPromise/isPromise.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isPromise } from '.';

describe('isPromise', () => {
it('should return true if the given value is a Promise', () => {
expect(isPromise(Promise.resolve())).toBeTruthy();

expect(isPromise(() => {})).toBeFalsy();
expect(isPromise('123')).toBeFalsy();
expect(isPromise(true)).toBeFalsy();
expect(isPromise({})).toBeFalsy();
expect(isPromise(null)).toBeFalsy();
});

it('should narrow the type to Promise if isPromise returns true', () => {
const promise: any = Promise.resolve(1);

if (isPromise<number>(promise)) {
expectTypeOf(promise).toEqualTypeOf<Promise<number>>();
} else {
expectTypeOf(promise).toEqualTypeOf<any>();
}
});
});

0 comments on commit 8f039d3

Please sign in to comment.