All files / src/utils RandomGeneratorUtils.ts

100% Statements 26/26
85.71% Branches 12/14
100% Functions 0/0
100% Lines 26/26
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 1131x   1x   1x       1x         1x         1x                     6275x 606x   5669x         6252x                 73122x                     856072x   856072x 1x     856072x                 151660x                     28724x 28724x                   88879x 88879x 88879x 88879x           88879x 413x     88466x   88466x      
import { Chance } from 'chance';
 
import { Utils } from './Utils';
 
export class RandomGeneratorUtils {
    /**
     * @type {string}
     */
    public static readonly randomGeneratorPool: string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 
    /**
     * @type {string}
     */
    public static readonly randomGeneratorPoolWithNumbers: string = `${RandomGeneratorUtils.randomGeneratorPool}0123456789`;
 
    /**
     * @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();
    }
 
    /**
     * @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
     * @param withPrefix
     * @param unique
     * @returns {string}
     */
    public static getRandomVariableName (length: number, withPrefix: boolean, unique: boolean): string {
        const prefix: string = withPrefix ? `_${Utils.hexadecimalPrefix}` : '';
        const rangeMinInteger: number = 10000;
        const rangeMaxInteger: number = 99999999;
        const randomVariableName: string = `${prefix}${(
            Utils.decToHex(
                RandomGeneratorUtils.getRandomInteger(rangeMinInteger, rangeMaxInteger)
            )
        ).substr(0, length)}`;
 
        if (unique && RandomGeneratorUtils.randomVariableNameSet.has(randomVariableName)) {
            return RandomGeneratorUtils.getRandomVariableName(length, withPrefix, unique);
        }
 
        RandomGeneratorUtils.randomVariableNameSet.add(randomVariableName);
 
        return randomVariableName;
    }
}