RandomGenerator.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { inject, injectable, postConstruct } from 'inversify';
  2. import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
  3. import md5 from 'md5';
  4. import { Chance } from 'chance';
  5. import { IInitializable } from '../interfaces/IInitializable';
  6. import { IOptions } from '../interfaces/options/IOptions';
  7. import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
  8. import { ISourceCode } from '../interfaces/source-code/ISourceCode';
  9. import { initializable } from '../decorators/Initializable';
  10. import { alphabetString } from '../constants/AlphabetString';
  11. import { alphabetStringUppercase } from '../constants/AlphabetStringUppercase';
  12. @injectable()
  13. export class RandomGenerator implements IRandomGenerator, IInitializable {
  14. /**
  15. * @type {string}
  16. */
  17. public static readonly randomGeneratorPool: string = `${alphabetString}${alphabetStringUppercase}`;
  18. /**
  19. * @type {Chance.Chance}
  20. */
  21. @initializable()
  22. private randomGenerator!: Chance.Chance;
  23. /**
  24. * @type {IOptions}
  25. */
  26. private readonly options: IOptions;
  27. /**
  28. * @type {ISourceCode}
  29. */
  30. private readonly sourceCode: ISourceCode;
  31. /**
  32. * @param {ISourceCode} sourceCode
  33. * @param {IOptions} options
  34. */
  35. public constructor (
  36. @inject(ServiceIdentifiers.ISourceCode) sourceCode: ISourceCode,
  37. @inject(ServiceIdentifiers.IOptions) options: IOptions
  38. ) {
  39. this.sourceCode = sourceCode;
  40. this.options = options;
  41. }
  42. @postConstruct()
  43. public initialize (): void {
  44. this.randomGenerator = new Chance(this.getRawSeed());
  45. }
  46. /**
  47. * @returns {number}
  48. */
  49. public getMathRandom (): number {
  50. return this.getRandomInteger(0, 99999) / 100000;
  51. }
  52. /**
  53. * @returns {Chance.Chance}
  54. */
  55. public getRandomGenerator (): Chance.Chance {
  56. return this.randomGenerator;
  57. }
  58. /**
  59. * @param {number} min
  60. * @param {number} max
  61. * @returns {number}
  62. */
  63. public getRandomInteger (min: number, max: number): number {
  64. return this.getRandomGenerator().integer({
  65. min: min,
  66. max: max
  67. });
  68. }
  69. /**
  70. * @param {number} min
  71. * @param {number} max
  72. * @param {number[]} valuesToExclude
  73. * @returns {number}
  74. */
  75. public getRandomIntegerExcluding (min: number, max: number, valuesToExclude: number[]): number {
  76. const valuesToPickArray: number[] = [];
  77. for (let value: number = min; value <= max; value++) {
  78. if (valuesToExclude.includes(value)) {
  79. continue;
  80. }
  81. valuesToPickArray.push(value);
  82. }
  83. return this.randomGenerator.pickone(valuesToPickArray);
  84. }
  85. /**
  86. * @param {number} length
  87. * @param {string} pool
  88. * @returns {string}
  89. */
  90. public getRandomString (length: number, pool: string = RandomGenerator.randomGeneratorPool): string {
  91. return this.getRandomGenerator().string({ length, pool });
  92. }
  93. /**
  94. * @returns {string}
  95. */
  96. public getInputSeed (): string {
  97. return this.options.seed.toString();
  98. }
  99. /**
  100. * We need to add numbers from md5 hash of source code to input seed to prevent same String Array name
  101. * for different bundles with same seed
  102. *
  103. * @returns {number}
  104. */
  105. public getRawSeed (): string {
  106. const inputSeed: string = this.getInputSeed();
  107. const inputSeedParts: string[] = `${inputSeed}`.split('|');
  108. if (inputSeedParts.length > 1) {
  109. return inputSeed;
  110. }
  111. const sourceCodeMD5Hash: string = md5(this.sourceCode.getSourceCode());
  112. return `${inputSeed}|${sourceCodeMD5Hash}`;
  113. }
  114. }