std/io/read_string_delim.ts
Yoshiya Hinosawa d93b33aff8
docs(io): document std/io (#5656)
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-08-08 23:20:43 +09:00

44 lines
1.3 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import type { Reader } from "./types.ts";
import { readDelim } from "./read_delim.ts";
/**
* Read {@linkcode Reader} chunk by chunk, splitting based on delimiter.
*
* @example Usage
* ```ts
* import { readStringDelim } from "@std/io/read-string-delim";
* import { assert } from "@std/assert/assert"
*
* let fileReader = await Deno.open("README.md");
*
* for await (let line of readStringDelim(fileReader, "\n")) {
* assert(typeof line === "string");
* }
* ```
*
* @param reader The reader to read from
* @param delim The delimiter to split the reader by
* @param decoderOpts The options
* @returns The async iterator of strings
*
* @deprecated This will be removed in 1.0.0. Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead.
*/
export async function* readStringDelim(
reader: Reader,
delim: string,
decoderOpts?: {
encoding?: string;
fatal?: boolean;
ignoreBOM?: boolean;
},
): AsyncIterableIterator<string> {
const encoder = new TextEncoder();
const decoder = new TextDecoder(decoderOpts?.encoding, decoderOpts);
for await (const chunk of readDelim(reader, encoder.encode(delim))) {
yield decoder.decode(chunk);
}
}