UNPKG

1.64 kBJavaScriptView Raw
1
2var ps = require('../');
3var B = require('bluebird');
4var fs = require('fs');
5var split = require('split');
6var path = require('path');
7
8var t = require('blue-tape');
9
10
11function lines() {
12 return raw().pipe(split())
13}
14function raw() {
15 return fs.createReadStream(path.join(__dirname, 'test.txt'), 'utf8');
16}
17
18function delayer() {
19 return ps.through(function(line) {
20 return this.push(B.delay(1).then(function() {
21 return line ? parseFloat(line) : null;
22 }));
23 });
24}
25
26
27t.test('ps.wait', function(t) {
28 var last = 0;
29 return ps.wait(lines().pipe(ps.map(function(el) {
30 return B.delay(1).then(function() {
31 if (el) last = el;
32 return el;
33 });
34 }))).then(function() {
35 t.equal(last, "9", 'should wait for the last element')
36 });
37});
38
39t.test('map-wait', function(t) {
40 var last = 0;
41 return lines().pipe(delayer())
42 .map(function(el) {
43 return B.delay(1).then(function() {
44 return (last = el);
45 })
46 }).wait().then(function() {
47 t.equal(last, 9, 'should wait for the last element')
48 });
49});
50
51
52t.test('combined', function(t) {
53 return lines().pipe(delayer())
54 .map(function(el) {
55 return el * 2;
56 })
57 .filter(function(el) {
58 return el > 4
59 })
60 .reduce(function(acc, el) {
61 return acc + el;
62 })
63 .then(function(sum) {
64 t.equal(sum, 84, 'should map-reduce to correct sum');
65 });
66
67});
68
69t.test('collect', function(t) {
70 return ps.collect(raw()).then(function(data) {
71 t.equal(data.length, 18, 'test.txt should be 18 bytes long');
72 });
73});