Skip to content

Commit 26ca7d7

Browse files
jeffbskitrevnorris
authored andcommitted
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.
1 parent 278183a commit 26ca7d7

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/_stream_transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Transform.prototype._write = function(chunk, encoding, cb) {
174174
Transform.prototype._read = function(n) {
175175
var ts = this._transformState;
176176

177-
if (ts.writechunk && ts.writecb && !ts.transforming) {
177+
if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
178178
ts.transforming = true;
179179
this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
180180
} else {

test/simple/test-stream2-transform.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ test('passthrough', function(t) {
104104
t.end();
105105
});
106106

107+
test('object passthrough', function (t) {
108+
var pt = new PassThrough({ objectMode: true });
109+
110+
pt.write(1);
111+
pt.write(true);
112+
pt.write(false);
113+
pt.write(0);
114+
pt.write('foo');
115+
pt.write('');
116+
pt.write({ a: 'b'});
117+
pt.end();
118+
119+
t.equal(pt.read(), 1);
120+
t.equal(pt.read(), true);
121+
t.equal(pt.read(), false);
122+
t.equal(pt.read(), 0);
123+
t.equal(pt.read(), 'foo');
124+
t.equal(pt.read(), '');
125+
t.same(pt.read(), { a: 'b'});
126+
t.end();
127+
});
128+
107129
test('simple transform', function(t) {
108130
var pt = new Transform;
109131
pt._transform = function(c, e, cb) {
@@ -126,6 +148,32 @@ test('simple transform', function(t) {
126148
t.end();
127149
});
128150

151+
test('simple object transform', function(t) {
152+
var pt = new Transform({ objectMode: true });
153+
pt._transform = function(c, e, cb) {
154+
pt.push(JSON.stringify(c));
155+
cb();
156+
};
157+
158+
pt.write(1);
159+
pt.write(true);
160+
pt.write(false);
161+
pt.write(0);
162+
pt.write('foo');
163+
pt.write('');
164+
pt.write({ a: 'b'});
165+
pt.end();
166+
167+
t.equal(pt.read(), '1');
168+
t.equal(pt.read(), 'true');
169+
t.equal(pt.read(), 'false');
170+
t.equal(pt.read(), '0');
171+
t.equal(pt.read(), '"foo"');
172+
t.equal(pt.read(), '""');
173+
t.equal(pt.read(), '{"a":"b"}');
174+
t.end();
175+
});
176+
129177
test('async passthrough', function(t) {
130178
var pt = new Transform;
131179
pt._transform = function(chunk, encoding, cb) {

0 commit comments

Comments
 (0)