-
Notifications
You must be signed in to change notification settings - Fork 0
/
masking-personal-information.js
45 lines (42 loc) · 1.07 KB
/
masking-personal-information.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
/**
* @param {string} s
* @return {string}
*/
var maskPII = function (s) {
const emailReg = /\w+@\w+\.\w+/;
s = s.toLowerCase();
// 正则 ( 效率很低 )
if (emailReg.test(s)) {
// email
return s.replace(/(\w)([\w\s]*?)(\w)(@\w+)/, "$1*****$3$4");
} else {
// mobile
return s
.replace(/[\+\-\(\)\s]/g, "")
.replace(/(\d{0,3})(\d{6}(\d{4}))/, (match, p1, p2, p3) => {
if (p1.length === 0) {
return `***-***-${p3}`;
} else if (p1.length === 1) {
return `+*-***-***-${p3}`;
} else if (p1.length === 2) {
return `+**-***-***-${p3}`;
} else if (p1.length === 3) {
return `+***-***-***-${p3}`;
}
});
}
};
var maskPII2 = function (s) {
const index = s.indexOf("@");
if (index !== -1) {
return `${s[0]}*****${s[index - 1]}${s.slice(index)}`;
} else {
s = s.replace(/[^0-9]/g, "");
const len = s.length;
if (len === 10) {
return `***-***-${s.slice(-4)}`;
} else {
return `+${"*".repeat(len - 10)}-***-***-${s.slice(-4)}`;
}
}
};