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