All files / src/node-transformers/obfuscation-transformers/replacers NumberLiteralReplacer.ts

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