├── .npmignore ├── .gitignore ├── test ├── 1.js ├── 4.js ├── 3.js └── 2.js ├── .travis.yml ├── LICENSE ├── index.js ├── package.json └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | *.*~ 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.swp 8 | *.pid 9 | *.gz 10 | *.tgz 11 | *.lock 12 | 13 | .travis.yml 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.*~ 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.swp 8 | *.pid 9 | *.gz 10 | *.tgz 11 | 12 | node_modules 13 | npm-debug.log 14 | -------------------------------------------------------------------------------- /test/1.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | , streamify 3 | ; 4 | 5 | test('require', function(t) { 6 | streamify = require('..'); 7 | 8 | t.ok(streamify, 'stream-array exists'); 9 | t.equal(typeof(streamify), 'function', 'require returns an object'); 10 | t.equal(0, Object.keys(streamify).length, 'No hidden exports exports'); 11 | t.equal(1, streamify.length, 'No hidden arguments'); 12 | t.end(); 13 | }); 14 | -------------------------------------------------------------------------------- /test/4.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | , streamify = require('..') 3 | , concat = require('concat-stream') 4 | ; 5 | 6 | test('immutable', function(t) { 7 | var s, a = [1, 2, 3, 4, 5]; 8 | 9 | s = streamify(a); 10 | s.pipe(concat({encoding: 'object'}, function(res) { 11 | t.equal(1, arguments.length, 'concat returns 1 arg'); 12 | t.deepEqual(a, res, 'result array matches input'); 13 | t.end(); 14 | })); 15 | }); 16 | -------------------------------------------------------------------------------- /test/3.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | , streamify = require('..') 3 | ; 4 | 5 | test('ctor', function(t) { 6 | t.throws(function() { 7 | streamify(); 8 | }, 'throws: no argument'); 9 | 10 | [null, undefined, 1, NaN, 'string', new Object(), function(){}].forEach( 11 | function(item) { 12 | t.throws(function() { 13 | streamify(item); 14 | }, 'throws: ' + (!item? 'null/undefined' : item.toString())); 15 | } 16 | ); 17 | 18 | [[], [1], [1,2], ['1', '2'], [new Buffer('asdf')]].forEach( 19 | function(item) { 20 | t.doesNotThrow(function() { 21 | streamify(item); 22 | }, 'accepts: ' + item.toString()); 23 | } 24 | ); 25 | 26 | t.end(); 27 | }); 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: node_js 4 | 5 | notifications: 6 | email: false 7 | 8 | before_install: 9 | - npm install -g npm@2 10 | - npm install -g npm 11 | - '[ "${TRAVIS_NODE_VERSION}" == "0.8" -o "${TRAVIS_NODE_VERSION}" == "0.10" ] || npm install -g covert' 12 | 13 | matrix: 14 | fast_finish: true 15 | include: 16 | - node_js: '0.8' 17 | env: TASK=test 18 | - node_js: '0.10' 19 | env: TASK=test 20 | - node_js: '0.12' 21 | env: TASK=coverage 22 | - node_js: '1' 23 | env: TASK=coverage 24 | - node_js: '2' 25 | env: TASK=coverage 26 | - node_js: '3' 27 | env: TASK=coverage 28 | - node_js: '4' 29 | env: TASK=coverage 30 | - node_js: '5' 31 | env: TASK=coverage 32 | - node_js: '6' 33 | env: TASK=coverage 34 | 35 | script: "npm run $TASK" 36 | 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Matthew I. Metnetsky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /test/2.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | , streamify = require('..') 3 | , concat = require('concat-stream') 4 | ; 5 | 6 | test('empty array', function(t) { 7 | var s = streamify([]); 8 | 9 | s.pipe(concat({encoding: 'object'}, function(res) { 10 | t.equal(1, arguments.length, 'concat returns 1 arg'); 11 | t.equal(0, res.length, 'result is an empty list'); 12 | t.deepEqual([], res, 'result matches expectation'); 13 | t.end(); 14 | })); 15 | }); 16 | 17 | test('array of strings', function(t) { 18 | var s = streamify(['1', '2', '3', 'Four']); 19 | 20 | s.pipe(concat(function(res) { 21 | t.equal(1, arguments.length, 'concat returns 1 arg'); 22 | t.equal('123Four', res.toString(), 'result matches expectation'); 23 | t.end(); 24 | })); 25 | }); 26 | 27 | test('array of buffers', function(t) { 28 | var s = streamify([new Buffer('One'), new Buffer('Two')]); 29 | 30 | s.pipe(concat(function(res) { 31 | t.equal(1, arguments.length, 'concat returns 1 arg'); 32 | t.equal('OneTwo', res.toString(), 'result matches expectation'); 33 | t.end(); 34 | })); 35 | }); 36 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var Readable = require('readable-stream').Readable 4 | ; 5 | 6 | /** 7 | * Create a new instance of StreamArray 8 | * 9 | * @access private 10 | * @param {Array} list 11 | */ 12 | function StreamArray(list) { 13 | if (!Array.isArray(list)) 14 | throw new TypeError('First argument must be an Array'); 15 | 16 | Readable.call(this, {objectMode:true}); 17 | 18 | this._i = 0; 19 | this._l = list.length; 20 | this._list = list; 21 | } 22 | 23 | StreamArray.prototype = Object.create(Readable.prototype, {constructor: {value: StreamArray}}); 24 | 25 | /** 26 | * Read the next item from the source Array and push into NodeJS stream 27 | 28 | * @access protected 29 | * @desc Read the next item from the source Array and push into NodeJS stream 30 | * @param {number} size The amount of data to read (ignored) 31 | */ 32 | StreamArray.prototype._read = function(size) { 33 | this.push(this._i < this._l ? this._list[this._i++] : null); 34 | }; 35 | 36 | /** 37 | * Create a new instance of StreamArray 38 | * 39 | * @module stream-array 40 | * @desc Push Array elements through a NodeJS stream 41 | * @type {function} 42 | * @param {Array} list An Array of objects, strings, numbers, etc 43 | */ 44 | module.exports = function(list) { 45 | return new StreamArray(list); 46 | }; 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stream-array", 3 | "version": "1.1.2", 4 | "description": "Pipe an Array through Node.js streams", 5 | "dependencies": { 6 | "readable-stream": "~2.1.0" 7 | }, 8 | "devDependencies": { 9 | "concat-stream": "~1.5.0", 10 | "tape": "~4.5.0" 11 | }, 12 | "scripts": { 13 | "test": "./node_modules/.bin/tape ./test/*", 14 | "coverage": "covert ./test/*" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/mimetnet/node-stream-array" 19 | }, 20 | "bugs": { 21 | "url": "https://github.com/mimetnet/node-stream-array/issues" 22 | }, 23 | "homepage": "https://github.com/mimetnet/node-stream-array", 24 | "keywords": [ 25 | "array", 26 | "readable", 27 | "stream", 28 | "pipe" 29 | ], 30 | "author": "Matthew I. Metnetsky ", 31 | "license": "MIT", 32 | "main": "index.js", 33 | "directories": { 34 | "test": "test" 35 | }, 36 | "testling": { 37 | "files": "test/*.js", 38 | "browsers": [ 39 | "ie/8..latest", 40 | "firefox/3.5", 41 | "firefox/latest", 42 | "firefox/nightly", 43 | "chrome/10", 44 | "chrome/latest", 45 | "chrome/canary", 46 | "opera/12..latest", 47 | "opera/next", 48 | "safari/5.1..latest", 49 | "ipad/6.0..latest", 50 | "iphone/6.0..latest", 51 | "android-browser/4.2..latest" 52 | ] 53 | }, 54 | "engines": { 55 | "node" : ">= 0.8" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # stream-array 2 | 3 | Pipe an Array through Node.js [Streams][12]. This is rather useful for testing 4 | other streams. 5 | 6 | [![npm version][1]][2] 7 | [![build status][3]][4] 8 | [![dependencies][5]][6] 9 | [![devDependencies][7]][8] 10 | [![Inch CI][16]][17] 11 | 12 | [//]: [![testling][9]][10] 13 | 14 | 15 | ## Usage 16 | 17 | ```js 18 | var streamify = require('stream-array'), 19 | os = require('os'); 20 | 21 | streamify(['1', '2', '3', os.EOL]).pipe(process.stdout); 22 | ``` 23 | 24 | 25 | ## API 26 | 27 | #### streamify(Array) 28 | The result of [require][13] is a 'function()' that when invoked, will return a 29 | [Readable][11] [Stream][12]. 30 | 31 | ```js 32 | var streamify = require('stream-array'); 33 | ``` 34 | 35 | The source array can contain any type as it is assumed that the receiving 36 | stream can handle it. Each element in the array will be [pushed][14] into the 37 | [piped][15] stream, **without** modifying the source array. 38 | 39 | ```js 40 | var readable = streamify(['Hello', new Buffer('World')]); 41 | ``` 42 | 43 | This [Stream][12] will [push][14] each element of the source array into the 44 | [piped][15] array. 45 | 46 | ```js 47 | readable(['1', '2', '3', os.EOL]).pipe(process.stdout); 48 | ``` 49 | 50 | ``` 51 | 123\n 52 | ``` 53 | 54 | ## Install 55 | 56 | ```sh 57 | npm install stream-array 58 | ``` 59 | 60 | [1]: https://badge.fury.io/js/stream-array.svg 61 | [2]: https://badge.fury.io/js/stream-array 62 | [3]: https://api.travis-ci.org/mimetnet/node-stream-array.svg 63 | [4]: https://travis-ci.org/mimetnet/node-stream-array 64 | [5]: https://david-dm.org/mimetnet/node-stream-array.svg 65 | [6]: https://david-dm.org/mimetnet/node-stream-array 66 | [7]: https://david-dm.org/mimetnet/node-stream-array/dev-status.svg?#info=devDependencies 67 | [8]: https://david-dm.org/mimetnet/node-stream-array/#info=devDependencies 68 | [//]: https://ci.testling.com/mimetnet/node-stream-array.png 69 | [//]: https://ci.testling.com/mimetnet/node-stream-array 70 | [11]: http://nodejs.org/api/stream.html#stream_class_stream_readable 71 | [12]: http://nodejs.org/api/stream.html#stream_stream 72 | [13]: http://nodejs.org/api/globals.html#globals_require 73 | [14]: https://nodejs.org/api/stream.html#stream_readable_push_chunk_encoding 74 | [15]: https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options 75 | [16]: https://inch-ci.org/github/mimetnet/node-stream-array.svg?branch=master 76 | [17]: http://inch-ci.org/github/mimetnet/node-stream-array 77 | 78 | ## License 79 | 80 | [MIT License](https://github.com/mimetnet/node-stream-array/blob/master/LICENSE) 81 | --------------------------------------------------------------------------------