Skip to content

Instantly share code, notes, and snippets.

@kesla
Last active December 19, 2015 07:19
Show Gist options
  • Save kesla/5918182 to your computer and use it in GitHub Desktop.
Save kesla/5918182 to your computer and use it in GitHub Desktop.
Weird behaviour with streams and falsy objects
var Transform = require('stream').Transform
var assert = require('assert')
var stream = new Transform({objectMode: true})
var read = function(stream, callback) {
var actual = []
var data
function onReadable () {
while((data = stream.read()) !== null) {
actual.push(data)
}
stream.once('readable', onReadable)
}
onReadable()
stream.once('end', function() {
callback(null, actual)
})
stream.once('error', callback)
}
stream._transform = function(chunk, encoding, callback) {
callback(null, chunk)
}
read(stream, function(err, actual) {
if (err) throw err
assert.deepEqual([1, 0, ''], actual)
})
stream.write(1)
stream.write(0)
stream.write('')
stream.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment