mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
d93b33aff8
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
44 lines
1.3 KiB
TypeScript
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);
|
|
}
|
|
}
|