pax_global_header00006660000000000000000000000064130076021270014510gustar00rootroot0000000000000052 comment=8a462e3342c97a92a82f3da4f09533acc7605a7c stream-iterate-1.2.0/000077500000000000000000000000001300760212700144365ustar00rootroot00000000000000stream-iterate-1.2.0/.gitignore000066400000000000000000000000151300760212700164220ustar00rootroot00000000000000node_modules stream-iterate-1.2.0/.travis.yml000066400000000000000000000000741300760212700165500ustar00rootroot00000000000000language: node_js node_js: - "0.10" - '0.12' - 'iojs' stream-iterate-1.2.0/LICENSE000066400000000000000000000020671300760212700154500ustar00rootroot00000000000000The 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. stream-iterate-1.2.0/README.md000066400000000000000000000015021300760212700157130ustar00rootroot00000000000000# stream-iterate Iterate through the values in a stream. ``` npm install stream-iterate ``` [![build status](http://img.shields.io/travis/mafintosh/stream-iterate.svg?style=flat)](http://travis-ci.org/mafintosh/stream-iterate) ## Usage ``` js var iterate = require('stream-iterate') var from = require('from2') var stream = from.obj(['a', 'b', 'c']) var read = iterate(stream) loop() // recursively iterates through each item in the stream function loop () { read(function (err, data, next) { console.log(err, data) next() loop() }) } ``` If you don't call `next` and call `read` again the same `(err, value)` pair will be returned. You can use this module to implement stuff like [a streaming merge sort](https://github.com/mafintosh/stream-iterate/blob/master/test.js#L5-L47). ## License [MIT](LICENSE) stream-iterate-1.2.0/index.js000066400000000000000000000025211300760212700161030ustar00rootroot00000000000000var Readable = require('readable-stream').Readable var shift = require('stream-shift') var stream2 = function (stream) { if (stream._readableState) return stream return new Readable({objectMode: true, highWaterMark: 16}).wrap(stream) } module.exports = function (stream) { stream = stream2(stream) var ended = false var data = null var err = null var destroyed = false var fn = null var consume = function (e) { if (e) { destroyed = true if (stream.destroy) stream.destroy(e) return } data = null err = null } var onresult = function () { if (!fn) return var tmp = fn fn = undefined tmp(err, data, consume) } var update = function () { if (!fn) return data = shift(stream) if (data === null && !ended) return onresult() } var onend = function () { ended = true onresult() } stream.on('readable', update) stream.on('error', function (e) { err = e onresult() }) stream.on('close', function () { if (stream._readableState.ended) return onend() }) stream.on('end', onend) return function (callback) { if (destroyed) return if (err) return callback(err, null, consume) if (data) return callback(null, data, consume) if (ended) return callback(null, null, consume) fn = callback update() } } stream-iterate-1.2.0/package.json000066400000000000000000000012541300760212700167260ustar00rootroot00000000000000{ "name": "stream-iterate", "version": "1.2.0", "description": "Iterate through the values of a stream", "main": "index.js", "dependencies": { "readable-stream": "^2.1.5", "stream-shift": "^1.0.0" }, "devDependencies": { "from2": "^1.3.0", "standard": "^3.3.2", "tape": "^4.0.0" }, "repository": { "type": "git", "url": "https://github.com/mafintosh/stream-iterate.git" }, "scripts": { "test": "standard && tape test.js" }, "author": "Mathias Buus (@mafintosh)", "license": "MIT", "bugs": { "url": "https://github.com/mafintosh/stream-iterate/issues" }, "homepage": "https://github.com/mafintosh/stream-iterate" } stream-iterate-1.2.0/test.js000066400000000000000000000023171300760212700157560ustar00rootroot00000000000000var tape = require('tape') var from = require('from2') var iterate = require('./') tape('merge sort', function (t) { var a = from.obj(['a', 'b', 'd', 'e', 'g', 'h']) var b = from.obj(['b', 'c', 'f']) var output = [] var readA = iterate(a) var readB = iterate(b) var loop = function () { readA(function (err, dataA, nextA) { if (err) throw err readB(function (err, dataB, nextB) { if (err) throw err if (!dataA && !dataB) { t.same(output, ['a', 'b', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], 'sorts list') t.end() return } if (!dataB || dataA < dataB) { output.push(dataA) nextA() return loop() } if (!dataA || dataA > dataB) { output.push(dataB) nextB() return loop() } output.push(dataA) output.push(dataB) nextA() nextB() loop() }) }) } loop() }) tape('error handling', function (t) { var a = from.obj(['a', 'b', 'd', 'e', 'g', 'h']) var read = iterate(a) a.destroy(new Error('oh no')) read(function (err) { t.ok(err, 'had error') t.same(err.message, 'oh no') t.end() }) })