diff --git a/index.js b/index.js index e455c0d..dc2b9b6 100644 --- a/index.js +++ b/index.js @@ -35,6 +35,10 @@ Create a TransformStream that passes only the first `count` chunks. @returns {TransformStream} A TransformStream that limits chunks. */ export function takeStream(count) { + if (!Number.isInteger(count) || count < 0) { + throw new TypeError("Expected count to be a non-negative integer"); + } + let taken = 0; return new TransformStream({ transform(chunk, controller) { @@ -57,6 +61,10 @@ Create a TransformStream that collects chunks into arrays of a given size. @returns {TransformStream} A TransformStream that batches chunks. */ export function batchStream(size) { + if (!Number.isInteger(size) || size <= 0) { + throw new TypeError("Expected size to be a positive integer"); + } + let buffer = []; return new TransformStream({ flush(controller) { diff --git a/test.js b/test.js index 890f046..b49b9f2 100644 --- a/test.js +++ b/test.js @@ -129,6 +129,14 @@ test("takeStream returns empty for count 0", async (t) => { t.deepEqual(result, []); }); +test("takeStream rejects invalid counts", (t) => { + t.throws(() => takeStream(-1), { + instanceOf: TypeError, + message: "Expected count to be a non-negative integer", + }); + t.throws(() => takeStream(1.5), { instanceOf: TypeError }); +}); + test("takeStream takes exactly 1", async (t) => { const result = await collectStream( createReadable([10, 20, 30]).pipeThrough(takeStream(1)) @@ -162,6 +170,14 @@ test("batchStream with size 1 wraps each chunk", async (t) => { t.deepEqual(result, [[1], [2], [3]]); }); +test("batchStream rejects invalid sizes", (t) => { + t.throws(() => batchStream(0), { + instanceOf: TypeError, + message: "Expected size to be a positive integer", + }); + t.throws(() => batchStream(1.5), { instanceOf: TypeError }); +}); + test("batchStream handles empty stream", async (t) => { const result = await collectStream( createReadable([]).pipeThrough(batchStream(3))