-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
37 lines (30 loc) · 1.1 KB
/
index.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
const AbstractEngine = module.exports.AbstractEngine = require('./lib/AbstractEngine.js')
const RandomEngine = module.exports.RandomEngine = require('./lib/RandomEngine.js')
const WeightedRandomEngine = module.exports.WeightedRandomEngine = require('./lib/WeightedRandomEngine.js')
const RoundRobinEngine = module.exports.RoundRobinEngine = require('./lib/RoundRobinEngine.js')
const WeightedRoundRobinEngine = module.exports.WeightedRoundRobinEngine = require('./lib/WeightedRoundRobinEngine.js')
module.exports.roundRobin = (pool) => {
if (pool.length === 0) {
throw new Error('pool length must be greater than zero')
}
const entry = pool[0]
if (entry.weight) {
return new WeightedRoundRobinEngine(pool)
} else {
return new RoundRobinEngine(pool)
}
}
module.exports.random = (pool, seed) => {
if (pool.length === 0) {
throw new Error('pool length must be greater than zero')
}
const entry = pool[0]
if (entry.weight) {
return new WeightedRandomEngine(pool, seed)
} else {
return new RandomEngine(pool, seed)
}
}
module.exports.isEngine = (engine) => {
return engine instanceof AbstractEngine
}