-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add generator detection functions to utils (#2923)
Functions like this are used in several places in the stack so move them to the utils package for reuse. Incorporates changes from #2918
- Loading branch information
1 parent
827a38a
commit 31a15a1
Showing
7 changed files
with
138 additions
and
0 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
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,10 @@ | ||
export function isAsyncGenerator (obj: unknown): obj is AsyncGenerator { | ||
if (obj == null) return false | ||
const asyncIterator = (obj as { [Symbol.asyncIterator]?: unknown })?.[ | ||
Symbol.asyncIterator | ||
] | ||
if (typeof asyncIterator !== 'function') return false | ||
|
||
const instance = obj as { next?: unknown } | ||
return typeof instance.next === '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function isGenerator (obj: unknown): obj is Generator { | ||
if (obj == null) return false | ||
const iterator = (obj as { [Symbol.iterator]?: unknown })?.[Symbol.iterator] | ||
if (typeof iterator !== 'function') return false | ||
|
||
const instance = obj as { next?: unknown } | ||
return typeof instance.next === '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { expect } from 'aegir/chai' | ||
import { isAsyncGenerator } from '../src/is-async-generator.js' | ||
|
||
describe('is-async-generator', () => { | ||
it('should return true if the value is an async generator', () => { | ||
async function * asyncGen (): AsyncGenerator<number> { | ||
yield 1 | ||
} | ||
const asyncGenerator = asyncGen() | ||
expect(isAsyncGenerator(asyncGenerator)).to.be.true() | ||
|
||
const asyncGenObj = (async function * () { | ||
yield 1 | ||
})() | ||
expect(isAsyncGenerator(asyncGenObj)).to.be.true() | ||
}) | ||
|
||
it('should return false if the value is not an async generator', () => { | ||
expect(isAsyncGenerator(1)).to.be.false() | ||
expect(isAsyncGenerator('string')).to.be.false() | ||
expect(isAsyncGenerator({})).to.be.false() | ||
expect(isAsyncGenerator([])).to.be.false() | ||
expect(isAsyncGenerator(null)).to.be.false() | ||
expect(isAsyncGenerator(undefined)).to.be.false() | ||
expect(isAsyncGenerator(() => {})).to.be.false() | ||
expect(isAsyncGenerator(async () => {})).to.be.false() | ||
expect( | ||
isAsyncGenerator(function * () { | ||
yield 1 | ||
}) | ||
).to.be.false() | ||
expect( | ||
isAsyncGenerator(async function * () { | ||
yield 1 | ||
}) | ||
).to.be.false() // async generator function, not generator | ||
expect(isAsyncGenerator(Promise.resolve())).to.be.false() | ||
expect(isAsyncGenerator({ next: async () => {} })).to.be.false() | ||
expect(isAsyncGenerator({ [Symbol.asyncIterator]: () => {} })).to.be.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,41 @@ | ||
import { expect } from 'aegir/chai' | ||
import { isGenerator } from '../src/is-generator.js' | ||
|
||
describe('is-generator', () => { | ||
it('should return true if the value is a generator', () => { | ||
function * gen (): Generator<number> { | ||
yield 1 | ||
} | ||
const generator = gen() | ||
expect(isGenerator(generator)).to.be.true() | ||
|
||
const genObj = (function * () { | ||
yield 1 | ||
})() | ||
expect(isGenerator(genObj)).to.be.true() | ||
}) | ||
|
||
it('should return false if the value is not a generator', () => { | ||
expect(isGenerator(1)).to.be.false() | ||
expect(isGenerator('string')).to.be.false() | ||
expect(isGenerator({})).to.be.false() | ||
expect(isGenerator([])).to.be.false() | ||
expect(isGenerator(null)).to.be.false() | ||
expect(isGenerator(undefined)).to.be.false() | ||
expect(isGenerator(() => {})).to.be.false() | ||
expect(isGenerator(async () => {})).to.be.false() | ||
expect( | ||
isGenerator(function * () { | ||
yield 1 | ||
}) | ||
).to.be.false() // generator function, not generator | ||
expect( | ||
isGenerator(async function * () { | ||
yield 1 | ||
}) | ||
).to.be.false() | ||
expect(isGenerator(Promise.resolve())).to.be.false() | ||
expect(isGenerator({ next: () => {} })).to.be.false() | ||
expect(isGenerator({ [Symbol.iterator]: () => {} })).to.be.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
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