std/streams/to_lines_test.ts
Doctor d5e31807c5
feat(streams/unstable): toLines() (#5121)
* feat(streams): add toLines

* chore(streams): remove testing code

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>

* chore(streams): fmt

* adjust(streams): change toLines return type to `ReadableStream<string>`

- Change the return type from `AsyncGenerator<string>` to `ReadableStream<string>`
- Change `readable` to only be of type `ReadableStream<Uint8Array>`
- Add `options` argument of type `PipeOptions.`

* work

* work

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-08-09 13:26:39 +02:00

39 lines
1.0 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { toLines } from "./to_lines.ts";
import { assertEquals } from "@std/assert";
Deno.test("toLines() parses simple input", async () => {
const stream = ReadableStream.from([
"qwertzu",
"iopasd\r\nmnbvc",
"xylk\rjhgfds\napoiuzt\r",
"qwr\r09ei\rqwrjiowqr\r",
"\nrewq0987\n\n654321",
"\nrewq0987\r\n\r\n654321\r",
]).pipeThrough(new TextEncoderStream());
assertEquals(await Array.fromAsync(toLines(stream)), [
"qwertzuiopasd",
"mnbvcxylk\rjhgfds",
"apoiuzt\rqwr\r09ei\rqwrjiowqr",
"rewq0987",
"",
"654321",
"rewq0987",
"",
"654321\r",
]);
});
Deno.test("toLines() parses large chunks", async () => {
const totalLines = 20_000;
const stream = ReadableStream.from(["\n".repeat(totalLines)]).pipeThrough(
new TextEncoderStream(),
);
const lines = await Array.fromAsync(toLines(stream));
assertEquals(lines.length, totalLines);
assertEquals(lines, Array.from({ length: totalLines }).fill(""));
});