VisitedLexicalScopeNodesStackStorage.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { inject, injectable } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { TNodeWithLexicalScopeStatements } from '../../types/node/TNodeWithLexicalScopeStatements';
  4. import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
  5. import { IOptions } from '../../interfaces/options/IOptions';
  6. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  7. import { IVisitedLexicalScopeNodesStackStorage } from '../../interfaces/storages/string-array-transformers/IVisitedLexicalScopeNodesStackStorage';
  8. import { ArrayStorage } from '../ArrayStorage';
  9. @injectable()
  10. export class VisitedLexicalScopeNodesStackStorage extends ArrayStorage <TNodeWithLexicalScopeStatements> implements IVisitedLexicalScopeNodesStackStorage {
  11. /**
  12. * @type {IArrayUtils}
  13. */
  14. private readonly arrayUtils: IArrayUtils;
  15. /**
  16. * @param {IRandomGenerator} randomGenerator
  17. * @param {IOptions} options
  18. * @param {IArrayUtils} arrayUtils
  19. */
  20. public constructor (
  21. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  22. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  23. @inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
  24. ) {
  25. super(randomGenerator, options);
  26. this.arrayUtils = arrayUtils;
  27. }
  28. /**
  29. * @returns {TNodeWithLexicalScopeStatements | undefined}
  30. */
  31. public getLastElement (): TNodeWithLexicalScopeStatements | undefined {
  32. return this.arrayUtils.getLastElement(this.getStorage());
  33. }
  34. /**
  35. * @returns {TNodeWithLexicalScopeStatements | undefined}
  36. */
  37. public getPenultimateElement (): TNodeWithLexicalScopeStatements | undefined {
  38. const storageLength: number = this.getLength();
  39. return this.get(storageLength - 2) ?? undefined;
  40. }
  41. /**
  42. * @param {TNodeWithLexicalScopeStatements} nodeWithLexicalScopeStatements
  43. */
  44. public push (nodeWithLexicalScopeStatements: TNodeWithLexicalScopeStatements): void {
  45. const storageLength: number = this.getLength();
  46. this.set(storageLength, nodeWithLexicalScopeStatements);
  47. }
  48. /**
  49. * @returns {TNodeWithLexicalScopeStatements| undefined}
  50. */
  51. public pop (): TNodeWithLexicalScopeStatements | undefined {
  52. const storageLength: number = this.getLength();
  53. return this.delete(storageLength - 1);
  54. }
  55. }