All files / src/utils RandomGeneratorUtils.ts

96.55% Statements 28/29
100% Branches 10/10
100% Functions 0/0
96.55% Lines 28/29
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 1281x   1x   1x       1x         1x         1x         1x         1x                     6286x 606x   5680x         6263x             74241x                                       569539x   569539x 1x     569539x                 241300x                     42289x 42289x               76757x 76757x 76757x 76757x           76757x 2x     76755x   76755x      
import { Chance } from 'chance';
 
import { Utils } from './Utils';
 
export class RandomGeneratorUtils {
    /**
     * @type {string}
     */
    public static readonly randomGeneratorPool: string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 
    /**
     * @type {string}
     */
    public static readonly randomGeneratorPoolNumbers: string = '0123456789';
 
    /**
     * @type {string}
     */
    public static readonly randomGeneratorPoolHexadecimal: string = `abcdef${RandomGeneratorUtils.randomGeneratorPoolNumbers}`;
 
    /**
     * @type {string}
     */
    public static readonly randomGeneratorPoolWithNumbers: string = `${RandomGeneratorUtils.randomGeneratorPool}${RandomGeneratorUtils.randomGeneratorPoolNumbers}`;
 
    /**
     * @type {Set<string>}
     */
    public static readonly randomVariableNameSet: Set <string> = new Set();
 
    /**
     * @type {Chance.Chance | Chance.SeededChance}
     */
    private static randomGenerator: Chance.Chance | Chance.SeededChance;
 
    /**
     * @param seed
     */
    public static initializeRandomGenerator (seed: number): void {
        if (seed !== 0) {
            RandomGeneratorUtils.randomGenerator = new Chance(seed);
        } else {
            RandomGeneratorUtils.randomGenerator = new Chance();
        }
    }
 
    public static clearRandomGenerator (): void {
        RandomGeneratorUtils.randomVariableNameSet.clear();
    }
 
    /**
     * @returns {number}
     */
    public static getMathRandom (): number {
        return RandomGeneratorUtils.getRandomInteger(0, 99999) / 100000;
    }
 
    /**
     * @param min
     * @param max
     * @returns {number}
     */
    public static getRandomFloat (min: number, max: number): number {
        return RandomGeneratorUtils.getRandomGenerator().floating({
            min: min,
            max: max,
            fixed: 7
        });
    }
 
    /**
     * @returns {Chance.Chance}
     */
    public static getRandomGenerator (): Chance.Chance {
        const randomGenerator: Chance.Chance = RandomGeneratorUtils.randomGenerator;
 
        if (!randomGenerator) {
            RandomGeneratorUtils.initializeRandomGenerator(0);
        }
 
        return RandomGeneratorUtils.randomGenerator;
    }
 
    /**
     * @param min
     * @param max
     * @returns {number}
     */
    public static getRandomInteger (min: number, max: number): number {
        return RandomGeneratorUtils.getRandomGenerator().integer({
            min: min,
            max: max
        });
    }
 
    /**
     * @param length
     * @param pool
     * @returns {string}
     */
    public static getRandomString (length: number, pool: string = RandomGeneratorUtils.randomGeneratorPool): string {
        return RandomGeneratorUtils.getRandomGenerator().string({ length, pool });
    }
 
    /**
     * @param length
     * @returns {string}
     */
    public static getRandomVariableName (length: number): string {
        const prefix: string = `_${Utils.hexadecimalPrefix}`;
        const rangeMinInteger: number = 10000;
        const rangeMaxInteger: number = 99999999;
        const randomVariableName: string = `${prefix}${(
            Utils.decToHex(
                RandomGeneratorUtils.getRandomInteger(rangeMinInteger, rangeMaxInteger)
            )
        ).substr(0, length)}`;
 
        if (RandomGeneratorUtils.randomVariableNameSet.has(randomVariableName)) {
            return RandomGeneratorUtils.getRandomVariableName(length);
        }
 
        RandomGeneratorUtils.randomVariableNameSet.add(randomVariableName);
 
        return randomVariableName;
    }
}