-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
46 lines (37 loc) · 883 Bytes
/
helpers.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
function runInPromise(fn, args = [], ctx = fn) {
return new Promise((resolve, reject) => {
if (!fn) {
resolve(null);
return;
}
args.push((err, result) => {
if (err) reject(err);
else resolve(result);
});
fn.apply(ctx, args);
});
}
function randomStr(length) {
let result = '';
if (!length) length = 5 + Math.floor(Math.random() * 10);
while (result.length < length) {
result += Math.random().toString(36).slice(2);
}
return result.substr(0, length);
}
function randomInt(min = 0, max = 1000) {
return Math.round(Math.random() * (max - min) + min);
}
const sets = new Map();
function getFromSet(set, setName) {
if (!sets.has(setName)) {
sets.set(setName, [...set]);
}
return sets.get(setName)[randomInt(0, set.size - 1)];
}
module.exports = {
getFromSet,
randomInt,
randomStr,
runInPromise
};