pkrumins / node-lazy

lazy lists for node.js
http://www.catonmat.net
488 stars 52 forks source link

Newline lines from .lines from createFileStream converted to 0 #11

Open thegoleffect opened 12 years ago

thegoleffect commented 12 years ago

I'm using code like this:

new Lazy(fs.createReadStream(fpath, {flags: 'r', encoding: 'utf8'}))
  .on("end", finished)
  .lines
  .forEach((line) ->
    all_lines.push(line)
  )

My files have blank lines that are just newline characters. Any lines like that get converted to "\u0030" aka the "0" character. Is this intentional?

pkrumins commented 12 years ago

It's not intentional.

krjkkim commented 12 years ago

I had the same problem and I looked at the source code. The following code looks suspicious

self.__defineGetter__('lines', function () {
        ...
            if(i>0) chunkArray.push(chunk.slice(lastNewLineIndex, i));
        ...

By simply removing the guarding if statement seems to solve the problem.

self.__defineGetter__('lines', function () {
        ...
            chunkArray.push(chunk.slice(lastNewLineIndex, i));
        ...
mhart commented 12 years ago

I'm seeing the same thing. Here's a simple test case that should just echo each line out as is (works fine for non-blank lines, produces undefined for each blank line if run interactively and 0 if piped):

Lazy = require('lazy');

process.stdin.resume();
process.stdin.setEncoding('utf8');

Lazy(process.stdin)
  .lines
  .map(String)
  .forEach(function(line) {
    process.stdout.write(line + '\n');
  });
opatry commented 11 years ago

Please, what is the status according to this issue? I'm stuck with that…

netj commented 11 years ago

Hi @opatry, I was also stuck with this problem and found a fix. Hope my fix also works for you.

Hi @pkrumins, could you please merge my fix and create a release soon, so I can simply install lazy from npm? You can easily check the bug and my fix with the following coffeescript snippet:

lines = ["a", "", "b"]
Lazy = require("lazy")
p = require("child_process").spawn("sh", ["-c", ("echo #{l}" for l in lines).join("; ")])
new Lazy(p.stdout).lines.map(String).join((x) -> console.log (x.join(",") is lines.join(",")), x)

Thanks!