AbstractIdentifierNamesGenerator.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { inject, injectable } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { IIdentifierNamesGenerator } from '../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
  4. import { IOptions } from '../../interfaces/options/IOptions';
  5. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  6. @injectable()
  7. export abstract class AbstractIdentifierNamesGenerator implements IIdentifierNamesGenerator {
  8. /**
  9. * @type {string}
  10. */
  11. protected readonly identifiersPrefix: string;
  12. /**
  13. * @type {IOptions}
  14. */
  15. protected readonly options: IOptions;
  16. /**
  17. * @type {IRandomGenerator}
  18. */
  19. protected readonly randomGenerator: IRandomGenerator;
  20. /**
  21. * @param {IRandomGenerator} randomGenerator
  22. * @param {IOptions} options
  23. */
  24. constructor (
  25. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  26. @inject(ServiceIdentifiers.IOptions) options: IOptions
  27. ) {
  28. this.randomGenerator = randomGenerator;
  29. this.options = options;
  30. this.identifiersPrefix = this.options.identifiersPrefix === true
  31. ? this.randomGenerator.getRandomString(3)
  32. : this.options.identifiersPrefix || '';
  33. }
  34. /**
  35. * @param {number} length
  36. * @returns {string}
  37. */
  38. public abstract generate (length: number): string;
  39. }