-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
30 lines (23 loc) · 692 Bytes
/
utils.js
File metadata and controls
30 lines (23 loc) · 692 Bytes
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
export function getRandomChar(charset) {
return charset[getRandomInt(charset.length)];
}
function reduceTo(value, max) {
function reduce(value) {
if (value > max) {
return reduce(value * Math.random())
}
return value;
}
return reduce(value)
}
export function getCryptoRandom() {
return window.crypto.getRandomValues(new Uint8Array(1))[0];
}
export function getRandomInt(max) {
const randomInt = getCryptoRandom(max)
const randomIntReduced = reduceTo(randomInt, max)
return Math.floor(randomIntReduced);
}
export function replaceAt(src, char, index) {
return src.substr(0, index) + char + src.substr(index + 1);
}