Skip to content

Commit

Permalink
feat: isPromise (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kfirfitousi committed Mar 3, 2023
1 parent 89149d8 commit 6d2a198
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isNull,
isNumber,
isObject,
isPromise,
isRegExp,
isString,
isSymbol,
Expand Down Expand Up @@ -219,3 +220,18 @@ describe('isWindow', () => {
expect(isWindow({})).toBe(false)
})
})

describe('isPromise', () => {
it('should return true if value is a Promise', () => {
expect(isPromise(new Promise(() => {}))).toBe(true)
expect(isPromise(Promise.resolve())).toBe(true)
})
it('should return false if value is not a Promise', () => {
expect(isWindow(undefined)).toBe(false)
expect(isWindow(null)).toBe(false)
expect(isWindow(123)).toBe(false)
expect(isWindow('string')).toBe(false)
expect(isWindow(true)).toBe(false)
expect(isWindow({})).toBe(false)
})
})
4 changes: 4 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export function isWindow(x: unknown): x is Window {
return toString(x) === '[object Window]'
}

export function isPromise(x: unknown): x is Promise<unknown> {
return toString(x) === '[object Promise]'
}

export function isIterator(obj: unknown): obj is Iterator<unknown> {
return isObject(obj) && obj !== null && typeof (obj as any)[Symbol.iterator] === 'function'
}

0 comments on commit 6d2a198

Please sign in to comment.