javascript-obfuscator icon indicating copy to clipboard operation
javascript-obfuscator copied to clipboard

[Idea] several-in-one-function

Open da411d opened this issue 4 years ago • 0 comments

Combine several functions in one and choose depending on arguments. I understand that it is hard not to break existing code, so it may be used for only new inserted code e.g. stringArray helpers or controlFlowFlattening.

Example 1

Before

function hello(name){
    console.log("Hello, " + name + "!");
}
function bye(name){
    console.log("Bye, " + name);
}

hello("Foo");
bye("Bar");

After

function hellobye(arg1, flag){
  if(Array.isArray(flag)){
    console.log("Hello, " + arg1 + "!");
  }else{
    console.log("Bye, " + arg1);
  }
}

hellobye("Foo", []);
hellobye("Bar", "0x123");

Example 2

Before

function hello(name, company){
    console.log("Hello, " + name + " from " + company + "!");
}
function bye(name){
    console.log("Bye, " + name);
}

hello("Foo", "FooBar Inc.");
bye("Bar");

After

function hellobye(arg1, arg2, flag){
    if(String(flag)[1] === "x"){
        console.log("Bye, " + arg1);
    }else{
        console.log("Hello, " + arg1 + " from " + arg2 + "!");
    }
}

hellobye("Foo", "FooBar Inc.", []);
hellobye("Bar", null, "0x123");

I used Array.isArray(flag) and String(flag)[1] === "x" for choosing code, but bitwise operators or even just numeric values (flag === 5) can be used as well.

da411d avatar Jul 17 '21 10:07 da411d