Skip to content

Instantly share code, notes, and snippets.

@heapwolf
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heapwolf/9022855 to your computer and use it in GitHub Desktop.
Save heapwolf/9022855 to your computer and use it in GitHub Desktop.
a transform stream
var stream = require('stream')
module.exports = function(opts) {
opts = opts || { objectMode: true }
return function(fn) {
var s = new stream.Transform(opts)
s._transform = function (chunk, encoding, done) {
var that = this;
var fin = function(data) {
if (typeof data !== 'undefined') {
that.push(data)
}
done()
}
var ret = fn.call(this, chunk, fin)
if(ret) {
this.push(ret)
done()
}
}
s._flush = function (done) {
var ret = fn.call(this, null, done)
if(ret || ret === null) {
done()
}
}
return s
}
}
{
"name": "txs",
"version": "1.0.0",
"description": "transform stream",
"main": "index.js",
"scripts": {
"test": "node ./test.js"
},
"keywords": [
"transform",
"through",
"throughstream",
"throughstreams",
"streams",
"streaming"
],
"author": "hij1nx",
"license": "MIT",
"dependencies": {},
"repository": {
"type": "git",
"url": "https://gist.github.com/hij1nx/9022855"
},
"devDependencies": {
"better-assert": "~1.0.0"
}
}
var ASSERT = require('assert').ok
var txs = require('./index')()
var fs = require('fs')
var Stream = require('stream').Stream
var s1 = txs(function(obj, done) {
return obj
})
var s2 = txs(function(obj, done) {
done(obj)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment