stream: objectMode transform should allow falsey values

If a transform stream has objectMode = true, it should
allow falsey values other than (null) like 0, false, ''.

null is reserved to indicate stream eof but other falsey
values should flow through properly.
This commit is contained in:
Jeff Barczewski 2013-03-30 22:32:06 -05:00 committed by Trevor Norris
parent 278183a902
commit 26ca7d73ca
2 changed files with 49 additions and 1 deletions

View File

@ -174,7 +174,7 @@ Transform.prototype._write = function(chunk, encoding, cb) {
Transform.prototype._read = function(n) {
var ts = this._transformState;
if (ts.writechunk && ts.writecb && !ts.transforming) {
if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
ts.transforming = true;
this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
} else {

View File

@ -104,6 +104,28 @@ test('passthrough', function(t) {
t.end();
});
test('object passthrough', function (t) {
var pt = new PassThrough({ objectMode: true });
pt.write(1);
pt.write(true);
pt.write(false);
pt.write(0);
pt.write('foo');
pt.write('');
pt.write({ a: 'b'});
pt.end();
t.equal(pt.read(), 1);
t.equal(pt.read(), true);
t.equal(pt.read(), false);
t.equal(pt.read(), 0);
t.equal(pt.read(), 'foo');
t.equal(pt.read(), '');
t.same(pt.read(), { a: 'b'});
t.end();
});
test('simple transform', function(t) {
var pt = new Transform;
pt._transform = function(c, e, cb) {
@ -126,6 +148,32 @@ test('simple transform', function(t) {
t.end();
});
test('simple object transform', function(t) {
var pt = new Transform({ objectMode: true });
pt._transform = function(c, e, cb) {
pt.push(JSON.stringify(c));
cb();
};
pt.write(1);
pt.write(true);
pt.write(false);
pt.write(0);
pt.write('foo');
pt.write('');
pt.write({ a: 'b'});
pt.end();
t.equal(pt.read(), '1');
t.equal(pt.read(), 'true');
t.equal(pt.read(), 'false');
t.equal(pt.read(), '0');
t.equal(pt.read(), '"foo"');
t.equal(pt.read(), '""');
t.equal(pt.read(), '{"a":"b"}');
t.end();
});
test('async passthrough', function(t) {
var pt = new Transform;
pt._transform = function(chunk, encoding, cb) {