RandomGeneratorUtils.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Chance } from 'chance';
  2. import { Utils } from './Utils';
  3. export class RandomGeneratorUtils {
  4. /**
  5. * @type {string}
  6. */
  7. public static readonly randomGeneratorPool: string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  8. /**
  9. * @type {string}
  10. */
  11. public static readonly randomGeneratorPoolWithNumbers: string = `${RandomGeneratorUtils.randomGeneratorPool}0123456789`;
  12. /**
  13. * @type {Set<string>}
  14. */
  15. public static randomVariableNameSet: Set <string> = new Set();
  16. /**
  17. * @type {Chance.Chance | Chance.SeededChance}
  18. */
  19. private static randomGenerator: Chance.Chance | Chance.SeededChance = new Chance();
  20. /**
  21. * @param min
  22. * @param max
  23. * @returns {number}
  24. */
  25. public static getRandomFloat (min: number, max: number): number {
  26. return RandomGeneratorUtils.getRandomGenerator().floating({
  27. min: min,
  28. max: max,
  29. fixed: 7
  30. });
  31. }
  32. /**
  33. * @returns {Chance.Chance}
  34. */
  35. public static getRandomGenerator (): Chance.Chance {
  36. const randomGenerator: Chance.Chance = RandomGeneratorUtils.randomGenerator;
  37. if (!randomGenerator) {
  38. throw new Error(`\`randomGenerator\` static property is undefined`);
  39. }
  40. return RandomGeneratorUtils.randomGenerator;
  41. }
  42. /**
  43. * @param min
  44. * @param max
  45. * @returns {number}
  46. */
  47. public static getRandomInteger (min: number, max: number): number {
  48. return RandomGeneratorUtils.getRandomGenerator().integer({
  49. min: min,
  50. max: max
  51. });
  52. }
  53. /**
  54. * @param length
  55. * @param pool
  56. * @returns {string}
  57. */
  58. public static getRandomString (length: number, pool: string = RandomGeneratorUtils.randomGeneratorPool): string {
  59. return RandomGeneratorUtils.getRandomGenerator().string({ length, pool });
  60. }
  61. /**
  62. * @param length
  63. * @param withPrefix
  64. * @param unique
  65. * @returns {string}
  66. */
  67. public static getRandomVariableName (length: number, withPrefix: boolean, unique: boolean): string {
  68. const prefix: string = withPrefix ? `_${Utils.hexadecimalPrefix}` : '';
  69. const rangeMinInteger: number = 10000;
  70. const rangeMaxInteger: number = 99999999;
  71. const randomVariableName: string = `${prefix}${(
  72. Utils.decToHex(
  73. RandomGeneratorUtils.getRandomInteger(rangeMinInteger, rangeMaxInteger)
  74. )
  75. ).substr(0, length)}`;
  76. if (unique && RandomGeneratorUtils.randomVariableNameSet.has(randomVariableName)) {
  77. return RandomGeneratorUtils.getRandomVariableName(length, withPrefix, unique);
  78. }
  79. RandomGeneratorUtils.randomVariableNameSet.add(randomVariableName);
  80. return randomVariableName;
  81. }
  82. /**
  83. * @param randomGenerator
  84. */
  85. public static setRandomGenerator (randomGenerator: Chance.Chance | Chance.SeededChance): void {
  86. RandomGeneratorUtils.randomGenerator = randomGenerator;
  87. }
  88. }