Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(streams/unstable): toLines() #5121

Merged
merged 7 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions streams/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"./to-array-buffer": "./to_array_buffer.ts",
"./to-blob": "./to_blob.ts",
"./to-json": "./to_json.ts",
"./to-lines": "./to_lines.ts",
"./to-text": "./to_text.ts",
"./to-transform-stream": "./to_transform_stream.ts",
"./zip-readable-streams": "./zip_readable_streams.ts"
Expand Down
1 change: 1 addition & 0 deletions streams/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export * from "./text_line_stream.ts";
export * from "./to_array_buffer.ts";
export * from "./to_blob.ts";
export * from "./to_json.ts";
export * from "./to_lines.ts";
export * from "./to_text.ts";
export * from "./to_transform_stream.ts";
export * from "./zip_readable_streams.ts";
41 changes: 41 additions & 0 deletions streams/to_lines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { TextLineStream } from "./text_line_stream.ts";

/**
* Converts a {@linkcode ReadableStream} of {@linkcode Uint8Array}s into one of
* lines delimited by `\n` or `\r\n`. Trims the last line if empty.
*
* @param readable A stream of {@linkcode Uint8Array}s.
* @param options Stream options.
* @returns A stream of lines delimited by `\n` or `\r\n`.
*
* @example Usage
* ```ts
* import { toLines } from "@std/streams/to-lines";
* import { assertEquals } from "@std/assert/equals";
*
* const readable = ReadableStream.from([
* "qwertzu",
* "iopasd\r\nmnbvc",
* "xylk\rjhgfds\napoiuzt\r",
* "qwr\r09ei\rqwrjiowqr\r",
* ]).pipeThrough(new TextEncoderStream());
*
* assertEquals(await Array.fromAsync(toLines(readable)), [
* "qwertzuiopasd",
* "mnbvcxylk\rjhgfds",
* "apoiuzt\rqwr\r09ei\rqwrjiowqr\r",
* ]);
* ```
*
* @experimental
*/
export function toLines(
readable: ReadableStream<Uint8Array>,
options?: StreamPipeOptions,
): ReadableStream<string> {
return readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream(), options);
}
38 changes: 38 additions & 0 deletions streams/to_lines_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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(""));
});