git ssb

3+

cel / ssb-npm-registry



Tree: d2f2697f296dd39aed6a8b63c6d04a736a7db5b3

Files: d2f2697f296dd39aed6a8b63c6d04a736a7db5b3 / node_modules / pull-stream / test / pull.js

2210 bytesRaw
1var tape = require('tape')
2
3function curry (fun) {
4 return function () {
5 var args = [].slice.call(arguments)
6 return function (read) {
7 return fun.apply(null, [read].concat(args))
8 }
9 }
10}
11
12var pull = require('../')
13
14function values (array) {
15 var i = 0
16 return function (abort, cb) {
17 if(abort) i = array.length, cb(abort)
18 else if(i >= array.length) cb(true)
19 else cb(null, array[i++])
20 }
21}
22
23var map = curry(function (read, mapper) {
24 return function (abort, cb) {
25 read(abort, function (end, data) {
26 if(end) cb(end)
27 else cb(null, mapper(data))
28 })
29 }
30 })
31
32var sum = curry(function (read, done) {
33 var total = 0
34 read(null, function next (end, data) {
35 if(end) return done(end === true ? null : end, total)
36 total += data
37 read(null, next)
38 })
39 })
40
41var log = curry(function (read) {
42 return function (abort, cb) {
43 read(abort, function (end, data) {
44 if(end) return cb(end)
45 console.error(data)
46 cb(null, data)
47 })
48 }
49 })
50
51tape('wrap pull streams into stream', function (t) {
52
53 pull(
54 values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
55 map(function (e) { return e*e }),
56 log(),
57 sum(function (err, value) {
58 console.log(value)
59 t.equal(value, 385)
60 t.end()
61 })
62 )
63
64})
65
66tape('turn pull(through,...) -> Through', function (t) {
67
68 pull(
69 values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
70 pull(
71 map(function (e) { return e*e }),
72 log()
73 ),
74 sum(function (err, value) {
75 console.log(value)
76 t.equal(value, 385)
77 t.end()
78 })
79 )
80
81})
82
83// pull(
84// values ([1 2 3 4 5 6 7 8 9 10])
85// pull(
86// map({x y;: e*e })
87// log()
88// )
89// sum({
90// err value:
91// t.equal(value 385)
92// t.end()
93// })
94// )
95//
96
97tape("writable pull() should throw when called twice", function (t) {
98 t.plan(2)
99
100 var stream = pull(
101 map(function (e) { return e*e }),
102 sum(function (err, value) {
103 console.log(value)
104 t.equal(value, 385)
105 })
106 )
107
108 stream(values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
109
110 t.throws(function () {
111 stream(values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
112 }, TypeError)
113})
114

Built with git-ssb-web