-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.js
64 lines (46 loc) · 1.43 KB
/
utils.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { emitterify, extend, keys, is } = require('utilise/pure')
const { random, pow } = Math
const ms = hrtime => hrtime[0] * 1e3 + hrtime[1] * 1e-6
const dp = (precision = 2) => num => ~~(num * pow(10, precision))/pow(10, precision)
const formatID = id => id.replace(/\./g, '-')
const jit = jitter => delay => delay*(1-jitter) + random()*delay*jitter
function emit(li, param1){
if (li)
for (let i = 0; i < li.length; i++)
li[i](param1)
}
const last = d => d && d[d.length-1] || {}
const combine = (arr, event, combined = emitterify()) => {
arr.map(parent => parent
.on(event)
.map(args => combined.emit(event, extend(args)({ parent }))))
return combined.on(event)
}
const merge = (...streams) => {
const output = emitterify().on('change')
keys(streams)
.map(stream => streams[stream]
.map(output.next)
)
return output
}
const stream = (input, { destroy = true, id } = {}) => emitterify(input)
.on('value')
.on('start', function(){
this.next({ type: 'update', value: input })
input
.on('change')
.map(this.next)
.until(this.once('stop'))
this
.once('stop')
.filter(d => destroy)
.map(d => input.emit('stop'))
})
.unpromise()
const avg = list => (list.reduce((a, b) => a + b, 0) / list.length)
const start = o => {
o.source.emit('start')
return o
}
module.exports = { formatID, jit, emit, last, combine, avg, ms, dp, merge, stream, start }