pull-stream
Advanced tools
Comparing version 2.27.0 to 2.28.1
@@ -70,6 +70,10 @@ # Throughs | ||
## take (test) | ||
## take (test [, opts]) | ||
Read from the source stream until `test` fails. | ||
If test is a function, read data from the source stream and forward it downstream until test(data) returns false. | ||
If `opts.last` is set to true, the data for which the test failed will be included in what is forwarded. | ||
If test is an integer, take n item from the source. | ||
## group (length) | ||
@@ -76,0 +80,0 @@ |
{ | ||
"name": "pull-stream", | ||
"description": "minimal pull stream", | ||
"version": "2.27.0", | ||
"version": "2.28.1", | ||
"homepage": "https://github.com/dominictarr/pull-stream", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -70,3 +70,3 @@ # pull-stream | ||
pull(createSourceStream(), createThroughStream()), createSinkStream()) | ||
pull(createSourceStream(), createThroughStream(), createSinkStream()) | ||
``` | ||
@@ -91,3 +91,3 @@ | ||
The readable stream eventually `callback(err)` if there was an error, or `callback(true)` | ||
if the stream has no more data. | ||
if the stream has no more data. In both cases a second argument will be irgnored. That is, the readable stream either provides data _or_ indicates an error or end-of-data condition, never both. | ||
@@ -94,0 +94,0 @@ if the user passes in `end = true`, then stop getting data from wherever. |
@@ -19,2 +19,22 @@ var pull = require('../') | ||
test('flatten - number of reads', function (t) { | ||
var reads = 0 | ||
pull( | ||
pull.values([ | ||
pull.values([1, 2, 3]), | ||
]), | ||
pull.flatten(), | ||
pull.through(function() { | ||
reads++ | ||
console.log('READ', reads) | ||
}), | ||
pull.take(2), | ||
pull.collect(function (err, numbers) { | ||
t.deepEqual([1, 2], numbers) | ||
t.equal(reads, 2) | ||
t.end() | ||
}) | ||
) | ||
}) | ||
test('flatten stream of streams', function (t) { | ||
@@ -21,0 +41,0 @@ |
@@ -42,6 +42,16 @@ var pull = require('../') | ||
test('take 5', function (t) { | ||
test('take - exclude last (default)', function (t) { | ||
pull( | ||
pull.values([1,2,3,4,5,6,7,8,9,10]), | ||
pull.take(5), | ||
pull.take(function(n) {return n<5}), | ||
pull.collect(function (err, four) { | ||
t.deepEqual(four, [1,2,3,4]) | ||
t.end() | ||
}) | ||
) | ||
}) | ||
test('take - include last', function (t) { | ||
pull( | ||
pull.values([1,2,3,4,5,6,7,8,9,10]), | ||
pull.take(function(n) {return n<5}, {last: true}), | ||
pull.collect(function (err, five) { | ||
@@ -53,1 +63,23 @@ t.deepEqual(five, [1,2,3,4,5]) | ||
}) | ||
test('take 5 causes 5 reads upstream', function (t) { | ||
var reads = 0 | ||
pull( | ||
pull.values([1,2,3,4,5,6,7,8,9,10]), | ||
pull.Through(function (read) { | ||
return function (end, cb) { | ||
if (end !== true) reads++ | ||
console.log(reads, end) | ||
read(end, cb) | ||
} | ||
})(), | ||
pull.take(5), | ||
pull.collect(function (err, five) { | ||
t.deepEqual(five, [1,2,3,4,5]) | ||
process.nextTick(function() { | ||
t.equal(reads, 5) | ||
t.end() | ||
}) | ||
}) | ||
) | ||
}) |
@@ -132,7 +132,10 @@ var u = require('pull-core') | ||
var take = exports.take = | ||
function (read, test) { | ||
function (read, test, opts) { | ||
opts = opts || {} | ||
var last = opts.last || false // whether the first item for which !test(item) should still pass | ||
var ended = false | ||
if('number' === typeof test) { | ||
last = true | ||
var n = test; test = function () { | ||
return n -- | ||
return --n | ||
} | ||
@@ -149,4 +152,4 @@ } | ||
ended = true | ||
read(true, function (end, data) { | ||
cb(ended, data) | ||
read(true, function () { | ||
last ? cb(end, data) : cb(true) | ||
}) | ||
@@ -207,2 +210,3 @@ } | ||
return function (abort, cb) { | ||
if (abort) return read(abort, cb) | ||
if(_read) nextChunk() | ||
@@ -209,0 +213,0 @@ else nextStream() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52111
36
1516