Last active
August 29, 2015 13:56
-
-
Save heapwolf/9022855 to your computer and use it in GitHub Desktop.
a transform stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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