-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 removed
BIN
-2.43 MB
.yarn/cache/@rollup-rollup-linux-arm64-gnu-npm-4.17.1-5704467160-10c0.zip
Binary file not shown.
Binary file removed
BIN
-2.66 MB
.yarn/cache/@rollup-rollup-linux-x64-gnu-npm-4.17.1-a2ec383d51-10c0.zip
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
}); | ||
}); |