마스킹처리 - 2 (고객정보)

2019. 11. 14. 16:01개발나들이/front

다른 방법도 있는 거 같아 정리해봐요


[소스]


var maskingUtil = (function() {
return {
maskingSocialNumber: function(value)
{
if (value)
{
var regex = /(\d{6})([\s|-]?)(\d{7})/gi;
var result = value.replace(regex, '$1$2*******');
return result;
}
},

maskingDriverLicense: function(value)
{
if (value)
{
var regex = /(\d{2}|\D{2})([\s|-]?)(\d{2})([\s|-]?)(\d{6})([\s|-]?)(\d{2})/gi;
var result = value.replace(regex, '$1$2$3$4******$6$7');
return result;
}
},

maskingPassport: function(value)
{
if (value)
{
var regex = /(\D{1})(\d{8})/gi;
var result = value.replace(regex, '$1********');
return result;
}
},

maskingBankingAccount: function(value)
{

if (value)
{
var regex = /(\d{3})([\s|-]?)(\d{6})([\s|-]?)(\d{2})([\s|-]?)(\d{3})/gi;
var result = value.replace(regex, '$1$2******$4$5$6$7');
return result;
}
},



maskingCardNumber: function(value)
{
if (value)
{
var regex = /(\d{4})([\s|-]?)(\d{4})([\s|-]?)(\d{4})([\s|-]?)(\d{4})/gi;
var result = value.replace(regex, '$1$2****$4****$6$7');
return result;
}
},

maskingMobileNumber: function(value)
{
if (value)
{
var regex = /(\d{3})([\s|-]?)(\d{2})(\d{2})([\s|-]?)(\d{2})(\d{2})/gi;
var result = value.replace(regex, '$1$2$3**$5$6**');
return result;
}
},

maskingTelNumber: function(value)
{
if (value)
{
var result = '';
var regex = /(\d{2,4})([\s|-]?)(\d{1,2})(\d{2})([\s|-]?)(\d{2})(\d{2})/gi; // 3자리 패턴
if (regex.test(value))
{
result = value.replace(regex, '$1$2$3**$5$6**');
}
else
{
regex = /(\d{1,2})(\d{2})([\s|-]?)(\d{2})(\d{2})/gi; // 2자리 패턴
if (regex.test(value))
{
result = value.replace(regex, '$1**$3$4**');
}
}
return result;
}
},

maskingEmail: function(value)
{
if (value && value.indexOf('@') > -1)
{
var result = '';
const len = value.split('@')[0].length - 3;
result = value.replace(new RegExp('.(?=.{0,' + len + '}@)', 'g'), '*');
return result;
}
}
}
})();

function test(title, testCode) {
try {
console.log(title);
testCode();
} catch (error) {
console.error(error);
}
}

function expect(result) {
return {
toBe: function(expected) {
if (result !== expected) {
throw new Error(result + ' is not equal to ' + expected);
}
}
}
}


var value = '';
var expectedValue = '';

value = '000000-0000000';
expectedValue = '000000-*******';
test('[주민번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingSocialNumber(value)).toBe(expectedValue);
});

value = '000000 0000000';
expectedValue = '000000 *******';

test('[주민번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingSocialNumber(value)).toBe(expectedValue);
});

value = '0000000000000';
expectedValue = '000000*******';

test('[주민번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingSocialNumber(value)).toBe(expectedValue);
});

value = '00-00-000000-00';
expectedValue = '00-00-******-00';

test('[운전면허번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingDriverLicense(value)).toBe(expectedValue);
});

value = 'XX-00-000000-00';
expectedValue = 'XX-00-******-00';

test('[운전면허번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingDriverLicense(value)).toBe(expectedValue);
});


value = '00 00 000000 00';
expectedValue = '00 00 ****** 00';
test('[운전면허번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingDriverLicense(value)).toBe(expectedValue);
});

value = 'XX 00 000000 00';
expectedValue = 'XX 00 ****** 00';

test('[운전면허번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingDriverLicense(value)).toBe(expectedValue);
});


value = '000000000000';
expectedValue = '0000******00';

test('[운전면허번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingDriverLicense(value)).toBe(expectedValue);
});


value = 'XX0000000000';
expectedValue = 'XX00******00';

test('[운전면허번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingDriverLicense(value)).toBe(expectedValue);
});


value = 'X00000000';
expectedValue = 'X********';

test('[여권번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingPassport(value)).toBe(expectedValue);
});


value = '000-000000-00-000';
expectedValue = '000-******-00-000';

test('[계좌번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingBankingAccount(value)).toBe(expectedValue);
});


value = '000 000000 00 000';
expectedValue = '000 ****** 00 000';

test('[계좌번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingBankingAccount(value)).toBe(expectedValue);
});


value = '00000000000000';
expectedValue = '000******00000';

test('[계좌번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingBankingAccount(value)).toBe(expectedValue);
});


value = '0000-0000-0000-0000';
expectedValue = '0000-****-****-0000';
test('[카드번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingCardNumber(value)).toBe(expectedValue);
});

value = '0000 0000 0000 0000';
expectedValue = '0000 **** **** 0000';

test('[카드번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingCardNumber(value)).toBe(expectedValue);
});

value = '0000000000000000';
expectedValue = '0000********0000';

test('[카드번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingCardNumber(value)).toBe(expectedValue);
});



value = '000-0000-0000';
expectedValue = '000-00**-00**';

test('[핸드폰번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingMobileNumber(value)).toBe(expectedValue);
});


value = '000 0000 0000';
expectedValue = '000 00** 00**';

test('[핸드폰번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingMobileNumber(value)).toBe(expectedValue);
});


value = '00000000000';
expectedValue = '00000**00**';

test('[핸드폰번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingMobileNumber(value)).toBe(expectedValue);
});



value = '00-000-0000';
expectedValue = '00-0**-00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});

value = '000-000-0000';
expectedValue = '000-0**-00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '00-000-0000';
expectedValue = '00-0**-00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '000-0000-0000';
expectedValue = '000-00**-00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '000-0000';
expectedValue = '0**-00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '0000-0000';
expectedValue = '00**-00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '00 000 0000';
expectedValue = '00 0** 00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '000 000 0000';
expectedValue = '000 0** 00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '00 000 0000';
expectedValue = '00 0** 00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '000 0000 0000';
expectedValue = '000 00** 00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '000 0000';
expectedValue = '0** 00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '0000 0000';
expectedValue = '00** 00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});



value = '0000000';
expectedValue = '0**00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = '00000000';
expectedValue = '00**00**';

test('[전화번호] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingTelNumber(value)).toBe(expectedValue);
});


value = 'abcd1234@ibk.co.kr';
expectedValue = 'ab******@ibk.co.kr';

test('[이메일] ' + value + ' ==> ' + expectedValue, function() {
expect(maskingUtil.maskingEmail(value)).toBe(expectedValue);
});


[실행결과]