Js脱敏工具

2023-12-26 16:32:13

tuoMin().phone("我的手机号是13800000000, 我家固话号是04100000000");

tuoMin().name("张三丰");

function tuoMin() {

    var object = new Object();

    object.phone = function(phone) {
        const phoneReg = /(\d{3})\d{4}(\d{4})/g;
        const landlineReg = /(\d{3,4})-(\d{7,8})/g;
        phone = phone.replace(phoneReg, (match, p1, p2) => {
            return p1 + '****' + p2;
        });
        phone = phone.replace(landlineReg, (match, p1, p2) => {
            return p1 + '-****' + p2;
        });
        return phone;
    }
 
    object.email = function(email) {
        var avg;
        var splitted;
        var email1;
        var email2;
        splitted = email.split('@');
        email1 = splitted[0];
        avg = email1.length / 2;
        email1 = email1.substring(0, email1.length - avg);
        email2 = splitted[1];
        return email1 + '***@' + email2;
    }
    object.name = function(name) {
        if (name.length == 2) {
            name = name.substring(0, 1) + '*';
            return name;
        } else if (name.length == 3) {
            name = name.substring(0, 1) + '*' + name.substring(2, 3);
            return name;
        } else if (name.length > 3) {
            name = name.substring(0, 1) + '*' + '*' + name.substring(3, name.length);
            return name;
        } else {
            return name;
        }
    }
    object.idcard = function(card) {
        const reg = /^(.{6})(?:\d+)(.{4})$/;
        const maskedIdCard = card.replace(reg, '$1******$2');
        return maskedIdCard;
    }
    object.address = function(value) {
        const addrReg = /(.{9})(.*)/;
        if (!value) {
            return "";
        }
        if (addrReg.test(value)) {
            let text1 = RegExp.$1;
            let text2 = RegExp.$2.replace(/./g, "*");
            return text1 + text2;
        }
        return value;
    }
    object.custom = function(str, begin, end) {
        if (!str && (begin + end) >= str.length) {
            return "";
        }
        let leftStr = str.substring(0, begin);
        let rightStr = str.substring(str.length - end, str.length);
        let strCon = ''
        for (let i = 0; i < str.length - end - begin; i++) {
            strCon += '*';
        }
        return leftStr + strCon + rightStr;
    }
    return object;
}

文章来源:https://blog.csdn.net/dongyan3595/article/details/135223645
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。