Skip to content

Commit a1495fe

Browse files
committed
feat(push): add push function
1 parent 80a9c02 commit a1495fe

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

index.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import test from "ava";
2-
import {asyncIterable, tail, toArray} from "./index";
2+
import {asyncIterable, push, tail, toArray} from "./index";
33

44
test("tail", async t => {
55
t.deepEqual(await toArray(tail(asyncIterable([1, 2, 3, 4]))), [2, 3, 4]);
66
t.deepEqual(await toArray(tail(asyncIterable([1]))), []);
77
t.deepEqual(await toArray(tail(asyncIterable([]))), []);
88
});
9+
10+
test("push", async t => {
11+
t.deepEqual(await toArray(push(asyncIterable([1, 2, 3]), 4)), [1, 2, 3, 4]);
12+
t.deepEqual(await toArray(push(asyncIterable([]), 4)), [4]);
13+
});

index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ export async function* tail<T>(iterable: AsyncIterableLike<T>): AsyncIterable<T>
5252
}
5353

5454
export const asyncTail = tail;
55+
56+
export async function* push<T>(iterable: AsyncIterableLike<T>, value: T): AsyncIterable<T> {
57+
for await (const element of await iterable) {
58+
yield element;
59+
}
60+
61+
yield value;
62+
}
63+
64+
export const asyncPush = push;
65+
66+
export function pushFn<T>(value: T): (iterable: AsyncIterableLike<T>) => AsyncIterable<T> {
67+
return iterable => push(iterable, value);
68+
}
69+
70+
export const asyncPushFn = pushFn;

0 commit comments

Comments
 (0)