-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (82 loc) · 2.65 KB
/
index.js
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
module.exports = (email, variation = "[x3][*]") => {
// Validate email
if (!email || !email.includes("@")) {
throw new Error("obscure-email: No valid email was passed!");
}
// Validate variation
const charLoc = variation.indexOf("[x");
const charLoc2 = variation.indexOf("[x", charLoc + 1);
const ranStarLoc = variation.indexOf("[r*");
const staticStarLoc = variation.indexOf("[*");
if (charLoc < 0 || (ranStarLoc < 0 && staticStarLoc < 0)) {
throw new Error("obscure-email: Variation invalid!");
}
// Set @ loc and star str builder
const at = email.indexOf("@");
const getStars = (stars, loop, i) => {
if (i === loop) {
return stars;
}
return getStars((stars += "*"), loop, ++i);
};
const emailNoAt = email.substring(0, at);
// Vars to be used
let stars = "";
let loop;
let regex;
let obfuscatedEmail;
// Determine char nums
regex = /\[x(.*?)\]/;
const numChars = +variation.match(regex)[1] || 3;
const subChar2 = variation.substring(charLoc + 1).match(regex);
const numChars2 = (subChar2 && +subChar2[1]) || 0;
const parsedNumChars = numChars + numChars2;
// Get star string
if (ranStarLoc > -1) {
// Get the random number of stars or default to 10
regex = /\[r\*(.*?)\]/;
const numStars = variation.match(regex);
const parsedNumStars = +numStars[1] || 10;
loop = Math.floor(Math.random() * parsedNumStars + 1);
} else {
// Get the defined number of stars if any
regex = /\[\*(.*?)\]/;
const numStars = variation.match(regex);
loop = +numStars[1] || Math.max(1, at - parsedNumChars);
}
stars = getStars(stars, loop, 0);
// Build the email str
if (charLoc === 0) {
// Variations that start with text
const suffix = email.substring(at);
obfuscatedEmail = `${emailNoAt}${stars}${suffix}`;
if (charLoc2 < 0) {
if (emailNoAt.length > parsedNumChars) {
obfuscatedEmail = `${emailNoAt.substring(
0,
parsedNumChars
)}${stars}${suffix}`;
}
} else {
// Variation has second char set
if (emailNoAt.length > parsedNumChars) {
obfuscatedEmail = `${emailNoAt.substring(
0,
numChars
)}${stars}${emailNoAt.substring(at - numChars2)}${suffix}`;
}
}
} else if (ranStarLoc === 0 || staticStarLoc === 0) {
// Variations that starts with stars
obfuscatedEmail = `${stars}${email}`;
if (emailNoAt.length > parsedNumChars) {
obfuscatedEmail = `${stars}${email.substring(
at - parsedNumChars,
email.length
)}`;
}
} else {
throw new Error("obscure-email: Could not build string...");
}
return obfuscatedEmail;
};