Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement newLines option allowing custom regular expression for matching new lines #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
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
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
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
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