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

correct get() result for buffer ending between CR and LF #3

Merged
merged 1 commit into from
Jul 18, 2017
Merged
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
14 changes: 12 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ function get(fileLocation, _, callback) {

var reader = fs.createReadStream(fileLocation);
var ran = false;
var tentativePreviousString;
var tentativeEndingType;
reader.on('data', function(buffer) {
if (!ran) {
var matched = buffer.toString().match(/\r\n|\r|\n/);
var str = (tentativePreviousString || '') + buffer.toString();
var matched = str.match(/\r\n|\r|\n/);
var returned = {
'\r': 'CR',
'\n': 'LF',
'\r\n': 'CRLF',
}[matched];
if (matched && returned === 'CR') { // handle case where current buffer ends between CR and LF of CRLF
if (!str.match(/\r./)) { // make sure CR is followed by something other than end of string
tentativePreviousString = str;
tentativeEndingType = 'CR';
return;
}
}
if (matched) {
ran = true;
reader.destroy();
Expand All @@ -30,7 +40,7 @@ function get(fileLocation, _, callback) {
reader.on('end', function(buffer) {
if (!ran) {
ran = true;
callback(null, 'NA');
callback(null, tentativeEndingType || 'NA');
}
});
});
Expand Down
23 changes: 23 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ describe('crlf', function() {
};
beforeEach(cleanup);
afterEach(cleanup);
var longLine = "";
var bufferLength = 1024 * 64; // fs.createReadStream buffer length
while (longLine.length < bufferLength) {
longLine += "LongLine";
}
longLine = longLine.substr(0, bufferLength - 1);

describe('get()', function() {
it('can check line ending settings for linux', function(done) {
Expand Down Expand Up @@ -46,6 +52,23 @@ describe('crlf', function() {
done();
});
});

it('can handle files where buffer ends between \\r and \\n of \\r\\n', function(done) {
var betweenLines = [longLine, longLine.substr(0, -1)].concat(lines); // this will cause two buffers ending between CR and LF in a row
fs.writeFileSync(fileLocation, betweenLines.join('\r\n'));
crlf.get(fileLocation, null, function(err, ending) {
assert.equal(ending, 'CRLF');
done();
});
});

it('can handle files where buffer ends between \\r and EOF', function(done) {
fs.writeFileSync(fileLocation, [longLine, ''].join('\r'));
crlf.get(fileLocation, null, function(err, ending) {
assert.equal(ending, 'CR');
done();
});
});
});

describe('set()', function() {
Expand Down