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 9223784
Show file tree
Hide file tree
Showing 11 changed files with 696 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-factory-*.*.*.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.
169 changes: 169 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# stream-from-factory [![Dependencies Status Image](https://gemnasium.com/schnittstabil/stream-from-factory.svg)](https://gemnasium.com/schnittstabil/stream-from-factory) [![Build Status Image](https://travis-ci.org/schnittstabil/stream-from-factory.svg)](https://travis-ci.org/schnittstabil/stream-from-factory) [![Coverage Status](https://coveralls.io/repos/schnittstabil/stream-from-factory/badge.png)](https://coveralls.io/r/schnittstabil/stream-from-factory)

Create streams from sync and async factories.

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

## About Factories

A factory is simply a function creating a product (i.e. a JavaScript value, like numbers, objects, etc.) and sends it back to its caller.

(The term _factory_ refers to the idea of Joshua Blochs _static factory methods_, not to _abstract factories_, nor to _factory methods_.)

### Synchronous Factories

_Synchronous Factories_ are functions `return`ing a single product - other than `undefined` - or throwing an arbitrary JavaScript value:

```JavaScript
function syncFactory() {
if (...) {
throw new Error('sth. went wrong...'); // typically Errors are thrown
}
return null; // that's ok (typeof null !== 'undefined'), but see below (API)
}
```
### Asynchronous Factories

_Asynchronous Factories_ working with callbacks (in node style), but don't return a value;

```JavaScript
function asyncFactory(done) {
if (...) {
// return an error:
done(new Error('sth. went wrong...'));
return; // that's ok (!)
}
// return a result:
done(null, ['a', 'string', 'array']);
}
```

## Usage

### Factories creating `String | Buffer`s

```JavaScript
var StreamFromFactory = require('stream-from-factory');

function syncFactory() {
return new Buffer('buff!');
}

function asyncFactory(done) {
setTimeout(function() {
done(null, 'strrrring!');
}, 1000);
}


StreamFromFactory(syncFactory)
.pipe(process.stdout); // output: buff!

StreamFromFactory(asyncFactory)
.pipe(process.stdout); // output: strrrring!
```

### Factories creating arbitrary JavaScript values

```JavaScript
var StreamFromFactory = require('stream-from-factory');

function logFunc(){
console.log('func!?!');
};

function asyncFactory(done) {
setTimeout(function() {
done(null, logFunc);
}, 1000);
}

StreamFromFactory.obj(asyncFactory)
.on('data', function(fn){
fn(); // output: func!?!
});
```

### Errors

```JavaScript
var StreamFromFactory = require('stream-from-factory');

function syncFactory() {
throw new Error('sth. went wrong ;-)');
}

function asyncFactory(done) {
setTimeout(function() {
done(new Error('sth. went wrong ;-)'));
}, 1000);
}

StreamFromFactory(syncFactory)
.on('error', function(err){
console.log(err); // output: [Error: sth. went wrong ;-)]
})
.on('data', function(data){
// do something awsome
});
```

### [Gulp](http://gulpjs.com/) File Factories

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

```bash
npm install vinyl
```

Test some awsome Gulp plugin:

```JavaScript
var StreamFromFactory = require('stream-from-factory'),
File = require('vinyl');

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

StreamFromFactory.obj(creatTestFile)
.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.

## API

### Class: StreamFromFactory

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

#### new StreamFromFactory(factory, [options])

* _factory_ `Function` Async or sync factory.
* _options_ `Object` passed through [new Readable([options])](http://nodejs.org/api/stream.html#stream_new_stream_readable_options)

Notes:

* The `new` operator can be omitted.
* `null` is special for streams (signals end-of-stream). Using a factory returning `null` will result in an empty stream.

#### StreamFromFactory#obj(factory, [options])

A convenience wrapper for `new StreamFromFactory(factory, {objectMode: true, ...})`.

## License

Copyright (c) 2014 Michael Mayer

Licensed under the MIT license.
Loading

0 comments on commit 9223784

Please sign in to comment.