RandomGenerator.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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} length
  71. * @param {string} pool
  72. * @returns {string}
  73. */
  74. public getRandomString (length: number, pool: string = RandomGenerator.randomGeneratorPool): string {
  75. return this.getRandomGenerator().string({ length, pool });
  76. }
  77. /**
  78. * @returns {string}
  79. */
  80. public getInputSeed (): string {
  81. return this.options.seed.toString();
  82. }
  83. /**
  84. * We need to add numbers from md5 hash of source code to input seed to prevent same String Array name
  85. * for different bundles with same seed
  86. *
  87. * @returns {number}
  88. */
  89. public getRawSeed (): string {
  90. const inputSeed: string = this.getInputSeed();
  91. const inputSeedParts: string[] = `${inputSeed}`.split('|');
  92. if (inputSeedParts.length > 1) {
  93. return inputSeed;
  94. }
  95. const sourceCodeMD5Hash: string = md5(this.sourceCode.getSourceCode());
  96. return `${inputSeed}|${sourceCodeMD5Hash}`;
  97. }
  98. }