Browse Source

readline: don't emit "line" events with a trailing '\n' char

Before this commit, readline was inconsistent in whether or not it would emit
"line" events with or without the trailing "\n" included. When "terminal"
mode was true, then there would be no "\n", when it was false, then the "\n"
would be present. However, the trailing "\n" doesn't add much, and most of the
time people just end up stripping it manually.

Part of #4243.
v0.9.4-release
Nathan Rajlich 12 years ago
parent
commit
e95e095289
  1. 2
      lib/readline.js
  2. 12
      test/simple/test-readline-interface.js

2
lib/readline.js

@ -308,7 +308,7 @@ Interface.prototype._normalWrite = function(b) {
string = lines.pop();
this._line_buffer = string;
lines.forEach(function(line) {
this._onLine(line + '\n');
this._onLine(line);
}, this);
} else if (string) {
// no newlines this time, save what we have for next time

12
test/simple/test-readline-interface.js

@ -43,7 +43,7 @@ rli = new readline.Interface(fi, {});
called = false;
rli.on('line', function(line) {
called = true;
assert.equal(line, 'asdf\n');
assert.equal(line, 'asdf');
});
fi.emit('data', 'asdf\n');
assert.ok(called);
@ -54,7 +54,7 @@ rli = new readline.Interface(fi, {});
called = false;
rli.on('line', function(line) {
called = true;
assert.equal(line, '\n');
assert.equal(line, '');
});
fi.emit('data', '\n');
assert.ok(called);
@ -76,7 +76,7 @@ rli = new readline.Interface(fi, {});
called = false;
rli.on('line', function(line) {
called = true;
assert.equal(line, 'a\n');
assert.equal(line, 'a');
});
fi.emit('data', 'a');
assert.ok(!called);
@ -93,7 +93,7 @@ rli.on('line', function(line) {
assert.equal(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', expectedLines.join(''));
fi.emit('data', expectedLines.join('\n') + '\n');
assert.equal(callCount, expectedLines.length);
rli.close();
@ -106,7 +106,7 @@ rli.on('line', function(line) {
assert.equal(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', expectedLines.join(''));
fi.emit('data', expectedLines.join('\n'));
assert.equal(callCount, expectedLines.length - 1);
rli.close();
@ -117,7 +117,7 @@ rli = new readline.Interface(fi, {});
callCount = 0;
rli.on('line', function(line) {
callCount++;
assert.equal(line, buf.toString('utf8') + '\n');
assert.equal(line, buf.toString('utf8'));
});
[].forEach.call(buf, function(i) {
fi.emit('data', Buffer([i]));

Loading…
Cancel
Save