Skip to content

Commit

Permalink
fix: parameter is iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Jan 11, 2024
1 parent 19b2a1b commit 71bbd34
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/fromIterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ export const fromIterable = <T>(
sourceIterable: Iterable<T>,
strategy?: QueuingStrategy,
): ReadableStream<T> => {
if (!sourceIterable?.[Symbol.iterator]) {
throw new Error("sourceIterable is not iterable");
}
const iterator = sourceIterable[Symbol.iterator]();
return new ReadableStream<T>(
{
start(controller) {
for (const value of sourceIterable) {
pull(controller) {
const { value, done } = iterator.next();
if (done) {
controller.close();
} else {
controller.enqueue(value);
}
controller.close();
},
},
strategy,
Expand Down
5 changes: 5 additions & 0 deletions test/fromIterable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ describe("fromIterable", () => {
"sourceIterable is not iterable",
);
});

it("should throw if parameter is iterable", () => {
// @ts-expect-error undefined parameter for test
expect(() => fromIterable(1)).toThrow("sourceIterable is not iterable");
});
});

0 comments on commit 71bbd34

Please sign in to comment.