StringArrayStorage.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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()
  45. .slice(0, StringArrayStorage.stringArrayNameLength);
  46. const baseStringArrayCallsWrapperName: string = this.identifierNamesGenerator
  47. .generate()
  48. .slice(0, StringArrayStorage.stringArrayNameLength);
  49. const stringArrayName: string = `${this.options.identifiersPrefix}${baseStringArrayName}`;
  50. const stringArrayCallsWrapperName: string = `${this.options.identifiersPrefix}${baseStringArrayCallsWrapperName}`;
  51. this.storageId = `${stringArrayName}|${stringArrayCallsWrapperName}`;
  52. }
  53. /**
  54. * @param {number} rotationValue
  55. */
  56. public rotateArray (rotationValue: number): void {
  57. this.storage = this.arrayUtils.rotate(this.storage, rotationValue);
  58. }
  59. /**
  60. * @returns {string}
  61. */
  62. public toString (): string {
  63. return this.storage.map((value: string) => {
  64. return `'${value}'`;
  65. }).toString();
  66. }
  67. }