Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
schnittstabil committed Jul 11, 2014
0 parents commit 2454d70
Show file tree
Hide file tree
Showing 11 changed files with 535 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# editorconfig.org
root = true

[*]
charset=utf-8
end_of_line=lf
indent_size=2
indent_style=space
insert_final_newline=true
tab_width=2
trim_trailing_whitespace = true
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

.coveralls.yml
stream-from-value-*.*.*.tgz
12 changes: 12 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"preset": "google",

"requireParenthesesAroundIIFE": true,
"maximumLineLength": 80,
"validateLineBreaks": "LF",
"validateIndentation": 2,

"disallowKeywords": ["with"],
"disallowSpacesInsideObjectBrackets": null,
"disallowImplicitTypeConversion": ["string"]
}
90 changes: 90 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"globals": {
/* mocha */
"after": true,
"afterEach": true,
"before": true,
"beforeEach": true,
"describe": true,
"it": true
},

/* Enforcing options */
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"es3": true,
"forin": true,
"freeze": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"noempty": true,
"nonbsp": true,
"nonew": true,
"plusplus": false,
"quotmark": "single",
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"maxparams": 8,
"maxdepth": 3,
"maxstatements": 20,
"maxcomplexity": 10,
"maxlen": 80,

/* Relaxing options */
"asi": false,
"boss": false,
"debug": false,
"eqnull": false,
"esnext": true,
"evil": false,
"expr": false,
"funcscope": false,
"gcl": false,
"globalstrict": false,
"iterator": false,
"lastsemic": false,
"laxbreak": false,
"laxcomma": false,
"loopfunc": false,
"maxerr": 100,
"moz": false,
"multistr": false,
"notypeof": false,
"proto": false,
"scripturl": false,
"smarttabs": false,
"shadow": false,
"sub": false,
"supernew": false,
"validthis": false,
"noyield": false,

/* Environments */
"browser": false,
"couch": false,
"devel": false,
"dojo": false,
"jquery": false,
"mootools": false,
"node": true,
"nonstandard": false,
"phantom": false,
"prototypejs": false,
"rhino": false,
"worker": false,
"wsh": false,
"yui": false,

/* Legacy */
"nomen": false,
"onevar": true,
"passfail": false,
"white": false
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "0.11"
- "0.10"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Michael Mayer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# stream-from-array [![Dependencies Status Image](https://gemnasium.com/schnittstabil/stream-from-array.svg)](https://gemnasium.com/schnittstabil/stream-from-array) [![Build Status Image](https://travis-ci.org/schnittstabil/stream-from-array.svg)](https://travis-ci.org/schnittstabil/stream-from-array) [![Coverage Status](https://coveralls.io/repos/schnittstabil/stream-from-array/badge.png)](https://coveralls.io/r/schnittstabil/stream-from-array)

Create streams from arrays of arbitrary Javascript values like strings, functions, arrays, etc.

```bash
npm install stream-from-array --save
```

## Usage

### Stream of array of `String | Buffer`s

```JavaScript
var StreamFromArray = require('stream-from-array');

StreamFromArray(['some', ' ', 'strings'])
.pipe(process.stdout); // output: some strings

StreamFromArray([new Buffer('some') , ' mixed ', new Buffer('strings')])
.pipe(process.stdout); // output: some mixed strings
```

### Stream of (arbitrary) Javascript Values

```JavaScript
var StreamFromArray = require('stream-from-array');

var i = 0;
StreamFromArray.obj(['some', 42, 'mixed', 'array', function(){}])
.on('data', function(data){
console.log(i++ + ': ' + typeof data);
/* outputs:
0: string
1: number
2: string
3: string
4: function
*/
});
```

### Stream of [Gulp](http://gulpjs.com/) Files

Gulp files are [vinyl](https://github.com/wearefractal/vinyl) files:

```bash
npm install vinyl
```

Test some awsome Gulp plugin:

```JavaScript
var StreamFromArray = require('stream-from-array'),
File = require('vinyl');

var hello = new File({
cwd: '/',
base: '/hello/',
path: '/hello/hello.js',
contents: new Buffer('console.log("Hello");')
});

var world = new File({
cwd: '/',
base: '/hello/',
path: '/hello/world.js',
contents: new Buffer('console.log("world!");')
});

StreamFromArray.obj([hello, world])
.pipe(someAwsomeGulpPlugin())
.on('data', function(file){
console.log(file.contents.toString()); // dunno what someAwsomeGulpPlugin does :)
});
```

See also [stream-recorder](https://github.com/schnittstabil/stream-recorder) for testing gulp plugins with stream-from-array.

## API

### Class: StreamFromArray

StreamFromArrays are [Readable](http://nodejs.org/api/stream.html#stream_class_stream_readable_1) streams.

#### new StreamFromArray(array, [options])

* _array_ `Array` Array of arbitrary Javascript values like numbers, strings, objects, functions, ...
* _options_ `Object` passed through [new Readable([options])](http://nodejs.org/api/stream.html#stream_new_stream_readable_options)

Note: The `new` operator can be omitted.

#### StreamFromArray#obj(array, [options])

A convenience wrapper for `new StreamFromArray(array, {objectMode: true, ...})`.

## License

Copyright (c) 2014 Michael Mayer

Licensed under the MIT license.
63 changes: 63 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';
var gulp = require('gulp'),
istanbul = require('gulp-istanbul'),
jscs = require('gulp-jscs'),
jshint = require('gulp-jshint'),
mocha = require('gulp-mocha'),
scripts = ['**/*.js', '!node_modules/**', '!coverage/**'],
tests = 'test.js';

function lint(fail) {
return function() {
var l = gulp.src(scripts)
/* hint */
.pipe(jshint())
.pipe(jshint.reporter())
/* jscs */
.pipe(jscs());

if (fail) {
return l.pipe(jshint.reporter('fail'));
} else {
return l;
}
};
}

gulp.task('lint', lint(true));
gulp.task('lint-watch', lint(false));

gulp.task('watch:lint', function() {
gulp.watch(scripts, ['lint-watch']);
});

gulp.task('watch:test', function() {
gulp.watch(scripts, ['test']);
});

gulp.task('watch', function() {
gulp.watch(scripts, ['lint-watch', 'test']);
});

gulp.task('test', function() {
return gulp.src(tests)
.pipe(mocha({reporter: 'spec'}));
});

gulp.task('coverage', function (done) {
gulp.src(scripts.concat(['!' + tests]))
.pipe(istanbul())
.on('finish', function () {
/* tests */
gulp.src(tests)
.pipe(mocha({
reporter: 'dot'
}))
.pipe(istanbul.writeReports({
reporters: ['lcovonly', 'text-summary', 'html']
}))
.on('end', done);
});
});

gulp.task('default', ['lint', 'coverage']);
39 changes: 39 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
var Readable = require('stream').Readable,
inherits = require('util').inherits;

function StreamFromArray(array, options) {
if (!(this instanceof StreamFromArray)) {
return new StreamFromArray(array, options);
}

Readable.call(this, options);

this.__array = array;
this.__index = 0;
}
inherits(StreamFromArray, Readable);

StreamFromArray.obj = function(array, options) {
options = options || {};
options.objectMode = true;
return new StreamFromArray(array, options);
};

StreamFromArray.prototype._read = function() {
var needMoreData,
value;
while (this.__index < this.__array.length && needMoreData !== false) {
value = this.__array[this.__index++];
needMoreData = this.push(value);
if (typeof value === 'undefined' || value === null) {
// value signaled end of data
this.__index = this.__array.length;
} else if (this.__index === this.__array.length) {
// end of data
this.push(null);
}
}
};

module.exports = StreamFromArray;

0 comments on commit 2454d70

Please sign in to comment.