From dc3b6ff86be0234ced8f0a03c6e256be4454c547 Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Sun, 15 Oct 2017 23:29:10 -0400 Subject: [PATCH] Add is.asyncFunction (#20) --- index.js | 5 ++++- readme.md | 12 ++++++++++++ test.js | 12 +++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6f57809..0da91b3 100644 --- a/index.js +++ b/index.js @@ -85,7 +85,10 @@ is.promise = x => is.nativePromise(x) || hasPromiseAPI(x); is.generator = x => is.iterable(x) && is.function(x.next) && is.function(x.throw); // TODO: Change to use `isObjectOfType` once Node.js 6 or higher is targeted -is.generatorFunction = x => is.function(x) && is.function(x.constructor) && x.constructor.name === 'GeneratorFunction'; +const isFunctionOfType = type => x => is.function(x) && is.function(x.constructor) && x.constructor.name === type; + +is.generatorFunction = isFunctionOfType('GeneratorFunction'); +is.asyncFunction = isFunctionOfType('AsyncFunction'); is.regExp = isObjectOfType('RegExp'); is.date = isObjectOfType('Date'); diff --git a/readme.md b/readme.md index d2e572b..9e565fb 100644 --- a/readme.md +++ b/readme.md @@ -84,6 +84,18 @@ Returns `true` for any object that implements its own `.next()` and `.throw()` m ##### .generatorFunction(value) +##### .asyncFunction(value) + +Returns `true` for any `async` function that can be called with the `await` operator. + +```js +is.asyncFunction(async () => {}); +// => true + +is.asyncFunction(() => {}); +// => false +``` + ##### .map(value) ##### .set(value) ##### .weakMap(value) diff --git a/test.js b/test.js index 43566f5..0d62dce 100644 --- a/test.js +++ b/test.js @@ -68,6 +68,10 @@ const types = new Map([ ['generatorFunction', function * () { yield 4; }], + ['asyncFunction', [ + async function () {}, + async () => {} + ]], ['map', new Map()], ['set', new Set()], ['weakMap', new WeakMap()], @@ -172,7 +176,7 @@ test('is.array', t => { }); test('is.function', t => { - testType(t, 'function', ['generatorFunction']); + testType(t, 'function', ['generatorFunction', 'asyncFunction']); }); test('is.buffer', t => { @@ -215,6 +219,12 @@ test('is.generatorFunction', t => { testType(t, 'generatorFunction', ['function']); }); +if (isNode8orHigher) { + test('is.asyncFunction', t => { + testType(t, 'asyncFunction', ['function']); + }); +} + test('is.map', t => { testType(t, 'map'); });