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

Add option for trimming CR at the end of a line #30

Open
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Install with
* `options` - Object
* `readChunk` - Integer number of bytes to read at once. Default: 1024
* `newLineCharacter` - String new line character, only works with one byte characters for now. Default: `\n` which is `0x0a` hex encoded
* `lineTrimCR` bool - If set to true, carriege return character - `0x0d` will be trimmed if found at the end of a line. Default: false

`node-readlines` can handle files without newLineCharacter after the last line

Expand Down
86 changes: 43 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions readlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class LineByLine {

if (!options.readChunk) options.readChunk = 1024;

// Trim carriege return CR 0x0D if found at the end of a line.
if (!options.lineTrimCR) options.lineTrimCR = false;

if (!options.newLineCharacter) {
options.newLineCharacter = 0x0a; //linux line ending
} else {
Expand All @@ -26,6 +29,7 @@ class LineByLine {
this.options = options;

this.newLineCharacter = options.newLineCharacter;
this.crCharacter = 0x0d;

this.reset();
}
Expand Down Expand Up @@ -152,6 +156,10 @@ class LineByLine {
line = line.slice(0, line.length-1);
}

if (this.options.lineTrimCR === true && line && line[line.length-1] === this.crCharacter) {
line = line.slice(0, line.length-1);
}

return line;
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/linesWithCR.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
line1here
line2here
line3here
25 changes: 24 additions & 1 deletion test/readlines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ test('should reset and start from the beggining', (t) => {

test('should read big lines', (t) => {
const liner = new lineByLine(path.resolve(__dirname, 'fixtures/bigLines.json'));


t.ok(JSON.parse(liner.next()), 'line 0: valid JSON');
t.ok(JSON.parse(liner.next()), 'line 1: valid JSON');
Expand Down Expand Up @@ -130,3 +129,27 @@ test('should correctly processes NULL character in lines', (t) => {
t.equals(liner.fd, null, 'fd is null');
t.end();
})

test('should NOT trim CR characters at the end of the line', (t) => {
const liner = new lineByLine(path.resolve(__dirname, 'fixtures/linesWithCR.txt'));

t.equals(liner.next().toString(), 'line1here\r');
t.equals(liner.next().toString(), 'line2here\r');
t.equals(liner.next().toString(), 'line3here\r');

t.equals(liner.fd, null, 'fd is null');
t.end();
})

test('should trim CR characters at the end of the line', (t) => {
const liner = new lineByLine(path.resolve(__dirname, 'fixtures/linesWithCR.txt'), {
'lineTrimCR': true
});

t.equals(liner.next().toString(), 'line1here');
t.equals(liner.next().toString(), 'line2here');
t.equals(liner.next().toString(), 'line3here');

t.equals(liner.fd, null, 'fd is null');
t.end();
})