0

I want to read some lines from a large csv file. A quick search here on SO pointed me to the 'lazy' module. Here's my attempt:

items = []
stream = fs.createReadStream src
lazy(stream)
    .lines
    .skip(1)
    .take(5)
    .forEach((line)->
        items.push line.toString())
    .on('end', ->
        console.log items)

But it doesn't print anything. What am I missing?

tldr
  • 11,924
  • 15
  • 75
  • 120

1 Answers1

0

The 'end' event doesn't appear to be emitted down the chain. Only 'data' and 'pipe' are.

Based on the definition of .join(), 'pipe' seems to be what lazy uses.

# ...
    .forEach((line)->
        items.push line.toString())
    .on('pipe', () ->
        console.log items)

You could also just use .join() to stay with lazy's own API:

# ...
    .forEach((line)->
        items.push line.toString())
    .join(() ->
        console.log items)

Side note: you don't necessarily need to collect the items yourself. You can also use .map():

lazy(stream)
    .lines
    .skip(1)
    .take(5)
    .map(item -> item.toString())
    .join((items) -> console.log items)

Or possibly with just:

# ...
    .map(String)
    .join(console.log);
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199