NumberLiteralObfuscatingReplacer.ts 1.4 KB

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