StringArrayStorage.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { inject, injectable, postConstruct } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
  4. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  5. import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
  6. import { IIdentifierNamesGenerator } from '../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { ArrayStorage } from '../ArrayStorage';
  9. @injectable()
  10. export class StringArrayStorage extends ArrayStorage <string> {
  11. /**
  12. * @type {number}
  13. */
  14. private static readonly stringArrayNameLength: number = 7;
  15. /**
  16. * @type {IArrayUtils}
  17. */
  18. private readonly arrayUtils: IArrayUtils;
  19. /**
  20. * @type {IIdentifierNamesGenerator}
  21. */
  22. private readonly identifierNamesGenerator: IIdentifierNamesGenerator;
  23. /**
  24. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  25. * @param {IArrayUtils} arrayUtils
  26. * @param {IRandomGenerator} randomGenerator
  27. * @param {IOptions} options
  28. */
  29. constructor (
  30. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  31. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  32. @inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
  33. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  34. @inject(ServiceIdentifiers.IOptions) options: IOptions
  35. ) {
  36. super(randomGenerator, options);
  37. this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
  38. this.arrayUtils = arrayUtils;
  39. }
  40. @postConstruct()
  41. public initialize (): void {
  42. super.initialize();
  43. const baseStringArrayName: string = this.identifierNamesGenerator
  44. .generate(StringArrayStorage.stringArrayNameLength);
  45. const baseStringArrayCallsWrapperName: string = this.identifierNamesGenerator
  46. .generate(StringArrayStorage.stringArrayNameLength);
  47. const stringArrayName: string = `${this.options.identifiersPrefix}${baseStringArrayName}`;
  48. const stringArrayCallsWrapperName: string = `${this.options.identifiersPrefix}${baseStringArrayCallsWrapperName}`;
  49. this.storageId = `${stringArrayName}|${stringArrayCallsWrapperName}`;
  50. }
  51. /**
  52. * @param {number} rotationValue
  53. */
  54. public rotateArray (rotationValue: number): void {
  55. this.storage = this.arrayUtils.rotate(this.storage, rotationValue);
  56. }
  57. /**
  58. * @returns {string}
  59. */
  60. public toString (): string {
  61. return this.storage.map((value: string) => {
  62. return `'${value}'`;
  63. }).toString();
  64. }
  65. }