-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixing 'end' issue and adding examples and tests
- Loading branch information
Showing
6 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
})) | ||
}) |