vincent-utils
v0.0.6
Published
> 日常使用的一些封装,例如:utils, storage等
Downloads
9
Readme
vin-utils
日常使用的一些封装,例如:utils, storage等
Storage 封装
可以加密存储数据,支持多种存储方式,如:localStorage, sessionStorage, cookie等
手机号脱敏
/**
* @desc: 手机号脱敏
* @example: { middle= 136****4532, end= 136********, ... }
* @return: {String}
* @param phone string
* @param mode
*/
export function mobilePhoneAnonymization(phone: string, mode: string = 'middle'): string {
const mobile_number_relaxed = /^1[0-9]{10}$/
// let regex = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
if (!mobile_number_relaxed.test(phone)) {
console.error('[Filter Name is mobilePhoneAnonymization] Invalid cell mobile phone number')
return '-'
}
let options: {
[key: string]: {
reg: RegExp,
handler: (...args: string[]) => string
}
} = {
'middle': { // middle= 136****4532
reg: /(\d{3})(\d*)(\d{4})/g,
handler: function (p1: string, p2: string, p3: string) {
return `${p1}${p2.replace(/\d/g, '*')}${p3}`
}
},
'end': { // end= 136********
reg: /(\d{3})(\d*)/g,
handler: function (p1: string, p2: string) {
return `${p1}${p2.replace(/\d/g, '*')}`
},
}
}
const strategy = options[mode] ?? options.middle;
return phone.replace(strategy.reg, (...args: string[]) => {
return strategy.handler(...args.slice(1, -1));
});
}