-
Notifications
You must be signed in to change notification settings - Fork 2
/
someOf.js
40 lines (34 loc) · 940 Bytes
/
someOf.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
var hash = require('./hash')
var fit = require('./utils/fit')
var resolve = require('./utils/resolve')
function someOf(a, b, c) {
return b != null && c != null ? someOfMain(a, b, c) : someOfCurried(a, b)
}
function someOfMain(input, range, samples) {
var ids = hash.sequence2(input, 'someOf')
var n =
typeof range === 'number'
? range
: fit(ids.next().value, range[0], range[1])
var i = -1
var results = []
var remaining = samples.slice()
var remainingLen
var chosenIndex
var chosen
var id
while (++i < n && (remainingLen = remaining.length)) {
id = ids.next().value
chosenIndex = id % remainingLen
chosen = remaining[chosenIndex]
remaining.splice(chosenIndex, 1)
results.push(resolve(id, chosen))
}
return results
}
function someOfCurried(range, samples) {
return function someOfCurriedFn(input) {
return someOf(input, range, samples)
}
}
module.exports = someOf