RandomGeneratorUtils.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 readonly randomVariableNameSet: Set <string> = new Set();
  16. /**
  17. * @type {Chance.Chance | Chance.SeededChance}
  18. */
  19. private static randomGenerator: Chance.Chance | Chance.SeededChance;
  20. /**
  21. * @param seed
  22. */
  23. public static initializeRandomGenerator (seed: number): void {
  24. if (seed !== 0) {
  25. RandomGeneratorUtils.randomGenerator = new Chance(seed);
  26. } else {
  27. RandomGeneratorUtils.randomGenerator = new Chance();
  28. }
  29. }
  30. public static clearRandomGenerator (): void {
  31. RandomGeneratorUtils.randomVariableNameSet.clear();
  32. }
  33. /**
  34. * @param min
  35. * @param max
  36. * @returns {number}
  37. */
  38. public static getRandomFloat (min: number, max: number): number {
  39. return RandomGeneratorUtils.getRandomGenerator().floating({
  40. min: min,
  41. max: max,
  42. fixed: 7
  43. });
  44. }
  45. /**
  46. * @returns {Chance.Chance}
  47. */
  48. public static getRandomGenerator (): Chance.Chance {
  49. const randomGenerator: Chance.Chance = RandomGeneratorUtils.randomGenerator;
  50. if (!randomGenerator) {
  51. RandomGeneratorUtils.initializeRandomGenerator(0);
  52. }
  53. return RandomGeneratorUtils.randomGenerator;
  54. }
  55. /**
  56. * @param min
  57. * @param max
  58. * @returns {number}
  59. */
  60. public static getRandomInteger (min: number, max: number): number {
  61. return RandomGeneratorUtils.getRandomGenerator().integer({
  62. min: min,
  63. max: max
  64. });
  65. }
  66. /**
  67. * @param length
  68. * @param pool
  69. * @returns {string}
  70. */
  71. public static getRandomString (length: number, pool: string = RandomGeneratorUtils.randomGeneratorPool): string {
  72. return RandomGeneratorUtils.getRandomGenerator().string({ length, pool });
  73. }
  74. /**
  75. * @param length
  76. * @param withPrefix
  77. * @param unique
  78. * @returns {string}
  79. */
  80. public static getRandomVariableName (length: number, withPrefix: boolean, unique: boolean): string {
  81. const prefix: string = withPrefix ? `_${Utils.hexadecimalPrefix}` : '';
  82. const rangeMinInteger: number = 10000;
  83. const rangeMaxInteger: number = 99999999;
  84. const randomVariableName: string = `${prefix}${(
  85. Utils.decToHex(
  86. RandomGeneratorUtils.getRandomInteger(rangeMinInteger, rangeMaxInteger)
  87. )
  88. ).substr(0, length)}`;
  89. if (unique && RandomGeneratorUtils.randomVariableNameSet.has(randomVariableName)) {
  90. return RandomGeneratorUtils.getRandomVariableName(length, withPrefix, unique);
  91. }
  92. RandomGeneratorUtils.randomVariableNameSet.add(randomVariableName);
  93. return randomVariableName;
  94. }
  95. }