Closed kt3k closed 2 months ago
Does this not already exist via TextLineStream
?
import { TextLineStream } from '@std/streams'
for await (
const line of (await Deno.open('./deno.json'))
.readable
.pipeThrough(new TextEncoderStream())
.pipeThrough(new TextLineStream())
)
console.log(line)
I'm impartial to adding this. The above code snippet using TextLineStream()
seems to suffice.
import { TextLineStream } from '@std/streams' for await ( const line of (await Deno.open('./deno.json')) .readable .pipeThrough(new TextEncoderStream()) .pipeThrough(new TextLineStream()) ) console.log(line)
The above example is correct, but it looks a bit cluttered for such typical simple task, compared to other languages/runtimes.
Reading lines from a file is much simpler in other languages.
python
file = open('README.md', 'r')
for line in file.readlines():
print(line)
perl
open(INFO, "README.md") or die();
foreach $line (<INFO>) {
print $line;
}
I think it would be useful to have special/ergonomic wrapper for such typical task
If something like this is to be added, I think a toLines(stream)
to align with toText
and toJSON
would be best. Although on the other hand somebody might assume it would return an iterable of strings instead of an async iterable of strings.
toLines
sounds good to me
I agree with the comment by @iuioiua — this seems convenient, but also somewhat trivial.
If it is decided to be added, I think that the return type should be ReadableStream<string>
to provide more convenience value: it already implements AsyncIterable<string>
and can also be piped, etc.
Idea from some discussion: A quick helper for getting an async iterable (or maybe another stream) of lines from a ReadableStream.
Example signature:
lines(readable: ReadableStream<Uint8Array>|ReadableStream<string>): AsyncIterable<string>