# All files / src OptionsNormalizer.ts

100% Statements 18/18
100% Branches 6/6
100% Functions 0/0
100% Lines 17/17

    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
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84

    1x

    1x

    1x

    1x

    23x

    69x
    69x

    23x

    23x
    10x

    23x

    23x
    11x

    23x

    23x
    12x

    23x

     import { IObfuscatorOptions } from "./interfaces/IObfuscatorOptions";

    import { TOptionsNormalizerRule } from "./types/TOptionsNormalizerRule";

    export class OptionsNormalizer {
        /**
         * @type {IObfuscatorOptions}
         */
        private static DISABLED_UNICODE_ARRAY_OPTIONS: IObfuscatorOptions = {
            encodeUnicodeLiterals: false,
            rotateUnicodeArray: false,
            unicodeArray: false,
            unicodeArrayThreshold: 0,
            wrapUnicodeArrayCalls: false
        };

        /**
         * @type {IObfuscatorOptions}
         */
        private static SELF_DEFENDING_OPTIONS: IObfuscatorOptions = {
            compact: true,
            selfDefending: true
        };

        /**
         * @type {TOptionsNormalizerRule[]}
         */
        private static normalizerRules: TOptionsNormalizerRule[] = [
            OptionsNormalizer.unicodeArrayRule,
            OptionsNormalizer.unicodeArrayThresholdRule,
            OptionsNormalizer.selfDefendingRule
        ];

        /**
         * @param options
         * @returns {IObfuscatorOptions}
         */
        public static normalizeOptions (options: IObfuscatorOptions): IObfuscatorOptions {
            let normalizedOptions: IObfuscatorOptions = Object.assign({}, options);

            for (let normalizerRule of OptionsNormalizer.normalizerRules) {
                normalizedOptions = normalizerRule(normalizedOptions);
            }

            return normalizedOptions;
        }

        /**
         * @param options
         * @returns {IObfuscatorOptions}
         */
        private static selfDefendingRule (options: IObfuscatorOptions): IObfuscatorOptions {
            if (options.selfDefending) {
                Object.assign(options, OptionsNormalizer.SELF_DEFENDING_OPTIONS);
            }

            return options;
        }

        /**
         * @param options
         * @returns {IObfuscatorOptions}
         */
        private static unicodeArrayRule (options: IObfuscatorOptions): IObfuscatorOptions {
            if (!options.unicodeArray) {
                Object.assign(options, OptionsNormalizer.DISABLED_UNICODE_ARRAY_OPTIONS);
            }

            return options;
        }

        /**
         * @param options
         * @returns {IObfuscatorOptions}
         */
        private static unicodeArrayThresholdRule (options: IObfuscatorOptions): IObfuscatorOptions {
            if (options.unicodeArrayThreshold === 0) {
                Object.assign(options, OptionsNormalizer.DISABLED_UNICODE_ARRAY_OPTIONS);
            }

            return options;
        }
    }