Skip to content

Instantly share code, notes, and snippets.

@jayantbh
Created April 18, 2017 20:10
Show Gist options
  • Save jayantbh/b259945f9fd1f79af9d5357b612f2d6c to your computer and use it in GitHub Desktop.
Save jayantbh/b259945f9fd1f79af9d5357b612f2d6c to your computer and use it in GitHub Desktop.
Generate a random email based on a given name
function randomInt(a = 0, b = 0) { return (a + window.crypto.getRandomValues(new Uint32Array(1))[0] % Math.abs(b - a)).toString(); }
String.prototype.nRepeat = function (n) { return '_'.repeat(n).split('').map(x => this.toString()); }
String.prototype.removeVowels = function (n) { return this.toString().replace(/a|e|i|o|u/g,''); }
function toss() { return Boolean(Math.round(Math.random())); }
let domain = '@gmail.com'.nRepeat(7).concat(['@live.com', '@yahoo.com', '@hotmail.com']); // Giving more weight to gmail domains
let names = ['Tony Stark', 'Bruce Wayne', 'Steve Rogers', 'Clark Kent'];
function randomDomain() { return domain[randomInt(0, domain.length)]; }
function genEmail(name) {
let [fn, ln] = name.split(' ');
let revrs = toss(); // Reverse ID string
let rmfnv = toss(); fn = rmfnv ? fn.removeVowels() : fn; // Remove vowels from first name
let rmlnv = toss(); ln = rmlnv && ln ? ln.removeVowels() : ln; // Remove vowels from last name
let numfr = toss() ? randomInt(0, 2000) : null; // Insert random number before first name
let nummd = toss() ? randomInt(0, 2000) : null; // Insert random number between first and last name
let numlt = toss() ? randomInt(0, 2000) : null; // Insert random number after last name
let indot = toss() ? '.' : null; // Insert between first and last name
let id = [numfr, fn, indot, nummd, ln, numlt];
id = revrs ? id.reverse() : id;
return id.join('').toLowerCase().replace(/^[0-9.]+/,'') + randomDomain();
}
names.forEach(name => console.log(name + ' -> ' + genEmail(name)))
/** Sample Output
Tony Stark -> tony.stark@gmail.com
Bruce Wayne -> brucewayne81@gmail.com
Steve Rogers -> rogers83.stv90@gmail.com
Clark Kent -> knt213.clark@hotmail.com
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment