# All files / src/node-obfuscators/replacers IdentifierReplacer.ts

92.31% Statements 12/13
75% Branches 3/4
50% Functions 1/2
91.67% Lines 11/12

    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

    48028x

    48028x

    1147756x

    1147756x
    1015462x

    132294x

    45453x
    45453x

    45453x

     import { AbstractReplacer } from './AbstractReplacer';
    import { Utils } from '../../Utils';

    export class IdentifierReplacer extends AbstractReplacer {
        /**
         * @type {Map<string, string>}
         */
        private namesMap: Map<string, string> = new Map<string, string>();

        /**
         * @param nodeValue
         * @returns {string}
         */
        public replace (nodeValue: string): string {
            const obfuscatedIdentifierName: string|undefined = this.namesMap.get(nodeValue);

            if (!obfuscatedIdentifierName) {
                return nodeValue;
            }

            return obfuscatedIdentifierName;
        }

        /**
         * Store all identifiers names as keys in given `namesMap` with random names as value.
         * Reserved names will be ignored.
         *
         * @param nodeName
         */
        public storeNames (nodeName: string): void {
            Eif (!this.isReservedName(nodeName)) {
                this.namesMap.set(nodeName, Utils.getRandomVariableName());
            }
        }

        /**
         * @param name
         * @returns {boolean}
         */
        private isReservedName (name: string): boolean {
            return this.options.reservedNames
                .some((reservedName: string) => {
                    return new RegExp(reservedName, 'g').test(name);
                });
        }
    }