Skip to content

Commit 66c99e5

Browse files
committed
feat(empty): add empty function
1 parent cc3a587 commit 66c99e5

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

index.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from "ava";
2-
import {asyncIterable, initial, last, only, push, tail, toArray, unshift} from "./index";
2+
import {asyncIterable, empty, initial, last, only, push, tail, toArray, unshift} from "./index";
33

44
test("tail", async t => {
55
t.deepEqual(await toArray(tail(asyncIterable([1, 2, 3, 4]))), [2, 3, 4]);
@@ -33,3 +33,9 @@ test("only", async t => {
3333
t.is(await only(asyncIterable([4])), 4);
3434
t.is(await only(asyncIterable([3, 4, 5])), null);
3535
});
36+
37+
test("empty", async t => {
38+
t.is(await empty(asyncIterable([])), true);
39+
t.is(await empty(asyncIterable([1])), false);
40+
t.is(await empty(asyncIterable([1, 2, 3])), false);
41+
});

index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,9 @@ export async function only<T>(iterable: AsyncIterableLike<T>): Promise<T | null>
119119
}
120120

121121
export const asyncOnly = only;
122+
123+
export async function empty(iterable: AsyncIterableLike<unknown>): Promise<boolean> {
124+
return (await asyncIterator(iterable).next()).done === true;
125+
}
126+
127+
export const asyncEmpty = empty;

0 commit comments

Comments
 (0)