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

Trailing EOL treated as an additional empty line

Open hashtagchris opened this issue 7 years ago • 0 comments

byline with keepEmptyLines: true behaves slightly differently than most line readers I've encountered, including node's readline module. I created sample.txt by running:

echo foo> sample.txt
echo bar>> sample.txt
echo>> sample.txt
echo and baz>> sample.txt

byline will return 5 chunks for this file (or 3 with keepEmptyLines: false). The readline sample returns four lines:

Line from file: foo
Line from file: bar
Line from file:
Line from file: and baz

As does this C# program:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        using (var stream = new FileStream("sample.txt", FileMode.Open))
        using (var reader = new StreamReader(stream))
        {
            string line;
            while (null != (line = reader.ReadLine()))
            {
                Console.WriteLine($"Line from file: {line}");
            }
        }
    }
}

(perl -ne 'print "Line from file: $_";' sample.txt also prints 4 lines, but with newlines still attached.)

If byline's current behavior is considered by design, it would be nice if there was a new option to ignore the trailing EOL in a stream. This would make byline more of a drop-in replacement for readline.

hashtagchris avatar Nov 22 '17 04:11 hashtagchris