nickewing / line-reader

Asynchronous line-by-line file reader for node.js
MIT License
453 stars 75 forks source link

support regexp separators and automatically handle windows/unix/mac properly by default #25

Closed jedwards1211 closed 9 years ago

jedwards1211 commented 9 years ago

This change makes the default separator /\r\n?|\n/, so that it automatically handles windows, mac, or unix files properly. It still supports string separators as well. If the regexp match is at the very end of the string, it will try to read more so in case there is a longer match.

I added tests for windows, mac, and unix files, and also a test where the buffer boundary falls in the middle of an \r\n (it should still parse as a single line break with the default settings).

This also fixes https://github.com/nickewing/line-reader/issues/24 by adding this check to the beginning of getLine():

    function nextLine(cb) {
      function getLine() {
        if (separatorIndex < 0 && eof) {
          separatorIndex = bufferStr.length;
        }

Cheers! Andy

nickewing commented 9 years ago

Hi @jedwards1211,

This change looks great. Thank you for taking the time to fix this issue!

jedwards1211 commented 9 years ago

No problem! I may try to fix some others in the future, depending on if I have time. I adapted the code into a synchronous line reader for my own use...would you be interested in incorporating that into this project?

nickewing commented 9 years ago

Sure, it would be great to have that as an option as long as the current functionality is preserved as well.

jedwards1211 commented 9 years ago

Right, it would be a separate thing