From 034549bb36b07d1f0867063c1132be1b0fc9039e Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 25 Sep 2024 13:47:26 +1000 Subject: [PATCH] BREAKING(io): remove `readLines()` (#5991) * deprecation(io/unstable): deprecate `readLines()` * BREAKING(io/unstable): remove `readLines()` --- io/deno.json | 1 - io/mod.ts | 1 - io/read_lines.ts | 56 ------------------------------ io/read_lines_test.ts | 80 ------------------------------------------- 4 files changed, 138 deletions(-) delete mode 100644 io/read_lines.ts delete mode 100644 io/read_lines_test.ts diff --git a/io/deno.json b/io/deno.json index 99b897d3a..b788d93a1 100644 --- a/io/deno.json +++ b/io/deno.json @@ -14,7 +14,6 @@ "./read-all": "./read_all.ts", "./read-delim": "./read_delim.ts", "./read-int": "./read_int.ts", - "./read-lines": "./read_lines.ts", "./read-long": "./read_long.ts", "./read-range": "./read_range.ts", "./read-short": "./read_short.ts", diff --git a/io/mod.ts b/io/mod.ts index ff0d0214a..51812825f 100644 --- a/io/mod.ts +++ b/io/mod.ts @@ -27,7 +27,6 @@ export * from "./multi_reader.ts"; export * from "./read_all.ts"; export * from "./read_delim.ts"; export * from "./read_int.ts"; -export * from "./read_lines.ts"; export * from "./read_long.ts"; export * from "./read_range.ts"; export * from "./read_short.ts"; diff --git a/io/read_lines.ts b/io/read_lines.ts deleted file mode 100644 index 410ca36c2..000000000 --- a/io/read_lines.ts +++ /dev/null @@ -1,56 +0,0 @@ -// 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 {@linkcode Reader}. - * - * @example Usage - * ```ts - * import { readLines } from "@std/io/read-lines"; - * import { assert } from "@std/assert/assert" - * - * using fileReader = await Deno.open("README.md"); - * - * for await (let line of readLines(fileReader)) { - * assert(typeof line === "string"); - * } - * ``` - * - * @param reader The reader to read from - * @param decoderOpts The options - * @returns The async iterator of strings - * - * @deprecated Use - * {@linkcode https://jsr.io/@std/streams/doc/unstable-to-lines/~/toLines | toLines} - * on the readable stream instead. This will be removed in 0.225.0. - */ -export async function* readLines( - reader: Reader, - decoderOpts?: { - encoding?: string; - fatal?: boolean; - ignoreBOM?: boolean; - }, -): AsyncIterableIterator { - 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 = []; - } - } -} diff --git a/io/read_lines_test.ts b/io/read_lines_test.ts deleted file mode 100644 index bf98e65e1..000000000 --- a/io/read_lines_test.ts +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -// This code has been ported almost directly from Go's src/bytes/buffer_test.go -// Copyright 2009 The Go Authors. All rights reserved. BSD license. -// https://github.com/golang/go/blob/master/LICENSE -import { assertEquals } from "@std/assert"; -import { Buffer } from "./buffer.ts"; -import { readLines } from "./read_lines.ts"; -import { readStringDelim } from "./read_string_delim.ts"; - -/** @todo (iuioiua) Can these tests be separated? */ -Deno.test("readStringDelimAndLines", async function () { - const enc = new TextEncoder(); - const data = new Buffer( - enc.encode("Hello World\tHello World 2\tHello World 3"), - ); - const chunks_ = []; - - for await (const c of readStringDelim(data, "\t")) { - chunks_.push(c); - } - - assertEquals(chunks_.length, 3); - assertEquals(chunks_, ["Hello World", "Hello World 2", "Hello World 3"]); - - const linesData = new Buffer(enc.encode("0\n1\n2\n3\n4\n5\n6\n7\n8\n9")); - const linesDataWithTrailingNewLine = new Buffer(enc.encode("1\n2\n3\n")); - // consider data with windows newlines too - const linesDataWindows = new Buffer( - enc.encode("0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9"), - ); - const lines_ = []; - - for await (const l of readLines(linesData)) { - lines_.push(l); - } - - assertEquals(lines_.length, 10); - assertEquals(lines_, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]); - - lines_.length = 0; - for await (const l of readLines(linesDataWithTrailingNewLine)) { - lines_.push(l); - } - - assertEquals(lines_.length, 3); - assertEquals(lines_, ["1", "2", "3"]); // No empty line at the end - - // Now test for "windows" lines - lines_.length = 0; - for await (const l of readLines(linesDataWindows)) { - lines_.push(l); - } - assertEquals(lines_.length, 10); - assertEquals(lines_, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]); -}); - -Deno.test("readLinesWithEncodingISO-8859-15", async function () { - const lines_ = []; - using file_ = await Deno.open("./io/testdata/iso-8859-15.txt"); - - for await (const l of readLines(file_, { encoding: "iso-8859-15" })) { - lines_.push(l); - } - - assertEquals(lines_.length, 12); - assertEquals(lines_, [ - "\u0020!\"#$%&'()*+,-./", - "0123456789:;<=>?", - "@ABCDEFGHIJKLMNO", - "PQRSTUVWXYZ[\\]^_", - "`abcdefghijklmno", - "pqrstuvwxyz{|}~", - "\u00a0¡¢£€¥Š§š©ª«¬\u00ad®¯", - "°±²³Žµ¶·ž¹º»ŒœŸ¿", - "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ", - "ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß", - "àáâãäåæçèéêëìíîï", - "ðñòóôõö÷øùúûüýþÿ", - ]); -});