All files / src/custom-nodes/string-array-nodes StringArrayNode.ts

100% Statements 20/20
75% Branches 6/8
100% Functions 1/1
100% Lines 20/20
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 1011x                   1x   1x   1x 1x     1x       1x         529x                                                   529x 529x       529x 529x 529x             529x 3x     526x             526x                   32626x             526x   526x      
import * as format from 'string-template';
 
import { TNodeWithBlockStatement } from '../../types/node/TNodeWithBlockStatement';
import { TObfuscationEvent } from '../../types/event-emitters/TObfuscationEvent';
import { TStatement } from '../../types/node/TStatement';
 
import { ICustomNodeWithData } from '../../interfaces/custom-nodes/ICustomNodeWithData';
import { IOptions } from '../../interfaces/options/IOptions';
import { IStorage } from '../../interfaces/storages/IStorage';
 
import { ObfuscationEvents } from '../../enums/ObfuscationEvents';
 
import { StringArrayTemplate } from '../../templates/custom-nodes/string-array-nodes/string-array-node/StringArrayTemplate';
 
import { AbstractCustomNode } from '../AbstractCustomNode';
import { NodeAppender } from '../../node/NodeAppender';
import { StringArrayStorage } from '../../storages/string-array/StringArrayStorage';
 
export class StringArrayNode extends AbstractCustomNode implements ICustomNodeWithData {
    /**
     * @type {number}
     */
    public static ARRAY_RANDOM_LENGTH: number = 4;
 
    /**
     * @type {TObfuscationEvent}
     */
    protected readonly appendEvent: TObfuscationEvent = ObfuscationEvents.AfterObfuscation;
 
    /**
     * @type {IStorage <string>}
     */
    private stringArray: IStorage <string>;
 
    /**
     * @type {string}
     */
    private stringArrayName: string;
 
    /**
     * @type {number}
     */
    private stringArrayRotateValue: number;
 
    /**
     * @param stringArray
     * @param stringArrayName
     * @param stringArrayRotateValue
     * @param options
     */
    constructor (
        stringArray: IStorage <string>,
        stringArrayName: string,
        stringArrayRotateValue: number = 0,
        options: IOptions
    ) {
        super(options);
 
        this.stringArray = stringArray;
        this.stringArrayName = stringArrayName;
        this.stringArrayRotateValue = stringArrayRotateValue;
    }
 
    /**
     * @param blockScopeNode
     */
    public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
        if (!this.stringArray.getLength()) {
            return;
        }
 
        NodeAppender.prependNode(blockScopeNode, this.getNode());
    }
 
    /**
     * @returns {string}
     */
    public getCode (): string {
        return format(StringArrayTemplate(), {
            stringArrayName: this.stringArrayName,
            stringArray: this.stringArray.toString()
        });
    }
 
    /**
     * @returns {IStorage <string>}
     */
    public getNodeData (): IStorage <string> {
        return this.stringArray;
    }
 
    /**
     * @returns {TStatement[]}
     */
    public getNode (): TStatement[] {
        (<StringArrayStorage>this.stringArray).rotateArray(this.stringArrayRotateValue);
 
        return super.getNode();
    }
}