All files / webpack:/src Utils.ts

85.29% Statements 29/34
62.5% Branches 10/16
100% Functions 2/2
85.29% Lines 29/34
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571x   1x           1x         1x               1850x                   1x       1x     1x 426x 426x 426x             1x             15x               141x       141x                   52x               51x       51x                             90x               1x               12x     60x                   131x   131x       131x 1028x 1028x 1028x           1028x                
import { Chance } from 'chance';
 
import { JSFuck } from './enums/JSFuck';
 
export class Utils {
    /**
     * @type {RegExp}
     */
    private static hexRepetitiveZerosRegExp: RegExp = new RegExp('^(0{2,})+(?!$)', '');
 
    /**
     * @type {Chance.Chance}
     */
    private static randomGenerator: Chance.Chance = new Chance();
 
    /**
     * @param array
     * @param searchElement
     * @returns {boolean}
     */
    public static arrayContains (array: any[], searchElement: any): boolean {
        return array.indexOf(searchElement) >= 0;
    }
 
    /**
     * @param array
     * @param times
     * @param reverse
     * @returns {T[]}
     */
    public static arrayRotate <T> (array: T[], times: number, reverse: boolean = false): T[] {
        if I(times < 0) {
            return;
        }
 
        let newArray: T[] = array,
            temp: T;
 
        while (times--) {
            if E(!reverse) {
                temp = newArray.pop();
                newArray.unshift(temp);
            } else {
                temp = newArray.shift();
                newArray.push(temp);
            }
        }
 
        return newArray;
    }
 
    /**
     * @param string
     */
    public static btoa (string: string): string {
        return new Buffer(encodeURI(string)).toString('base64');
    }
 
    /**
     * @param dec
     * @returns {string}
     */
    public static decToHex (dec: number): string {
        const decToHexSliceValue: number = -6,
            exponent: number = 6,
            radix: number = 16;
 
        return (dec + Math.pow(radix, exponent))
            .toString(radix)
            .substr(decToHexSliceValue)
            .replace(Utils.hexRepetitiveZerosRegExp, '');
    }
 
    /**
     * @returns {Chance.Chance}
     */
    public static getRandomGenerator (): Chance.Chance {
        return Utils.randomGenerator;
    }
 
    /**
     * @param length
     * @returns any
     */
    public static getRandomVariableName (length: number = 6): string {
        const rangeMinInteger: number = 10000,
            rangeMaxInteger: number = 99999999,
            prefix: string = '_0x';
 
        return `${prefix}${(
            Utils.decToHex(
                Utils.getRandomGenerator().integer({
                    min: rangeMinInteger,
                    max: rangeMaxInteger
                })
            )
        ).substr(0, length)}`;
    }
 
    /**
     * @param number
     * @returns {boolean}
     */
    public static isInteger (number: number): boolean {
        return number % 1 === 0;
    }
 
    /**
     * @param obj
     * @returns {T}
     */
    public static strEnumify <T extends {[prop: string]: ''|string}> (obj: T): T {
        return obj;
    }
 
    /**
     * @param string
     * @returns {string}
     */
    public static stringToJSFuck (string: string): string {
        return Array
            .from(string)
            .map((character: string): string => {
                return JSFuck[character] || character;
            })
            .join(' + ');
    }
 
    /**
     * @param string
     * @returns {string}
     */
    public static stringToUnicode (string: string): string {
        const radix: number = 16;
 
        let prefix: string,
            regexp: RegExp = new RegExp('[\x00-\x7F]'),
            template: string;
 
        return `'${string.replace(/[\s\S]/g, (escape: string): string => {
            if E(regexp.test(escape)) {
                prefix = '\\x';
                template = '0'.repeat(2);
            } else {
                prefix = '\\u';
                template = '0'.repeat(4);  
            }
            
            return `${prefix}${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`;
        })}'`;
    }
}
 
 
 
// WEBPACK FOOTER //
// ./src/Utils.ts