Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
goGenerator
, goAsyncGenerator
BREAKING CHANGE: - It requires Node.js >= v18.17.0 - CommonJS => ESM
- Loading branch information
1 parent
209bb79
commit d135630
Showing
18 changed files
with
1,857 additions
and
3,075 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,76 @@ | ||
import { describe, test, expect, vi } from 'vitest' | ||
import { goAsyncGenerator } from '@src/go-async-generator.js' | ||
import { getErrorAsync } from 'return-style' | ||
import { toArrayAsync } from 'iterable-operator' | ||
|
||
describe('goAsyncGenerator', () => { | ||
describe('non-generator', () => { | ||
test('return value', async () => { | ||
const fn = vi.fn() | ||
|
||
await toArrayAsync(goAsyncGenerator(fn)) | ||
|
||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
|
||
test('throw error', async () => { | ||
const fn = vi.fn(() => { | ||
throw new Error('foo') | ||
}) | ||
|
||
const err = await getErrorAsync(() => toArrayAsync(goAsyncGenerator(fn))) | ||
|
||
expect(err).toBeInstanceOf(Error) | ||
expect(err?.message).toBe('foo') | ||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
}) | ||
|
||
describe('generator', () => { | ||
test('yield value', async () => { | ||
const fn = vi.fn(function* () { | ||
yield | ||
}) | ||
|
||
await toArrayAsync(goAsyncGenerator(fn)) | ||
|
||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
|
||
test('throw error', async () => { | ||
const fn = vi.fn(function* () { | ||
throw new Error('foo') | ||
}) | ||
|
||
const err = await getErrorAsync(() => toArrayAsync(goAsyncGenerator(fn))) | ||
|
||
expect(err).toBeInstanceOf(Error) | ||
expect(err?.message).toBe('foo') | ||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
}) | ||
|
||
describe('async generator', () => { | ||
test('yield value', async () => { | ||
const fn = vi.fn(async function* () { | ||
yield | ||
}) | ||
|
||
await toArrayAsync(goAsyncGenerator(fn)) | ||
|
||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
|
||
test('throw error', async () => { | ||
const fn = vi.fn(async function* () { | ||
throw new Error('foo') | ||
}) | ||
|
||
const err = await getErrorAsync(() => toArrayAsync(goAsyncGenerator(fn))) | ||
|
||
expect(err).toBeInstanceOf(Error) | ||
expect(err?.message).toBe('foo') | ||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
}) | ||
}) |
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,52 @@ | ||
import { describe, test, expect, vi } from 'vitest' | ||
import { goGenerator } from '@src/go-generator.js' | ||
import { getError } from 'return-style' | ||
import { toArray } from 'iterable-operator' | ||
|
||
describe('goGenerator', () => { | ||
describe('non-generator', () => { | ||
test('return value', () => { | ||
const fn = vi.fn() | ||
|
||
toArray(goGenerator(fn)) | ||
|
||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
|
||
test('throw error', () => { | ||
const fn = vi.fn(() => { | ||
throw new Error('foo') | ||
}) | ||
|
||
const err = getError(() => toArray(goGenerator(fn))) | ||
|
||
expect(err).toBeInstanceOf(Error) | ||
expect(err?.message).toBe('foo') | ||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
}) | ||
|
||
describe('generator', () => { | ||
test('yield value', () => { | ||
const fn = vi.fn(function* () { | ||
yield | ||
}) | ||
|
||
toArray(goGenerator(fn)) | ||
|
||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
|
||
test('throw error', () => { | ||
const fn = vi.fn(function* () { | ||
throw new Error('foo') | ||
}) | ||
|
||
const err = getError(() => toArray(goGenerator(fn))) | ||
|
||
expect(err).toBeInstanceOf(Error) | ||
expect(err?.message).toBe('foo') | ||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
}) | ||
}) |
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
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
File renamed without changes.
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,14 @@ | ||
// @ts-check | ||
|
||
import js from '@eslint/js' | ||
import ts from 'typescript-eslint' | ||
|
||
export default ts.config( | ||
js.configs.recommended | ||
, ...ts.configs.recommended | ||
, { | ||
rules: { | ||
'require-yield': 'off' | ||
} | ||
} | ||
) |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
export async function* goAsyncGenerator<Yield, Next>( | ||
fn: () => | ||
| void | ||
| Generator<Yield, void, Next> | ||
| AsyncGenerator<Yield, void, Next> | ||
): AsyncGenerator<Yield, void, Next> { | ||
const generator = fn() | ||
if (generator) { | ||
yield* generator | ||
} | ||
} |
Oops, something went wrong.