All files / src Utils.js

100% Statements 44/44
94.12% Branches 16/17
100% Functions 2/2
100% Lines 37/37
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  1x 1x     850x   7x 7x 4x   3x 3x 211x 209x 209x     2x 2x     3x     1x     80x 80x           34x   33x 33x 33x           47x     1x     1x     6x         41x 41x 41x 233x 226x 226x     7x 7x   233x       1x 1x 1x  
"use strict";
const chance_1 = require('chance');
const JSFuck_1 = require('./enums/JSFuck');
class Utils {
    static arrayContains(array, searchElement) {
        return array.indexOf(searchElement) >= 0;
    }
    static arrayRotate(array, times, reverse = false) {
        if (times <= 0) {
            return array;
        }
        let newArray = array, temp;
        while (times--) {
            if (!reverse) {
                temp = newArray.pop();
                newArray.unshift(temp);
            }
            else {
                temp = newArray.shift();
                newArray.push(temp);
            }
        }
        return newArray;
    }
    static btoa(string) {
        return new Buffer(encodeURI(string)).toString('base64');
    }
    static decToHex(dec) {
        const decToHexSliceValue = -6, exponent = 6, radix = 16;
        return (dec + Math.pow(radix, exponent))
            .toString(radix)
            .substr(decToHexSliceValue)
            .replace(Utils.hexRepetitiveZerosRegExp, '');
    }
    static getRandomGenerator() {
        return Utils.randomGenerator;
    }
    static getRandomVariableName(length = 6) {
        const rangeMinInteger = 10000, rangeMaxInteger = 99999999, prefix = '_0x';
        return `${prefix}${(Utils.decToHex(Utils.getRandomGenerator().integer({
            min: rangeMinInteger,
            max: rangeMaxInteger
        }))).substr(0, length)}`;
    }
    static isInteger(number) {
        return number % 1 === 0;
    }
    static strEnumify(obj) {
        return obj;
    }
    static stringToJSFuck(string) {
        return Array
            .from(string)
            .map((character) => {
            return JSFuck_1.JSFuck[character] || character;
        })
            .join(' + ');
    }
    static stringToUnicode(string) {
        const radix = 16;
        let prefix, regexp = new RegExp('[\x00-\x7F]'), template;
        return `'${string.replace(/[\s\S]/g, (escape) => {
            if (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)}`;
        })}'`;
    }
}
Utils.hexRepetitiveZerosRegExp = new RegExp('^(0{2,})+(?!$)', '');
Utils.randomGenerator = new chance_1.Chance();
exports.Utils = Utils;
//# sourceMappingURL=Utils.js.map