network-resurrector/src/utils/obfuscateStrings.js

25 lines
744 B
JavaScript

const obfuscateForChars = (text, placeholder = "*") => {
const firstChar = text.substring(0, 1);
const lastChar = text.substring(text.length - 1);
const middleChars = text
.substring(1, text.length - 1)
.replace(/[a-zA-Z0-9]/g, placeholder);
return firstChar + middleChars + lastChar;
};
const obfuscate = (text, placeholder = "*") => {
if (text.length <= 2) return text;
if (text.length <= 4) {
return obfuscateForChars(text);
}
const firstTwoChars = text.substring(0, 2);
const lastTwoChars = text.substring(text.length - 2);
const middleChars = text
.substring(2, text.length - 2)
.replace(/[a-zA-Z0-9]/g, placeholder);
return firstTwoChars + middleChars + lastTwoChars;
};
export { obfuscate };