feat: add jest and initial tests for masking values
This commit is contained in:
committed by
Christel Westerberg
parent
d84efcbb0f
commit
d28cbf10c7
+36
-7
@@ -1,15 +1,44 @@
|
||||
const emailRegex =
|
||||
/(?<=.)[^@\n](?=[^@\n]*?@)|(?:(?<=@.+)|(?!^)\G(?=[^@\n]*$)).(?=.*\.)/gm
|
||||
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) {
|
||||
return str.replace(emailRegex, "*")
|
||||
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)
|
||||
}
|
||||
|
||||
const phoneRegex = /.(?!.{0,1}$)/gm
|
||||
export function phone(str: string) {
|
||||
return str.replace(phoneRegex, "*")
|
||||
return maskAllButLastTwoChar(str)
|
||||
}
|
||||
|
||||
const textRegex = /(?!^)./gm
|
||||
export function text(str: string) {
|
||||
return str.replace(textRegex, "*")
|
||||
return maskAllButFirstChar(str)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user