Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,32 @@ lineStream.pipe(output);

```

# Line Endings

By default, byline matches end of line according to the following regular expression:

```regexp
/\r\n|[\n\v\f\r\x85\u2028\u2029]/g
```

You can override this default and specify your own regular expression for matching end of line for
your data.

```javascript
var stream = fs.createReadStream('sample.txt');
stream = byline.createStream(stream,{newLines: /\n/g});

// ...
```

or

```javascript
var stream = byline(fs.createReadStream('sample.txt', { encoding: 'utf8' }),{newLines: /\n/g});

// ...
```

# Empty Lines

By default byline skips empty lines, if you want to keep them, pass the `keepEmptyLines` option in
Expand Down
3 changes: 2 additions & 1 deletion lib/byline.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function LineStream(options) {
// which re-concatanates the lines, just without newlines.
this._readableState.objectMode = true;
this._lineBuffer = [];
this._split = options.newLines || /\r\n|[\n\v\f\r\x85\u2028\u2029]/g;
this._keepEmptyLines = options.keepEmptyLines || false;
this._lastChunkEndedWithCR = false;

Expand Down Expand Up @@ -100,7 +101,7 @@ LineStream.prototype._transform = function(chunk, encoding, done) {
this._chunkEncoding = encoding;

// see: http://www.unicode.org/reports/tr18/#Line_Boundaries
var lines = chunk.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g);
var lines = chunk.split(this._split);

// don't split CRLF which spans chunks
if (this._lastChunkEndedWithCR && chunk[0] == '\n') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"devDependencies": {
"mocha": "~2.1.0",
"request": "~2.27.0"
"request": "^2.88.0"
},
"scripts": {
"test": "mocha -R spec --timeout 60000"
Expand Down
Binary file added test/rfc-DOS.txt
Binary file not shown.
16 changes: 16 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ describe('byline', function() {
});
});

it('should match given newline separators using newLine option', function(done) {
var input = fs.createReadStream('test/rfc-DOS.txt');
var lineStream = byline(input, { keepEmptyLines: true, newLines: /\r\n/g });
lineStream.setEncoding('utf8');

var lines = [];
lineStream.on('data', function(line) {
lines.push(line);
});

lineStream.on('end', function() {
assert.equal(9859, lines.length);
done();
});
});

it('should not split a CRLF which spans two chunks', function(done) {
var input = fs.createReadStream('test/CRLF.txt');
var lineStream = byline(input, { keepEmptyLines: true });
Expand Down