All files / src/utils RandomGeneratorUtils.ts

95.24% Statements 20/21
81.25% Branches 13/16
100% Functions 0/0
95% Lines 19/20
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 921x   1x   1x       1x         1x         1x               50178x                     119013x   119013x       119013x                 37321x                     31105x 31105x               31278x 31278x 31278x 31278x   31278x                     34x      
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 {Chance.Chance | Chance.SeededChance}
     */
    private static randomGenerator: Chance.Chance | Chance.SeededChance = new Chance();
 
    /**
     * @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;
 
        Iif (!randomGenerator) {
            throw new Error(`\`randomGenerator\` static property is undefined`);
        }
 
        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
     * @returns {string}
     */
    public static getRandomVariableName (length: number = 6, withPrefix: boolean = true): string {
        const prefix: string = withPrefix ? `_${Utils.hexadecimalPrefix}` : '';
        const rangeMinInteger: number = 10000;
        const rangeMaxInteger: number = 99999999;
 
        return `${prefix}${(
            Utils.decToHex(
                RandomGeneratorUtils.getRandomInteger(rangeMinInteger, rangeMaxInteger)
            )
        ).substr(0, length)}`;
    }
 
    /**
     * @param randomGenerator
     */
    public static setRandomGenerator (randomGenerator: Chance.Chance | Chance.SeededChance): void {
        RandomGeneratorUtils.randomGenerator = randomGenerator;
    }
}