toLines

import { toLines } from "jsr:@ende/streams/unstable-to-lines";

function ยท experimental

function toLines(readable: ReadableStream<Uint8Array>, options?: StreamPipeOptions): ReadableStream<string>

Converts a ReadableStream of Uint8Arrays into one of lines delimited by \n or \r\n. Trims the last line if empty.

readable: ReadableStream<Uint8Array>

A stream of Uint8Arrays.

options?: StreamPipeOptions

Stream options.

Returns

A stream of lines delimited by \n or \r\n.

Example: Usage

import { toLines } from "@ende/streams/unstable-to-lines";
import { assertEquals } from "@ende/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",
]);

Source