std/io/read_lines.ts
Yoshiya Hinosawa 0ce9c2bf7e
experiment
2024-01-31 18:10:15 +09:00

52 lines
1.4 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 { BufReader } from "./buf_reader.ts";
import { concat } from "@std/bytes/concat";
/**
* Read strings line-by-line from a Reader.
*
* @example
* ```ts
* import { readLines } from "@std/io/read_lines";
* import * as path from "@std/path";
*
* const filename = path.join(Deno.cwd(), "std/io/README.md");
* let fileReader = await Deno.open(filename);
*
* for await (let line of readLines(fileReader)) {
* console.log(line);
* }
* ```
*
* @deprecated (will be removed after 1.0.0) Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead.
*/
export async function* readLines(
reader: Reader,
decoderOpts?: {
encoding?: string;
fatal?: boolean;
ignoreBOM?: boolean;
},
): AsyncIterableIterator<string> {
const bufReader = new BufReader(reader);
let chunks: Uint8Array[] = [];
const decoder = new TextDecoder(decoderOpts?.encoding, decoderOpts);
while (true) {
const res = await bufReader.readLine();
if (!res) {
if (chunks.length > 0) {
yield decoder.decode(concat(chunks));
}
break;
}
chunks.push(res.line);
if (!res.more) {
yield decoder.decode(concat(chunks));
chunks = [];
}
}
}