All files / src/templates/custom-nodes/string-array-nodes/string-array-calls-wrapper SelfDefendingTemplate.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 3/3
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 421x         1x 1x                                                                      
import { Utils } from '../../../../utils/Utils';
 
/**
 * @returns {string}
 */
export function SelfDefendingTemplate (): string {
    return `
        var StatesClass = function (rc4Bytes) {
            this.rc4Bytes = rc4Bytes;
            this.states = [1, 0, 0];
            this.newState = function(){return 'newState';};
            this.firstState = '${Utils.stringToUnicodeEscapeSequence(`\\w+ *\\(\\) *{\\w+ *`)}';
            this.secondState = '${Utils.stringToUnicodeEscapeSequence(`['|"].+['|"];? *}`)}';
        };
        
        StatesClass.prototype.checkState = function () {
            var regExp = new RegExp(this.firstState + this.secondState);
 
            return this.runState(regExp.test(this.newState.toString()) ? --this.states[1] : --this.states[0]);
        };
        
        StatesClass.prototype.runState = function (stateResult) {
            if (!Boolean(~stateResult)) {
                return stateResult;
            }
            
            return this.getState(this.rc4Bytes);
        };
 
        StatesClass.prototype.getState = function (rc4Bytes) {
            for (var i = 0, len = this.states.length; i < len; i++) {
                this.states.push(Math.round(Math.random()));
                len = this.states.length;
            }
            
            return rc4Bytes(this.states[0]);
        };
 
        new StatesClass({stringArrayCallsWrapperName}).checkState();
    `;
}