diff --git a/source/index.ts b/source/index.ts index 6cb8159..15c1998 100644 --- a/source/index.ts +++ b/source/index.ts @@ -14,7 +14,9 @@ export const enum TypeName { symbol = 'symbol', Function = 'Function', Generator = 'Generator', + AsyncGenerator = 'AsyncGenerator', GeneratorFunction = 'GeneratorFunction', + AsyncGeneratorFunction = 'AsyncGeneratorFunction', AsyncFunction = 'AsyncFunction', Observable = 'Observable', Array = 'Array', @@ -141,6 +143,8 @@ is.asyncIterable = (value: unknown): value is AsyncIterableIterator => is.generator = (value: unknown): value is Generator => is.iterable(value) && is.function_(value.next) && is.function_(value.throw); +is.asyncGenerator = (value: unknown): value is AsyncGenerator => is.asyncIterable(value) && is.function_(value.next) && is.function_(value.throw); + is.nativePromise = (value: unknown): value is Promise => isObjectOfType>(TypeName.Promise)(value); @@ -153,6 +157,8 @@ is.promise = (value: unknown): value is Promise => is.nativePromise(val is.generatorFunction = isObjectOfType(TypeName.GeneratorFunction); +is.asyncGeneratorFunction = (value: unknown): value is ((...args: any[]) => Promise) => getObjectType(value) === TypeName.AsyncGeneratorFunction; + is.asyncFunction = (value: unknown): value is ((...args: any[]) => Promise) => getObjectType(value) === TypeName.AsyncFunction; // eslint-disable-next-line no-prototype-builtins, @typescript-eslint/ban-types diff --git a/test/test.ts b/test/test.ts index 0d9c13a..fa6771f 100644 --- a/test/test.ts +++ b/test/test.ts @@ -123,7 +123,8 @@ const types = new Map([ function () {}, () => {}, async function () {}, - function * (): unknown {} + function * (): unknown {}, + async function * (): unknown {} ], typename: TypeName.Function }], @@ -189,6 +190,15 @@ const types = new Map([ ], typename: TypeName.Generator }], + ['asyncGenerator', { + is: is.asyncGenerator, + fixtures: [ + (async function * () { + yield 4; + })() + ], + typename: TypeName.AsyncGenerator + }], ['generatorFunction', { is: is.generatorFunction, fixtures: [ @@ -198,6 +208,15 @@ const types = new Map([ ], typename: TypeName.Function }], + ['asyncGeneratorFunction', { + is: is.asyncGeneratorFunction, + fixtures: [ + async function * () { + yield 4; + } + ], + typename: TypeName.Function + }], ['asyncFunction', { is: is.asyncFunction, fixtures: [ @@ -513,7 +532,7 @@ test('is.array', t => { }); test('is.function', t => { - testType(t, 'function', ['generatorFunction', 'asyncFunction', 'boundFunction']); + testType(t, 'function', ['generatorFunction', 'asyncGeneratorFunction', 'asyncFunction', 'boundFunction']); }); test('is.boundFunction', t => { @@ -572,10 +591,33 @@ test('is.generator', t => { testType(t, 'generator'); }); +test('is.asyncGenerator', t => { + testType(t, 'asyncGenerator'); + + const fixture = (async function * () { + yield 4; + })(); + if (is.asyncGenerator(fixture)) { + t.true(is.function_(fixture.next)); + } +}); + test('is.generatorFunction', t => { testType(t, 'generatorFunction', ['function']); }); +test('is.asyncGeneratorFunction', t => { + testType(t, 'asyncGeneratorFunction', ['function']); + + const fixture = async function * () { + yield 4; + }; + + if (is.asyncGeneratorFunction(fixture)) { + t.true(is.function_(fixture().next)); + } +}); + test('is.map', t => { testType(t, 'map', ['emptyMap']); });