Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request(streams): lines(readable): AsyncIterable<string> #5071

Closed
kt3k opened this issue Jun 18, 2024 · 6 comments · Fixed by #5121
Closed

feature request(streams): lines(readable): AsyncIterable<string> #5071

kt3k opened this issue Jun 18, 2024 · 6 comments · Fixed by #5121

Comments

@kt3k
Copy link
Member

kt3k commented Jun 18, 2024

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>

import { lines } from "@std/streams";

const file = await Deno.open(filename);
for await (const line of lines(file.readable)) {
  console.log(line);
}
@kt3k kt3k changed the title feature request(streams): lines(readable: ReadableStream<Uint8Array>|ReadableStream<string>): AsyncIterable<string> feature request(streams): lines(readable): AsyncIterable<string> Jun 18, 2024
@BlackAsLight
Copy link
Contributor

BlackAsLight commented Jun 18, 2024

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)

@iuioiua
Copy link
Collaborator

iuioiua commented Jun 18, 2024

I'm impartial to adding this. The above code snippet using TextLineStream() seems to suffice.

@kt3k
Copy link
Member Author

kt3k commented Jun 19, 2024

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

@BlackAsLight
Copy link
Contributor

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.

@kt3k
Copy link
Member Author

kt3k commented Jun 20, 2024

toLines sounds good to me

@jsejcksn
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants