mirror of
https://github.com/denoland/std.git
synced 2024-11-21 12:40:03 +00:00
BREAKING(io): remove readLines()
(#5991)
* deprecation(io/unstable): deprecate `readLines()` * BREAKING(io/unstable): remove `readLines()`
This commit is contained in:
parent
511585aa97
commit
034549bb36
@ -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",
|
||||
|
@ -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";
|
||||
|
@ -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<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 = [];
|
||||
}
|
||||
}
|
||||
}
|
@ -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®¯",
|
||||
"°±²³Žµ¶·ž¹º»ŒœŸ¿",
|
||||
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ",
|
||||
"ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß",
|
||||
"àáâãäåæçèéêëìíîï",
|
||||
"ðñòóôõö÷øùúûüýþÿ",
|
||||
]);
|
||||
});
|
Loading…
Reference in New Issue
Block a user