NumberLiteralReplacer.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { IOptions } from '../../../interfaces/options/IOptions';
  4. import { AbstractReplacer } from './AbstractReplacer';
  5. import { Utils } from '../../../utils/Utils';
  6. @injectable()
  7. export class NumberLiteralReplacer extends AbstractReplacer {
  8. /**
  9. * @type {Map<string, string>}
  10. */
  11. private readonly numberLiteralCache: Map <number, string> = new Map();
  12. /**
  13. * @param options
  14. */
  15. constructor (
  16. @inject(ServiceIdentifiers.IOptions) options: IOptions
  17. ) {
  18. super(options);
  19. }
  20. /**
  21. * @param nodeValue
  22. * @returns {string}
  23. */
  24. public replace (nodeValue: number): string {
  25. if (this.numberLiteralCache.has(nodeValue)) {
  26. return <string>this.numberLiteralCache.get(nodeValue);
  27. }
  28. let result: string;
  29. if (!Utils.isCeilNumber(nodeValue)) {
  30. result = String(nodeValue);
  31. } else {
  32. result = `${Utils.hexadecimalPrefix}${Utils.decToHex(nodeValue)}`;
  33. }
  34. this.numberLiteralCache.set(nodeValue, result);
  35. return result;
  36. }
  37. }