Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: async generator guards
Browse files Browse the repository at this point in the history
  • Loading branch information
tmgulland committed Jun 6, 2024
1 parent a9f5bc8 commit dd51e31
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const arraysEquals: Equals<any[]> = (arr1, arr2) => {
if (!isArray(arr1) || !isArray(arr2)) {
return false
}

if (arr1.length !== arr2.length) {
return false
}
Expand Down
6 changes: 6 additions & 0 deletions src/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export const isFunction = (n: unknown): n is Function => typeof n === 'function'
export const isAsyncFunction = (n: unknown): n is Function =>
isFunction(n) && n.constructor.name === 'AsyncFunction'

export const isAsyncGenerator = <T>(value: any): value is AsyncGenerator<T> =>
value && typeof value[Symbol.asyncIterator] === 'function'

export const isAsyncGeneratorFunction = (n: unknown): n is Function =>
isFunction(n) && n.constructor.name === 'AsyncGeneratorFunction'

export const isSet = <T>(n: unknown): n is Set<T> => n instanceof Set

export const isValidURL = (n: unknown): n is string => {
Expand Down
47 changes: 46 additions & 1 deletion test/guards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
isRGBColorString,
isHSLColorString,
isLabColorString,
isLchColorString
isLchColorString,
isAsyncGenerator,
isAsyncGeneratorFunction
} from '../src/guards'

describe('Primitive Type Checkers', () => {
Expand Down Expand Up @@ -110,6 +112,49 @@ describe('Specialized Type and Functionality Checkers', () => {
expect(isAsyncFunction(() => {})).toBe(false)
})

it('isAsyncGenerator detects async generators', () => {
async function testAsync() {}
function testSync() {}

async function* testAsyncGenerator() {
yield 1
yield 2
yield 3
}
function* testSyncGenerator() {
yield 1
yield 2
yield 3
}
expect(isAsyncGenerator(testSync)).toBe(false)
expect(isAsyncGenerator(testAsync)).toBe(false)
expect(isAsyncGenerator(testAsyncGenerator())).toBe(true)
expect(isAsyncGenerator(testSyncGenerator())).toBe(false)
expect(isAsyncGenerator([1, 2, 3])).toBe(false)
})

it('isAsyncGeneratorFunction detects async generator functions', () => {
async function testAsync() {}
function testSync() {}
async function* testAsyncGenerator() {
yield 1
yield 2
yield 3
}

function* testSyncGenerator() {
yield 1
yield 2
yield 3
}

expect(isAsyncGenerator(testSync)).toBe(false)
expect(isAsyncGenerator(testAsync)).toBe(false)
expect(isAsyncGeneratorFunction(testAsyncGenerator)).toBe(true)
expect(isAsyncGeneratorFunction(testSyncGenerator)).toBe(false)
expect(isAsyncGeneratorFunction([1, 2, 3])).toBe(false)
})

it('isSet checks for Set instances', () => {
expect(isSet(new Set())).toBe(true)
expect(isSet(new Map())).toBe(false)
Expand Down

0 comments on commit dd51e31

Please sign in to comment.