마스킹처리 - 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);
});
[실행결과]
'개발나들이 > front' 카테고리의 다른 글
[React] 리액트 강좌 (0) | 2021.06.21 |
---|---|
[React] PropTypes 종류 정리 (0) | 2020.01.14 |
마스킹처리 - 1 (전화번호, 이메일, 차량번호, 사업가번호 등등) (0) | 2019.10.30 |
XSS / CSRF 공격 가능성 막기 (스크립트 막기) (0) | 2019.10.07 |
[javascript] url 파싱 (0) | 2019.09.03 |