forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move 'isAsyncIterable' into 'jsutils' (graphql#2775)
- Loading branch information
1 parent
ab329e9
commit f3e2954
Showing
3 changed files
with
73 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import identityFunc from '../identityFunc'; | ||
import isAsyncIterable from '../isAsyncIterable'; | ||
|
||
describe('isAsyncIterable', () => { | ||
it('should return `true` for AsyncIterable', () => { | ||
// $FlowFixMe[prop-missing] Flow doesn't support Symbol.asyncIterator | ||
const asyncIteratable = { [Symbol.asyncIterator]: identityFunc }; | ||
expect(isAsyncIterable(asyncIteratable)).to.equal(true); | ||
|
||
// istanbul ignore next (Never called and use just as a placeholder) | ||
async function* asyncGeneratorFunc() { | ||
/* do nothing */ | ||
} | ||
|
||
expect(isAsyncIterable(asyncGeneratorFunc())).to.equal(true); | ||
|
||
// But async generator function itself is not iteratable | ||
expect(isAsyncIterable(asyncGeneratorFunc)).to.equal(false); | ||
}); | ||
|
||
it('should return `false` for all other values', () => { | ||
expect(isAsyncIterable(null)).to.equal(false); | ||
expect(isAsyncIterable(undefined)).to.equal(false); | ||
|
||
expect(isAsyncIterable('ABC')).to.equal(false); | ||
expect(isAsyncIterable('0')).to.equal(false); | ||
expect(isAsyncIterable('')).to.equal(false); | ||
|
||
expect(isAsyncIterable([])).to.equal(false); | ||
expect(isAsyncIterable(new Int8Array(1))).to.equal(false); | ||
|
||
expect(isAsyncIterable({})).to.equal(false); | ||
expect(isAsyncIterable({ iterable: true })).to.equal(false); | ||
|
||
const iterator = { [Symbol.iterator]: identityFunc }; | ||
expect(isAsyncIterable(iterator)).to.equal(false); | ||
|
||
// istanbul ignore next (Never called and use just as a placeholder) | ||
function* generatorFunc() { | ||
/* do nothing */ | ||
} | ||
expect(isAsyncIterable(generatorFunc())).to.equal(false); | ||
|
||
const invalidAsyncIteratable = { | ||
// $FlowFixMe[prop-missing] Flow doesn't support Symbol.asyncIterator | ||
[Symbol.asyncIterator]: { next: identityFunc }, | ||
}; | ||
expect(isAsyncIterable(invalidAsyncIteratable)).to.equal(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { SYMBOL_ASYNC_ITERATOR } from '../polyfills/symbols'; | ||
|
||
/** | ||
* Returns true if the provided object implements the AsyncIterator protocol via | ||
* either implementing a `Symbol.asyncIterator` or `"@@asyncIterator"` method. | ||
*/ | ||
declare function isAsyncIterable(value: mixed): boolean %checks(value instanceof | ||
AsyncIterable); | ||
|
||
// eslint-disable-next-line no-redeclare | ||
export default function isAsyncIterable(maybeAsyncIterable) { | ||
if (maybeAsyncIterable == null || typeof maybeAsyncIterable !== 'object') { | ||
return false; | ||
} | ||
|
||
return typeof maybeAsyncIterable[SYMBOL_ASYNC_ITERATOR] === 'function'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters