Class DelimiterStream

Divide a stream into chunks delimited by a given byte sequence.

If you are working with a stream of string, consider using TextDelimiterStream.

Divide a CSV stream by commas, discarding the commas:

import { DelimiterStream } from "@std/streams/delimiter-stream";
import { assertEquals } from "@std/assert";

const inputStream = ReadableStream.from(["foo,bar", ",baz"]);

const transformed = inputStream.pipeThrough(new TextEncoderStream())
.pipeThrough(new DelimiterStream(new TextEncoder().encode(",")))
.pipeThrough(new TextDecoderStream());

assertEquals(await Array.fromAsync(transformed), ["foo", "bar", "baz"]);

Divide a stream after semi-colons, keeping the semicolons in the output:

import { DelimiterStream } from "@std/streams/delimiter-stream";
import { assertEquals } from "@std/assert";

const inputStream = ReadableStream.from(["foo;", "bar;baz", ";"]);

const transformed = inputStream.pipeThrough(new TextEncoderStream())
.pipeThrough(
new DelimiterStream(new TextEncoder().encode(";"), {
disposition: "suffix",
}),
).pipeThrough(new TextDecoderStream());

assertEquals(await Array.fromAsync(transformed), ["foo;", "bar;", "baz;"]);
Hierarchy
  • TransformStream<Uint8Array, Uint8Array>
    • DelimiterStream

Constructors

Properties

Constructors

Properties

readable: ReadableStream<Uint8Array>
writable: WritableStream<Uint8Array>