Add is.asyncFunction (#20)

This commit is contained in:
Brandon Smith 2017-10-15 23:29:10 -04:00 committed by Sindre Sorhus
parent 04cb80dfb1
commit dc3b6ff86b
3 changed files with 27 additions and 2 deletions

View file

@ -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');

View file

@ -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)

12
test.js
View file

@ -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');
});