Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially hide email address in TypeScript(Javascript)

I am using TypeScript(Javascript)

How can I partially hide email address like this?

I only found an answer using PHP.

Thanks

like image 634
Hongbo Miao Avatar asked Feb 21 '16 02:02

Hongbo Miao


2 Answers

As stated above, this really isn't a JavaScript job, but here's something short to get you started:

var censorWord = function (str) {
   return str[0] + "*".repeat(str.length - 2) + str.slice(-1);
}

var censorEmail = function (email){
     var arr = email.split("@");
     return censorWord(arr[0]) + "@" + censorWord(arr[1]);
}

console.log(censorEmail("[email protected]"));

j*********n@g*******m

like image 160
Charles Clayton Avatar answered Oct 04 '22 12:10

Charles Clayton


Like when the lab technician makes kryptonite for Richard Prior in Superman III i've got to say "i'm not sure what you need this for, but here you go..."

Its kinda long-form but that shows the algorithm better.

function obscure_email(email) {
    var parts = email.split("@");
    var name = parts[0];
    var result = name.charAt(0);
    for(var i=1; i<name.length; i++) {
        result += "*";
    }
    result += name.charAt(name.length - 1);
    result += "@";
    var domain = parts[1];
    result += domain.charAt(0);
    var dot = domain.indexOf(".");
    for(var i=1; i<dot; i++) {
        result += "*";
    }
    result += domain.substring(dot);

    return result;
}
like image 32
Gordon MacDonald Avatar answered Oct 04 '22 14:10

Gordon MacDonald