Files: 611dd81d35553e9f46997b8293c9234dedbf14ff / test / flatten.js
2973 bytesRaw
1 | var pull = require('../') |
2 | var test = require('tape') |
3 | |
4 | test('flatten arrays', function (t) { |
5 | pull( |
6 | pull.values([ |
7 | [1, 2, 3], |
8 | [4, 5, 6], |
9 | [7, 8, 9] |
10 | ]), |
11 | pull.flatten(), |
12 | pull.collect(function (err, numbers) { |
13 | t.deepEqual([1, 2, 3, 4, 5, 6, 7, 8, 9], numbers) |
14 | t.end() |
15 | }) |
16 | ) |
17 | }) |
18 | |
19 | test('flatten - number of reads', function (t) { |
20 | var reads = 0 |
21 | pull( |
22 | pull.values([ |
23 | pull.values([1, 2, 3]), |
24 | ]), |
25 | pull.flatten(), |
26 | pull.through(function() { |
27 | reads++ |
28 | console.log('READ', reads) |
29 | }), |
30 | pull.take(2), |
31 | pull.collect(function (err, numbers) { |
32 | t.deepEqual([1, 2], numbers) |
33 | t.equal(reads, 2) |
34 | t.end() |
35 | }) |
36 | ) |
37 | |
38 | }) |
39 | test('flatten stream of streams', function (t) { |
40 | |
41 | pull( |
42 | pull.values([ |
43 | pull.values([1, 2, 3]), |
44 | pull.values([4, 5, 6]), |
45 | pull.values([7, 8, 9]) |
46 | ]), |
47 | pull.flatten(), |
48 | pull.collect(function (err, numbers) { |
49 | t.deepEqual([1, 2, 3, 4, 5, 6, 7, 8, 9], numbers) |
50 | t.end() |
51 | }) |
52 | ) |
53 | |
54 | }) |
55 | |
56 | test('flatten stream of broken streams', function (t) { |
57 | var _err = new Error('I am broken'), sosEnded |
58 | pull( |
59 | pull.values([ |
60 | pull.error(_err) |
61 | ], function(err) { |
62 | sosEnded = err; |
63 | }), |
64 | pull.flatten(), |
65 | pull.onEnd(function (err) { |
66 | t.equal(err, _err) |
67 | process.nextTick(function() { |
68 | t.equal(sosEnded, null, 'should abort stream of streams') |
69 | t.end() |
70 | }) |
71 | }) |
72 | ) |
73 | }) |
74 | |
75 | test('abort flatten', function (t) { |
76 | var sosEnded, s1Ended, s2Ended |
77 | var read = pull( |
78 | pull.values([ |
79 | pull.values([1,2], function(err) {s1Ended = err}), |
80 | pull.values([3,4], function(err) {s2Ended = err}), |
81 | ], function(err) { |
82 | sosEnded = err; |
83 | }), |
84 | pull.flatten() |
85 | ) |
86 | |
87 | read(null, function(err, data) { |
88 | t.notOk(err) |
89 | t.equal(data,1) |
90 | read(true, function(err, data) { |
91 | t.equal(err, true) |
92 | process.nextTick(function() { |
93 | t.equal(sosEnded, null, 'should abort stream of streams') |
94 | t.equal(s1Ended, null, 'should abort current nested stream') |
95 | t.equal(s2Ended, undefined, 'should not abort queued nested stream') |
96 | t.end() |
97 | }) |
98 | }) |
99 | }) |
100 | }) |
101 | |
102 | test('abort flatten before 1st read', function (t) { |
103 | var sosEnded, s1Ended |
104 | var read = pull( |
105 | pull.values([ |
106 | pull.values([1,2], function(err) {s1Ended = err}) |
107 | ], function(err) { |
108 | sosEnded = err; |
109 | }), |
110 | pull.flatten() |
111 | ) |
112 | |
113 | read(true, function(err, data) { |
114 | t.equal(err, true) |
115 | t.notOk(data) |
116 | process.nextTick(function() { |
117 | t.equal(sosEnded, null, 'should abort stream of streams') |
118 | t.equal(s1Ended, undefined, 'should abort current nested stream') |
119 | t.end() |
120 | }) |
121 | }) |
122 | }) |
123 | |
124 | test('flattern handles stream with normal objects', function (t) { |
125 | pull( |
126 | pull.values([ |
127 | [1,2,3], 4, [5,6,7], 8, 9 ,10 |
128 | ]), |
129 | pull.flatten(), |
130 | pull.collect(function (err, ary) { |
131 | t.deepEqual(ary, [1,2,3,4,5,6,7,8,9,10]) |
132 | t.end() |
133 | }) |
134 | ) |
135 | }) |
136 |
Built with git-ssb-web