Coder Social home page Coder Social logo

Comments (6)

kt3k avatar kt3k commented on July 19, 2024 1
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

from deno_std.

jsejcksn avatar jsejcksn commented on July 19, 2024 1

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.

from deno_std.

BlackAsLight avatar BlackAsLight commented on July 19, 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)

from deno_std.

iuioiua avatar iuioiua commented on July 19, 2024

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

from deno_std.

BlackAsLight avatar BlackAsLight commented on July 19, 2024

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.

from deno_std.

kt3k avatar kt3k commented on July 19, 2024

toLines sounds good to me

from deno_std.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.