diff --git a/source/index.ts b/source/index.ts index ba5c0f7..1cbf72c 100644 --- a/source/index.ts +++ b/source/index.ts @@ -136,6 +136,7 @@ namespace is { // tslint:disable-line:no-namespace export const nullOrUndefined = (value: any): value is null | undefined => null_(value) || undefined(value); export const object = (value: any): value is object => !nullOrUndefined(value) && (function_(value) || isObject(value)); export const iterable = (value: any): value is IterableIterator => !nullOrUndefined(value) && function_(value[Symbol.iterator]); + export const asyncIterable = (value: any): value is AsyncIterableIterator => !nullOrUndefined(Symbol.asyncIterator) && typeof value[Symbol.asyncIterator] === 'function'; export const generator = (value: any): value is Generator => iterable(value) && function_(value.next) && function_(value.throw); export const nativePromise = (value: any): value is Promise => diff --git a/source/tests/test.ts b/source/tests/test.ts index ae99c2c..a329459 100644 --- a/source/tests/test.ts +++ b/source/tests/test.ts @@ -10,6 +10,7 @@ import ZenObservable from 'zen-observable'; import m from '..'; const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8; +const isNode10orHigher = Number(process.versions.node.split('.')[0]) >= 10; class PromiseSubclassFixture extends Promise {} class ErrorSubclassFixture extends Error {} @@ -337,6 +338,14 @@ const types = new Map([ Infinity, -Infinity ] + }], + ['asyncIterable', { + is: m.asyncIterable, + fixtures: [ + Object.create({ + [Symbol.asyncIterator]: () => {} // tslint:disable-line:no-empty + }) + ] }] ]); @@ -454,6 +463,14 @@ if (isNode8orHigher) { });*/ } +if (isNode10orHigher) { + test('is.asyncIterable', t => { + t.true(m.asyncIterable(Object.create({ + [Symbol.asyncIterator]: () => {} // tslint:disable-line:no-empty + }))); + }); +} + test('is.generator', t => { testType(t, 'generator'); }); diff --git a/tsconfig.json b/tsconfig.json index 2a1baca..f7d6fd1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,8 @@ "dom", "es2015", "es2016", - "es2017.sharedmemory" + "es2017.sharedmemory", + "esnext.asynciterable" ], "module": "commonjs", "moduleResolution": "node",