StringArrayStorage.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 {IArrayUtils}
  13. */
  14. private readonly arrayUtils: IArrayUtils;
  15. /**
  16. * @type {IIdentifierNamesGenerator}
  17. */
  18. private readonly identifierNamesGenerator: IIdentifierNamesGenerator;
  19. /**
  20. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  21. * @param {IArrayUtils} arrayUtils
  22. * @param {IRandomGenerator} randomGenerator
  23. * @param {IOptions} options
  24. */
  25. constructor (
  26. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  27. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  28. @inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
  29. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  30. @inject(ServiceIdentifiers.IOptions) options: IOptions
  31. ) {
  32. super(randomGenerator, options);
  33. this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
  34. this.arrayUtils = arrayUtils;
  35. }
  36. @postConstruct()
  37. public initialize (): void {
  38. super.initialize();
  39. const baseStringArrayName: string = this.identifierNamesGenerator
  40. .generate();
  41. const baseStringArrayCallsWrapperName: string = this.identifierNamesGenerator
  42. .generate();
  43. const stringArrayName: string = `${this.options.identifiersPrefix}${baseStringArrayName}`;
  44. const stringArrayCallsWrapperName: string = `${this.options.identifiersPrefix}${baseStringArrayCallsWrapperName}`;
  45. this.storageId = `${stringArrayName}|${stringArrayCallsWrapperName}`;
  46. }
  47. /**
  48. * @param {number} rotationValue
  49. */
  50. public rotateArray (rotationValue: number): void {
  51. this.storage = this.arrayUtils.rotate(this.storage, rotationValue);
  52. }
  53. /**
  54. * @returns {string}
  55. */
  56. public toString (): string {
  57. return this.storage.map((value: string) => {
  58. return `'${value}'`;
  59. }).toString();
  60. }
  61. }