UNPKG

esbuild-gas-plugin

Version:
40 lines (39 loc) 1.21 kB
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. import { BufReader } from "./buf_reader.js"; import { concat } from "../bytes/concat.js"; /** * 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, decoderOpts) { const bufReader = new BufReader(reader); let chunks = []; 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 = []; } } }