Skip to content

Commit

Permalink
Add .asyncGenerator and .asyncGeneratorFunction detection (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
forresst authored Jan 29, 2020
1 parent d28c86e commit 446a7a0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
32 changes: 32 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,38 @@ is.asyncFunction(() => {});
// => false
```

##### .asyncGenerator(value)

```js
is.asyncGenerator(
(async function * () {
yield 4;
})()
);
// => true

is.asyncGenerator(
(function * () {
yield 4;
})()
);
// => false
```

##### .asyncGeneratorFunction(value)

```js
is.asyncGeneratorFunction(async function * () {
yield 4;
});
// => true

is.asyncGeneratorFunction(function * () {
yield 4;
});
// => false
```

##### .boundFunction(value)

Returns `true` for any `bound` function.
Expand Down
10 changes: 10 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -141,6 +143,8 @@ is.asyncIterable = <T = unknown>(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 = <T = unknown>(value: unknown): value is Promise<T> =>
isObjectOfType<Promise<T>>(TypeName.Promise)(value);

Expand All @@ -153,6 +157,8 @@ is.promise = <T = unknown>(value: unknown): value is Promise<T> => is.nativeProm

is.generatorFunction = isObjectOfType<GeneratorFunction>(TypeName.GeneratorFunction);

is.asyncGeneratorFunction = (value: unknown): value is ((...args: any[]) => Promise<unknown>) => getObjectType(value) === TypeName.AsyncGeneratorFunction;

is.asyncFunction = <T = unknown>(value: unknown): value is ((...args: any[]) => Promise<T>) => getObjectType(value) === TypeName.AsyncFunction;

// eslint-disable-next-line no-prototype-builtins, @typescript-eslint/ban-types
Expand Down Expand Up @@ -454,9 +460,11 @@ interface Assert {
iterable: <T = unknown>(value: unknown) => asserts value is Iterable<T>;
asyncIterable: <T = unknown>(value: unknown) => asserts value is AsyncIterable<T>;
generator: (value: unknown) => asserts value is Generator;
asyncGenerator: (value: unknown) => asserts value is AsyncGenerator;
nativePromise: <T = unknown>(value: unknown) => asserts value is Promise<T>;
promise: <T = unknown>(value: unknown) => asserts value is Promise<T>;
generatorFunction: (value: unknown) => asserts value is GeneratorFunction;
asyncGeneratorFunction: (value: unknown) => asserts value is AsyncGeneratorFunction;
// eslint-disable-next-line @typescript-eslint/ban-types
asyncFunction: (value: unknown) => asserts value is Function;
// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down Expand Up @@ -542,9 +550,11 @@ export const assert: Assert = {
iterable: <T = unknown>(value: unknown): asserts value is Iterable<T> => assertType(is.iterable(value), AssertionTypeDescription.iterable, value),
asyncIterable: <T = unknown>(value: unknown): asserts value is AsyncIterable<T> => assertType(is.asyncIterable(value), AssertionTypeDescription.asyncIterable, value),
generator: (value: unknown): asserts value is Generator => assertType(is.generator(value), TypeName.Generator, value),
asyncGenerator: (value: unknown): asserts value is AsyncGenerator => assertType(is.asyncGenerator(value), TypeName.AsyncGenerator, value),
nativePromise: <T = unknown>(value: unknown): asserts value is Promise<T> => assertType(is.nativePromise(value), AssertionTypeDescription.nativePromise, value),
promise: <T = unknown>(value: unknown): asserts value is Promise<T> => assertType(is.promise(value), TypeName.Promise, value),
generatorFunction: (value: unknown): asserts value is GeneratorFunction => assertType(is.generatorFunction(value), TypeName.GeneratorFunction, value),
asyncGeneratorFunction: (value: unknown): asserts value is AsyncGeneratorFunction => assertType(is.asyncGeneratorFunction(value), TypeName.AsyncGeneratorFunction, value),
// eslint-disable-next-line @typescript-eslint/ban-types
asyncFunction: (value: unknown): asserts value is Function => assertType(is.asyncFunction(value), TypeName.AsyncFunction, value),
// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down
49 changes: 47 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ const types = new Map<string, Test>([
function () {},
() => {},
async function () {},
function * (): unknown {}
function * (): unknown {},
async function * (): unknown {}
],
typename: TypeName.Function
}],
Expand Down Expand Up @@ -232,6 +233,16 @@ const types = new Map<string, Test>([
],
typename: TypeName.Generator
}],
['asyncGenerator', {
is: is.asyncGenerator,
assert: assert.asyncGenerator,
fixtures: [
(async function * () {
yield 4;
})()
],
typename: TypeName.AsyncGenerator
}],
['generatorFunction', {
is: is.generatorFunction,
assert: assert.generatorFunction,
Expand All @@ -243,6 +254,17 @@ const types = new Map<string, Test>([
typename: TypeName.Function,
typeDescription: TypeName.GeneratorFunction
}],
['asyncGeneratorFunction', {
is: is.asyncGeneratorFunction,
assert: assert.asyncGeneratorFunction,
fixtures: [
async function * () {
yield 4;
}
],
typename: TypeName.Function,
typeDescription: TypeName.AsyncGeneratorFunction
}],
['asyncFunction', {
is: is.asyncFunction,
assert: assert.asyncFunction,
Expand Down Expand Up @@ -609,7 +631,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 => {
Expand Down Expand Up @@ -678,10 +700,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']);
});
Expand Down

0 comments on commit 446a7a0

Please sign in to comment.