-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b3736ab
Showing
6 changed files
with
151 additions
and
0 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 @@ | ||
node_modules |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Mathias Buus | ||
|
||
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. |
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,31 @@ | ||
# stream-each | ||
|
||
Iterate all the data in a stream | ||
|
||
``` | ||
npm install stream-each | ||
``` | ||
|
||
## Usage | ||
|
||
``` js | ||
var each = require('stream-each') | ||
|
||
each(stream, function (data, next) { | ||
console.log('data from stream', data) | ||
// when ready to consume next chunk | ||
next() | ||
}) | ||
``` | ||
|
||
## API | ||
|
||
#### `each(stream, iterator)` | ||
|
||
Iterate the data in the stream by calling the iterator function with `(data, next)` | ||
where data is a data chunk and next is a callback. Call next when you are ready to | ||
consume the next chunk. Optionally you can call next with an error to destroy the stream | ||
|
||
## License | ||
|
||
MIT |
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 @@ | ||
module.exports = each | ||
|
||
function each (stream, fn) { | ||
var want = false | ||
|
||
stream.on('readable', function () { | ||
if (want) loop(null) | ||
}) | ||
loop(null) | ||
|
||
return stream | ||
|
||
function loop (err) { | ||
if (err) return stream.destroy(err) | ||
var data = stream.read() | ||
if (!data) { | ||
want = true | ||
} else { | ||
want = false | ||
fn(data, loop) | ||
} | ||
} | ||
} |
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,21 @@ | ||
{ | ||
"name": "stream-each", | ||
"version": "0.0.0", | ||
"description": "Iterate all the data in a stream", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tape": "^4.2.1", | ||
"through2": "^2.0.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mafintosh/stream-each.git" | ||
}, | ||
"author": "Mathias Buus (@mafintosh)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mafintosh/stream-each/issues" | ||
}, | ||
"homepage": "https://github.com/mafintosh/stream-each" | ||
} |
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,54 @@ | ||
var tape = require('tape') | ||
var through = require('through2') | ||
var each = require('./') | ||
|
||
tape('each', function (t) { | ||
var s = through.obj() | ||
s.write('a') | ||
s.write('b') | ||
s.write('c') | ||
s.end() | ||
|
||
s.on('end', function () { | ||
t.end() | ||
}) | ||
|
||
var expected = ['a', 'b', 'c'] | ||
each(s, function (data, next) { | ||
t.same(data, expected.shift()) | ||
next() | ||
}) | ||
}) | ||
|
||
tape('each (write after)', function (t) { | ||
var s = through.obj() | ||
s.on('end', function () { | ||
t.end() | ||
}) | ||
|
||
var expected = ['a', 'b', 'c'] | ||
each(s, function (data, next) { | ||
t.same(data, expected.shift()) | ||
next() | ||
}) | ||
|
||
setTimeout(function () { | ||
s.write('a') | ||
s.write('b') | ||
s.write('c') | ||
s.end() | ||
}, 100) | ||
}) | ||
|
||
tape('each error', function (t) { | ||
var s = through.obj() | ||
s.write('hello') | ||
s.on('error', function (err) { | ||
t.same(err.message, 'stop') | ||
t.end() | ||
}) | ||
|
||
each(s, function (data, next) { | ||
next(new Error('stop')) | ||
}) | ||
}) |