| 'use strict'; | ||
| var inherits = require('util').inherits; | ||
| var Transform = require('stream').Transform; | ||
| var isString = require('lodash.isstring'); | ||
| var isFunction = require('lodash.isfunction'); | ||
| var isBoolean = require('lodash.isboolean'); | ||
| /** | ||
| * Collects all data that passes through and then calls callback(data). | ||
| * The value of `data` will be an array if options.objectMode is true. | ||
| * Otherwise, it will be a string if options.encoding is defined. Otherwise, | ||
| * it will be a buffer. | ||
| */ | ||
| function Collect(callback, options) { | ||
| Transform.call(this, options); | ||
| this.callback = callback; | ||
| this.encoding = options.encoding; | ||
| this.objectMode = options.objectMode; | ||
| this.data = []; | ||
| } | ||
| inherits(Collect, Transform); | ||
| Collect.prototype._transform = function(chunk, enc, next) { | ||
| this.data.push(chunk); | ||
| this.push(chunk); | ||
| next(); | ||
| }; | ||
| Collect.prototype._flush = function(done) { | ||
| if (this.objectMode) { | ||
| this.callback(this.data); | ||
| } | ||
| else { | ||
| var data = Buffer.concat(this.data); | ||
| if (this.encoding) this.callback(data.toString(this.encoding)); | ||
| else this.callback(data); | ||
| } | ||
| done(); | ||
| }; | ||
| module.exports = function(/*[encoding], [objectMode], callback, [options]*/) { | ||
| var encoding, objectMode, callback, options; | ||
| // Create an array of arguments | ||
| var args = Array.prototype.slice.call(arguments); | ||
| // Determine if user has provided an encoding argument | ||
| if (isString(args[0])) { | ||
| // Ensure it is a valid encoding | ||
| if (Buffer.isEncoding(args[0])) encoding = args.shift(); | ||
| else throw new RangeError('Encoding not recognized'); | ||
| } | ||
| // Determine whether user has provided a boolean for object mode. | ||
| if (isBoolean(args[0])) objectMode = args.shift(); | ||
| // Ensure user has provided a callback function | ||
| if (isFunction(args[0])) callback = args.shift(); | ||
| else throw new Error('Please provide a callback function'); | ||
| // Determine whether user has provided an options argument and add encoding | ||
| // and/or objectMode if these have been provided. | ||
| options = args[0] || {}; | ||
| if (encoding) options.encoding = encoding; | ||
| if (objectMode) { | ||
| options.objectMode = objectMode; | ||
| options.encoding = null; | ||
| } | ||
| return new Collect(callback, options); | ||
| }; |
+13
-6
@@ -5,2 +5,3 @@ 'use strict'; | ||
| var Readable = require('stream').Readable; | ||
| var isArray = require('lodash.isarray'); | ||
@@ -10,4 +11,4 @@ /** | ||
| */ | ||
| function InitStream(data, opts) { | ||
| Readable.call(this, opts); | ||
| function InitStream(data, options) { | ||
| Readable.call(this, options); | ||
| this.data = data; | ||
@@ -19,4 +20,4 @@ } | ||
| InitStream.prototype._read = function() { | ||
| this.push(this.data); | ||
| this.push(null); | ||
| this.push(this.data.shift()); | ||
| if (this.data.length === 0) this.push(null); | ||
| }; | ||
@@ -27,4 +28,10 @@ | ||
| */ | ||
| module.exports = function(data, opts) { | ||
| return new InitStream(data, opts); | ||
| module.exports = function(data, options) { | ||
| data = (isArray(data)) ? data.slice() : [data]; | ||
| options = options || {}; | ||
| options.objectMode = options.objectMode || true; | ||
| return new InitStream(data, options); | ||
| }; |
+18
-8
| { | ||
| "name": "noria", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "description": "A collection of utilities and Classes for working with Node.js streams", | ||
@@ -12,3 +12,5 @@ "main": "lib/index.js", | ||
| "scripts": { | ||
| "test": "mocha" | ||
| "test": "mocha", | ||
| "coverage": "istanbul cover _mocha && open ./coverage/lcov-report/index.html", | ||
| "coveralls": "istanbul cover _mocha && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage" | ||
| }, | ||
@@ -23,8 +25,8 @@ "engines": { | ||
| "keywords": [ | ||
| "stream", | ||
| "utility", | ||
| "util", | ||
| "classes", | ||
| "node", | ||
| "nodejs" | ||
| "stream", | ||
| "utility", | ||
| "util", | ||
| "classes", | ||
| "node", | ||
| "nodejs" | ||
| ], | ||
@@ -39,5 +41,13 @@ "author": "Akim McMath <akim.elijah.mcmath@gmail.com>", | ||
| "chai": "^3.4.1", | ||
| "coveralls": "^2.11.4", | ||
| "event-stream": "^3.3.2", | ||
| "istanbul": "^0.4.0", | ||
| "mocha": "^2.3.4" | ||
| }, | ||
| "dependencies": { | ||
| "lodash.isarray": "^3.0.4", | ||
| "lodash.isboolean": "^3.0.1", | ||
| "lodash.isfunction": "^3.0.6", | ||
| "lodash.isstring": "^3.0.1" | ||
| } | ||
| } |
+43
-15
| # Noria | ||
| [](https://npmjs.org/package/noria) | ||
| [](https://www.npmjs.com/package/noria) | ||
| [](https://travis-ci.org/akim-mcmath/noria) | ||
| [](https://img.shields.io/gemnasium/akim-mcmath/noria) | ||
| [](LICENSE.md) | ||
| [](https://travis-ci.org/akim-mcmath/noria) | ||
| [](https://coveralls.io/github/akim-mcmath/noria?branch=master) | ||
| [](https://gemnasium.com/akim-mcmath/noria) | ||
| A collection of utilities and Classes for working with | ||
| A collection of utilities for working with | ||
| Node.js [streams](https://nodejs.org/api/stream.html). | ||
@@ -20,5 +21,32 @@ | ||
| #### collect([encoding], [objectMode], callback, [options]) | ||
| * `encoding` String (optional) - alias for `options.encoding`. | ||
| * `objectMode` Boolean (optional) - alias for `options.objectMode`. | ||
| * `callback` Function - Called after all data has been collected. Takes one | ||
| `data` parameter. | ||
| * `options` Object (optional) - Options passed to the stream. | ||
| Returns a new [Transform](https://nodejs.org/api/stream.html#stream_class_stream_transform) | ||
| stream that buffers all incoming data and then calls `callback(data)`. Or, if | ||
| `objectMode` is true, `data` will be an array containing each object that has | ||
| passed through. If `encoding` is defined, `data` will be converted to a string | ||
| before it is passed to `callback`. | ||
| The `options` parameter is identical to that of Node's | ||
| [Transform stream constructor](https://nodejs.org/api/stream.html#stream_new_stream_transform_options). | ||
| The following will print the contents of a file to the console before writing | ||
| it to a new location. | ||
| ```js | ||
| fs.createReadStream('liftoff-file.txt') | ||
| .pipe(noria.collect('utf8', function(data) { | ||
| console.log(data); | ||
| })) | ||
| .pipe(fs.createWriteStrem('landing-file.txt')); | ||
| ``` | ||
| #### init(data, [options]) | ||
| * `data` Buffer | String | Object - The data to pass to the stream. | ||
| * `data` - The data to pass to the stream. | ||
| * `options` Object (optional) - Options to pass to the stream. | ||
@@ -28,15 +56,16 @@ | ||
| [Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable) | ||
| stream initialized with some arbitrary data. The `options` parameter is | ||
| identical that of Node's | ||
| stream initialized with some arbitrary data. If `data` is an array, each | ||
| element in the array will be treated as a chunk of data. options.objectMode is | ||
| true by default. The `options` parameter is identical that of Node's | ||
| [Readable stream constructor](https://nodejs.org/api/stream.html#stream_new_stream_readable_options). | ||
| To pass an Object as the `data` argument, ensure that you set | ||
| `objectMode: true`. | ||
| ```js | ||
| const noria = require('noria'); | ||
| // Initializes with one chunk of type String. | ||
| noria.init('Colorless green ideas sleep furiously.'); | ||
| noria.init('Colorless green ideas sleep furiously.') | ||
| .pipe(process.stdout) | ||
| // Initializes with three chunks of various types. | ||
| noria.init([42, 'than', {name: 'adams'}]); | ||
| // Prints: 'Colorless green ideas sleep furiously.' | ||
| // Initializes with one chunk of type Array. | ||
| noria.init([['the', 'universe']]); | ||
| ``` | ||
@@ -59,3 +88,2 @@ | ||
| ```js | ||
| const noria = require('noria'); | ||
| const fs = require('fs'); | ||
@@ -76,2 +104,2 @@ | ||
| Copyright © 2015 Akim McMath. Licensed under the MIT License. | ||
| Copyright © 2015 Akim McMath. Licensed under the [MIT License](LICENSE.md). |
10151
73.82%7
16.67%115
130%102
37.84%4
Infinity%5
66.67%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added