Skip to content

Instantly share code, notes, and snippets.

@ikey4u
Last active April 20, 2020 13:07
Show Gist options
  • Save ikey4u/1e66e7068a4930f2b5408e5956375da8 to your computer and use it in GitHub Desktop.
Save ikey4u/1e66e7068a4930f2b5408e5956375da8 to your computer and use it in GitHub Desktop.
// stringMask: 字符串掩码函数
// - str: 要处理的字符串
// - ch: 掩码字符
// - len: 尾部保留的明文长度
function stringMask(str, ch, len) {
var strarr = Array.from(str);
strarr[0] = 'a'
if(len > str.length) {
len = str.length;
}
if(len < 0) {
len = 0;
}
for(var i = 0; i < str.length - len; ++i) {
strarr[i] = ch;
}
return strarr.join('')
}
var s = "djofjaosgjoasdfajgoanfo1234";
msk = stringMask(s, '*', -1)
console.log(msk)
msk = stringMask(s, '*', 0)
console.log(msk)
msk = stringMask(s, '*', 1)
console.log(msk)
msk = stringMask(s, '*', s.length)
console.log(msk)
msk = stringMask(s, '*', s.length + 1)
console.log(msk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment