Added a very complicated AsyncIterableReader class.
The class was already implemented in "https://deno.land/std@0.144.0/streams/conversion.ts". So, I litterally lost 1 day of work! I hate myself.
This commit is contained in:
1 parent
17a1b4bed3
commit
0d627c9051
125 files changed
+26943
-83
No files matched your search
+49
@@ -0,0 +1,49 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
// This module is browser compatible.
|
||||
|
||||
import { type Reader } from "../types.d.ts";
|
||||
import { BufReader } from "./buf_reader.ts";
|
||||
import { concat } from "../bytes/concat.ts";
|
||||
|
||||
/**
|
||||
* Read strings line-by-line from a Reader.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { readLines } from "https://deno.land/std@$STD_VERSION/io/read_lines.ts";
|
||||
* import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";
|
||||
*
|
||||
* 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);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
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 = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user