-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
43 lines (37 loc) · 945 Bytes
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
const _ = require('highland');
function pick(arr, random) {
random = random || Math.random;
return arr[Math.floor(random()*arr.length)];
}
function fan(streams) {
return _.through((s) => {
const sources = streams.map(() => s.fork())
, sinks = sources.map((s, i) => s.through(streams[i]));
return _(sinks).merge();
});
}
function spread(streams, random) {
return _.through((s) => {
const sources = streams.map(() => _())
, sinks = sources.map((s, i) => s.through(streams[i]))
, errors = s.consume((err, x, push, next) => {
if (err) {
push(err);
next();
} else if (x === _.nil) {
sources.forEach((s) => s.end());
push(null, _.nil);
} else {
pick(sources, random).write(x);
next();
}
});
return _(sinks).append(errors).merge();
});
}
module.exports = {
pick: pick,
fan: fan,
spread: spread
};