-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
58 lines (47 loc) · 1.36 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
function RandomInRange(minValue, maxValue) {
return Math.round(Math.random() * (maxValue - minValue)) + minValue;
}
function Shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function GenerateRandomSet(randomGenerator, size) {
let resultSet = new Set();
while (resultSet.size < size) {
resultSet.add(randomGenerator());
}
return resultSet;
}
function Range(start, count) {
return [...Array(count).keys()].map((elmnt) => elmnt + start);
}
function FilterArrWithSet(arr, set) {
return arr.filter(value => !set.has(value));
}
function Clamp(num, min, max) {
return Math.min(Math.max(num, min), max);
}
function pipe(until, fallback, ...fns) {
return (input) => {
for (fnc of fns) {
temp = fnc(input);
if (until(temp)) return fallback(temp);
input = Object.assign(input, temp);
}
return input;
};
}
function isNegative(value) {
return Math.sign(value) === -1 ? true : false;
}
module.exports.RandomInRange = RandomInRange;
module.exports.Shuffle = Shuffle;
module.exports.GenerateRandomSet = GenerateRandomSet;
module.exports.Range = Range;
module.exports.FilterArrWithSet = FilterArrWithSet;
module.exports.Clamp = Clamp;
module.exports.isNegative = isNegative;
module.exports.pipe = pipe;