|
@@ -4,9 +4,9 @@ import { JSFuck } from './enums/JSFuck';
|
|
|
|
|
|
export class Utils {
|
|
export class Utils {
|
|
/**
|
|
/**
|
|
- * @type {Chance.Chance}
|
|
|
|
|
|
+ * @type {Chance.Chance | Chance.SeededChance}
|
|
*/
|
|
*/
|
|
- private static randomGenerator: Chance.Chance = new Chance();
|
|
|
|
|
|
+ private static randomGenerator: Chance.Chance | Chance.SeededChance = new Chance();
|
|
|
|
|
|
/**
|
|
/**
|
|
* @param array
|
|
* @param array
|
|
@@ -99,13 +99,44 @@ export class Utils {
|
|
return domain;
|
|
return domain;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * @param min
|
|
|
|
+ * @param max
|
|
|
|
+ * @returns {number}
|
|
|
|
+ */
|
|
|
|
+ public static getRandomFloat (min: number, max: number): number {
|
|
|
|
+ return Utils.getRandomGenerator().floating({
|
|
|
|
+ min: min,
|
|
|
|
+ max: max,
|
|
|
|
+ fixed: 7
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @returns {Chance.Chance}
|
|
* @returns {Chance.Chance}
|
|
*/
|
|
*/
|
|
public static getRandomGenerator (): Chance.Chance {
|
|
public static getRandomGenerator (): Chance.Chance {
|
|
|
|
+ const randomGenerator: Chance.Chance = Utils.randomGenerator;
|
|
|
|
+
|
|
|
|
+ if (!randomGenerator) {
|
|
|
|
+ throw new Error(`\`randomGenerator\` static property is undefined`);
|
|
|
|
+ }
|
|
|
|
+
|
|
return Utils.randomGenerator;
|
|
return Utils.randomGenerator;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * @param min
|
|
|
|
+ * @param max
|
|
|
|
+ * @returns {number}
|
|
|
|
+ */
|
|
|
|
+ public static getRandomInteger (min: number, max: number): number {
|
|
|
|
+ return Utils.getRandomGenerator().integer({
|
|
|
|
+ min: min,
|
|
|
|
+ max: max
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @param length
|
|
* @param length
|
|
* @returns {string}
|
|
* @returns {string}
|
|
@@ -117,10 +148,7 @@ export class Utils {
|
|
|
|
|
|
return `${prefix}${(
|
|
return `${prefix}${(
|
|
Utils.decToHex(
|
|
Utils.decToHex(
|
|
- Utils.getRandomGenerator().integer({
|
|
|
|
- min: rangeMinInteger,
|
|
|
|
- max: rangeMaxInteger
|
|
|
|
- })
|
|
|
|
|
|
+ Utils.getRandomInteger(rangeMinInteger, rangeMaxInteger)
|
|
)
|
|
)
|
|
).substr(0, length)}`;
|
|
).substr(0, length)}`;
|
|
}
|
|
}
|
|
@@ -140,7 +168,7 @@ export class Utils {
|
|
result: string = '';
|
|
result: string = '';
|
|
|
|
|
|
while (i1 < s1.length || i2 < s2.length) {
|
|
while (i1 < s1.length || i2 < s2.length) {
|
|
- if (Math.random() < 0.5 && i2 < s2.length) {
|
|
|
|
|
|
+ if (Utils.getRandomFloat(0, 1) < 0.5 && i2 < s2.length) {
|
|
result += s2.charAt(++i2);
|
|
result += s2.charAt(++i2);
|
|
} else {
|
|
} else {
|
|
result += s1.charAt(++i1);
|
|
result += s1.charAt(++i1);
|
|
@@ -213,6 +241,13 @@ export class Utils {
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * @param randomGenerator
|
|
|
|
+ */
|
|
|
|
+ public static setRandomGenerator (randomGenerator: Chance.Chance | Chance.SeededChance): void {
|
|
|
|
+ Utils.randomGenerator = randomGenerator;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @param obj
|
|
* @param obj
|
|
* @returns {T}
|
|
* @returns {T}
|