-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert_async_iterable.ts
33 lines (31 loc) · 1.01 KB
/
assert_async_iterable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
import { isAsyncIterable } from "https://deno.land/x/isx@1.3.1/is_async_iterable.ts";
import type { ErrorConstructorLike } from "./types.ts";
/** Assert the input is `AsyncIterable`.
* @param input - Any input.
* @example
* ```ts
* import { assertAsyncIterable } from "https://deno.land/x/assertion@$VERSION/assert_async_iterable.ts";
* import {
* assertFalse,
* assertThrows,
* } from "https://deno.land/std/testing/asserts.ts";
* assertFalse(
* assertAsyncIterable({
* async *[Symbol.asyncIterator]() {
* yield "hello";
* },
* }),
* );
* assertThrows(() => assertAsyncIterable(() => {}));
* ```
* @throws {Error} If the input is not {@link AsyncIterable}.
*/
export function assertAsyncIterable<T>(
input: unknown,
msg?: string,
constructor: ErrorConstructorLike = Error,
): asserts input is AsyncIterable<T> {
if (!isAsyncIterable(input)) throw new constructor(msg);
}