Comparing version 0.0.0 to 0.1.0
"use strict"; | ||
var Transform = require("stream").Transform; | ||
var util = require("util"); | ||
/** | ||
Process each chunk of a stream via the supplied function. | ||
@module stream-via | ||
*/ | ||
module.exports = via; | ||
via.async = viaAsync; | ||
function SimpleTransform(options){ | ||
if (!(this instanceof SimpleTransform)) return new SimpleTransform(options); | ||
Transform.call(this, options); | ||
this.buf = new Buffer(0); | ||
} | ||
util.inherits(SimpleTransform, Transform); | ||
/** | ||
@param {function} | ||
@param [options] {object} - passed to Transform constructor | ||
@return {external:Duplex} | ||
@alias module:stream-via | ||
*/ | ||
function via(transformFunction, options){ | ||
var stream = SimpleTransform(options); | ||
stream._transform = function(chunk, enc, done){ | ||
if (chunk){ | ||
try { | ||
this.push(transformFunction(chunk, enc)); | ||
} catch(err){ | ||
stream.emit("error", err); | ||
} | ||
} | ||
done(); | ||
}; | ||
return stream; | ||
} | ||
function viaAsync(transformFunction, options){ | ||
var stream = SimpleTransform(options); | ||
stream._transform = function(chunk, enc, done){ | ||
if (chunk){ | ||
try { | ||
this.push(transformFunction(chunk, enc, done)); | ||
} catch(err){ | ||
stream.emit("error", err); | ||
done(); | ||
} | ||
} | ||
}; | ||
return stream; | ||
} | ||
/** | ||
@external Duplex | ||
@see https://nodejs.org/api/stream.html#stream_class_stream_duplex | ||
*/ |
{ | ||
"name": "stream-via", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "stream-via", | ||
@@ -9,6 +9,9 @@ "repository": "https://github.com/75lb/stream-via.git", | ||
"main": "./lib/stream-via.js", | ||
"bin": { | ||
"stream-via": "bin/cli.js" | ||
}, | ||
"keywords": [], | ||
"keywords": [ | ||
"through", | ||
"stream", | ||
"process", | ||
"chunk", | ||
"transform" | ||
], | ||
"engines": { | ||
@@ -19,5 +22,9 @@ "node": ">=0.10.0" | ||
"test": "tape test/*.js", | ||
"lint": "jshint lib/*.js bin/*.js test/*.js; echo", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo" | ||
}, | ||
"devDependencies": { | ||
"buffer-equal": "0.0.1", | ||
"jsdoc-to-markdown": "^1.1.1", | ||
"tape": "^4.0.0" | ||
} | ||
} |
@@ -5,4 +5,19 @@ [![view on npm](http://img.shields.io/npm/v/stream-via.svg)](https://www.npmjs.org/package/stream-via) | ||
[![Dependency Status](https://david-dm.org/75lb/stream-via.svg)](https://david-dm.org/75lb/stream-via) | ||
![Analytics](https://ga-beacon.appspot.com/UA-27725889-6/stream-via/README.md?pixel) | ||
#stream-via | ||
<a name="module_stream-via"></a> | ||
## stream-via | ||
Process each chunk of a stream via the supplied function. | ||
<a name="exp_module_stream-via--via"></a> | ||
### via(transformFunction, [options]) ⇒ <code>[Duplex](https://nodejs.org/api/stream.html#stream_class_stream_duplex)</code> ⏏ | ||
**Kind**: Exported function | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| transformFunction | <code>function</code> | | | ||
| [options] | <code>object</code> | passed to Transform constructor | | ||
* * * | ||
© 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
var test = require("tape"); | ||
var lib = require("../"); | ||
var via = require("../"); | ||
var PassThrough = require("stream").PassThrough; | ||
var bufferEquals = require("buffer-equal"); | ||
test("first", function(t){ | ||
test("via(func) - utf8", function(t){ | ||
var stream = via(function(data){ | ||
return data + "yeah?"; | ||
}); | ||
stream.on("readable", function(){ | ||
var chunk = this.read(); | ||
if (chunk){ | ||
t.strictEqual(chunk, "cliveyeah?"); | ||
t.end(); | ||
} | ||
}); | ||
stream.setEncoding("utf8"); | ||
stream.end("clive"); | ||
}); | ||
test("via(func) - buffer", function(t){ | ||
var stream = via(function(data){ | ||
return Buffer.concat([ data, Buffer([ 2 ])]); | ||
}); | ||
stream.on("readable", function(){ | ||
var chunk = this.read(); | ||
if (chunk){ | ||
t.ok(bufferEquals(chunk, Buffer([ 1, 2 ]))); | ||
t.end(); | ||
} | ||
}); | ||
stream.end(Buffer([ 1 ])); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
5748
84
23
0
3
8