node-lazy icon indicating copy to clipboard operation
node-lazy copied to clipboard

Newline lines from .lines from createFileStream converted to 0

Open thegoleffect opened this issue 14 years ago • 5 comments

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?

thegoleffect avatar Nov 30 '11 23:11 thegoleffect

It's not intentional.

pkrumins avatar Dec 01 '11 15:12 pkrumins

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));
        ...

krjkkim avatar Feb 13 '12 08:02 krjkkim

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');
  });

mhart avatar May 03 '12 06:05 mhart

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

opatry avatar Jul 12 '13 00:07 opatry

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!

netj avatar Aug 11 '13 08:08 netj