py-random-js 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/randomizer.js +25 -7
package/package.json
CHANGED
package/randomizer.js
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
|
-
class Randomizer{
|
|
1
|
+
class Randomizer {
|
|
2
|
+
constructor() {
|
|
3
|
+
const upperChars = (Array.from({ length: 26 }, (_, i) => String.fromCharCode(65 + i))).join('');
|
|
4
|
+
const lowerChars = (Array.from({ length: 26 }, (_, i) => String.fromCharCode(97 + i))).join('') ;
|
|
5
|
+
const digits = (Array.from({ length: 10 }, (_, i) => String.fromCharCode(48 + i))).join('');
|
|
6
|
+
this.alphabet = upperChars + lowerChars + digits + "+/";
|
|
7
|
+
this.hex = lowerChars.slice(0, 6) + digits;
|
|
8
|
+
}
|
|
2
9
|
int(targetValue) {
|
|
3
10
|
return Math.floor(Math.random() * (targetValue + 1));
|
|
4
11
|
}
|
|
12
|
+
float(targetValue, targetRound = 2) {
|
|
13
|
+
const randomFloat = Math.random() * targetValue;
|
|
14
|
+
return Number(randomFloat.toFixed(targetRound));
|
|
15
|
+
}
|
|
5
16
|
array(targetArray) {
|
|
6
17
|
const randomElement = Math.floor(Math.random() * targetArray.length);
|
|
7
18
|
return targetArray[randomElement];
|
|
8
19
|
}
|
|
9
|
-
string(targetLength) {
|
|
10
|
-
const chars =
|
|
11
|
-
|
|
20
|
+
string(targetLength, hex = false) {
|
|
21
|
+
const chars = hex ? this.hex
|
|
22
|
+
: this.alphabet;
|
|
23
|
+
let str = '';
|
|
12
24
|
for (let i = targetLength; i > 0; i--) {
|
|
13
25
|
let char = chars[Math.floor(Math.random() * chars.length)];
|
|
14
|
-
|
|
26
|
+
str = str + char;
|
|
15
27
|
}
|
|
16
|
-
return
|
|
28
|
+
return str;
|
|
17
29
|
}
|
|
18
30
|
shuffle(targetArray) {
|
|
19
31
|
for (let i = targetArray.length - 1; i > 0; i--) {
|
|
@@ -23,11 +35,17 @@ class Randomizer{
|
|
|
23
35
|
return targetArray;
|
|
24
36
|
}
|
|
25
37
|
coinFlip() {
|
|
26
|
-
return Math.random() < 0.5
|
|
38
|
+
return Math.random() < 0.5;
|
|
27
39
|
}
|
|
28
40
|
chance(targetPercentage) {
|
|
29
41
|
return Math.random() < (targetPercentage / 100);
|
|
30
42
|
}
|
|
43
|
+
uuid() { // 8-4-4-4-12 (xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx)
|
|
44
|
+
return `${this.string(8, true)}-${this.string(4, true)}-4${this.string(3, true)}-${this.string(4, true)}-${this.string(12, true)}`;
|
|
45
|
+
}
|
|
46
|
+
color() {
|
|
47
|
+
return `#${this.string(6, true)}`;
|
|
48
|
+
}
|
|
31
49
|
}
|
|
32
50
|
export default new Randomizer();
|
|
33
51
|
|