45 lines
1.0 KiB
TypeScript
45 lines
1.0 KiB
TypeScript
function maskAllButFirstChar(str: string) {
|
|
const first = str[0]
|
|
const rest = str.substring(1)
|
|
const restMasked = "*".repeat(rest.length)
|
|
|
|
return `${first}${restMasked}`
|
|
}
|
|
|
|
function maskAllButLastTwoChar(str: string) {
|
|
const lastTwo = str.slice(-2)
|
|
const rest = str.substring(0, str.length - 2)
|
|
const restMasked = "*".repeat(rest.length)
|
|
|
|
return `${restMasked}${lastTwo}`
|
|
}
|
|
|
|
export function email(str: string) {
|
|
const parts = str.split("@")
|
|
|
|
const aliasMasked = maskAllButFirstChar(parts[0])
|
|
|
|
if (parts[1]) {
|
|
const domainParts = parts[1].split(".")
|
|
if (domainParts.length > 1) {
|
|
const domainTLD = domainParts.pop()
|
|
const domainPartsMasked = domainParts
|
|
.map((domainPart, i) => {
|
|
return maskAllButFirstChar(domainPart)
|
|
})
|
|
.join(".")
|
|
return `${aliasMasked}@${domainPartsMasked}.${domainTLD}`
|
|
}
|
|
}
|
|
|
|
return maskAllButFirstChar(str)
|
|
}
|
|
|
|
export function phone(str: string) {
|
|
return maskAllButLastTwoChar(str)
|
|
}
|
|
|
|
export function text(str: string) {
|
|
return maskAllButFirstChar(str)
|
|
}
|