Username desensitization
Two words, then desensitize the second word
Three words, then desensitize the second word
If there are more than three characters, the second and third characters will be desensitized.
// 用户名脱敏
export const treatName = function (str) {
if (str.length == 2) {
return str.substring(0, 1) + '*'
} else if (str.length == 3) {
return str.substring(0, 1) + "*" + str.substring(2, 3)
} else if (str.length > 3) {
return str.substring(0, 1) + "*" + '*' + str.substring(3, str.length)
}
return ''
}
Limit the number of bytes entered
The code example scenario is vue3
<input @input="handleInput" v-model="inputValue"/>
const inputValue = ref('')
function getStr(str, num){
let len = 0;
const chinese = /[^\x00-\xff]/ig;
for(let i=0;i<str.length;i++){
if(str.charAt(i).match(chinese)){
len +=2;
}else{
len +=1;
}
if(len > num){
return str.slice(0,i)
}
}
return str
}
function handleInput(){
inputValue.value = getStr(inputValue.value, 20)
}
Additional words
In the warehouse , it also provides a summary of many common front-end requirements and implementations. Guests are welcome to take a look~
If this note can help you, please help to highlight it on githubstar
, thank you!