Skip to content

Commit

Permalink
fixing 'end' issue and adding examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Aug 17, 2013
1 parent c7c19b6 commit 4fc7136
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 4 deletions.
66 changes: 66 additions & 0 deletions examples/complex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

// demonstrates how to split up generated values into three times as many via async-through
var nestStream = require('../')
, asyncThru = require('async-through')
, from = require('from')
, sep = '';

var stream = nestStream(getInnerStream, sep);

function getInnerStream (outerVal) {
return from([1 + outerVal, 3 + outerVal, 4 + outerVal, 5 + outerVal])
}

function transform (data) {
// ignore queued separators, but queue non-null value to keep stream going
if (data === sep) return this.queue(sep);

setTimeout(function () {
this.queue('\nn : ' + data, true);
this.queue('\nn * 2: ' + data * 2, true);
this.queue('\nn / 2: ' + data / 2 + '\n');
}.bind(this), 100);
}


from([10, 20, 30, 40])
.pipe(stream)
.pipe(asyncThru(transform))
.on('end', function () { console.error('ended') })
.pipe(process.stdout);

/* =>
* n : 11
* n * 2: 22
* n / 2: 5.5
*
* n : 13
* n * 2: 26
* n / 2: 6.5
*
* n : 14
* n * 2: 28
* n / 2: 7
*
* [ ... ]
*
* n : 44
* n * 2: 88
* n / 2: 22
*
* n : 45
* n * 2: 90
* n / 2: 22.5
* ended
*/










23 changes: 23 additions & 0 deletions examples/custom-sep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

var nestStream = require('../')
, through = require('through')
, from = require('from');

var stream = nestStream(getInnerStream, '::');

function getInnerStream (outerVal) {
return from([1 + outerVal, 3 + outerVal, 4 + outerVal, 5 + outerVal])
}

function stringify (data) {
this.queue(data + ' ');
}

from([10, 20, 30, 40])
.pipe(stream)
.pipe(through(stringify))
.on('end', function () { console.error('ended') })
.pipe(process.stdout);

// => 11 13 14 15 :: 21 23 24 25 :: 31 33 34 35 :: 41 43 44 45 :: ended
23 changes: 23 additions & 0 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

var nestStream = require('../')
, through = require('through')
, from = require('from');

var stream = nestStream(getInnerStream);

function getInnerStream (outerVal) {
return from([1 + outerVal, 3 + outerVal, 4 + outerVal, 5 + outerVal])
}

function stringify (data) {
this.queue(data + ' ');
}

from([10, 20, 30, 40])
.pipe(stream)
.pipe(through(stringify))
.on('end', function () { console.error('ended') })
.pipe(process.stdout);

// => 11 13 14 15 21 23 24 25 31 33 34 35 41 43 44 45 ended
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ var asyncThru = require('async-through');
* @name exports
* @function
* @param getInnerStream {Function} function (outerValue) { .. } that returns a stream generated from the provided value
* @param sep {Object} (optional)
* any non-null object to serve as a separator between values queued for one value piped by the outer stream
* default: ''
* @return {Stream} that emits a value for each value provided by either of the inner streams
*/
var go = module.exports = function (getInnerStream) {
var go = module.exports = function (getInnerStream, sep) {
var outerStream = asyncThru(onouter);
sep = sep || '';

var ondata = function (data) { outerStream.queue(data, true); };
var onend = function () { outerStream.queue(null); }
var onend = function () { outerStream.queue(sep); }

function onouter (data) {
getInnerStream(data)
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
"url": "git://github.com/thlorenz/nest-stream.git"
},
"homepage": "https://github.com/thlorenz/nest-stream",
"dependencies": {},
"dependencies": {
"async-through": "~0.2.0"
},
"devDependencies": {
"nave": "~0.4.3",
"tape": "~1.0.4",
"tap": "~0.4.3"
"tap": "~0.4.3",
"from": "~0.1.3",
"through": "~2.3.4",
"concat-stream": "~1.0.1"
},
"keywords": [],
"author": {
Expand Down
47 changes: 47 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

'use strict';
/*jshint asi: true */

var test = require('tape')

var nestStream = require('../')
, concatStream = require('concat-stream')
, from = require('from');


test('\ndefault separator', function (t) {

var stream = nestStream(getInnerStream);
function getInnerStream (outerVal) {
return from([1 + outerVal, 3 + outerVal, 4 + outerVal, 5 + outerVal])
}

var ended;
from([10, 20, 30, 40])
.pipe(stream)
.on('end', function () { ended = true })
.pipe(concatStream(function (data) {
t.ok(ended, 'ended')
t.deepEqual(data, [ 11, 13, 14, 15, '', 21, 23, 24, 25, '', 31, 33, 34, 35, '', 41, 43, 44, 45, '' ], 'data')
t.end()
}))
})

test('\ncustom separator', function (t) {

var sep = 'o/';
var stream = nestStream(getInnerStream, sep);
function getInnerStream (outerVal) {
return from([1 + outerVal, 3 + outerVal, 4 + outerVal, 5 + outerVal])
}

var ended;
from([10, 20, 30 ])
.pipe(stream)
.on('end', function () { ended = true })
.pipe(concatStream(function (data) {
t.ok(ended, 'ended')
t.deepEqual(data, [ 11, 13, 14, 15, 'o/', 21, 23, 24, 25, 'o/', 31, 33, 34, 35, 'o/' ], 'data')
t.end()
}))
})

0 comments on commit 4fc7136

Please sign in to comment.