- ensure at least 40% of the characters are masked for secrets of length 4 or more - secrets with length <= 6 now show first and last char with mask - secrets with length <= 12 now show first two and last two chars - longer secrets show 3 prefix and 4 suffix
23 lines
665 B
TypeScript
23 lines
665 B
TypeScript
export function maskedSecretPlaceholder(value: unknown, fallback = ""): string {
|
|
const secret = typeof value === "string" ? value.trim() : ""
|
|
if (!secret) {
|
|
return fallback
|
|
}
|
|
|
|
// ensure at least 40% of the characters are masked for secrets of length 4 or more
|
|
if (secret.length <= 6) {
|
|
const first = secret[0]
|
|
const last = secret[secret.length - 1]
|
|
return `${first}***${last}`
|
|
}
|
|
|
|
if (secret.length <= 12) {
|
|
const firstTwo = secret.slice(0, 2)
|
|
const lastTwo = secret.slice(-2)
|
|
return `${firstTwo}****${lastTwo}`
|
|
}
|
|
|
|
const prefix = secret.slice(0, 3)
|
|
const suffix = secret.slice(-4)
|
|
return `${prefix}*****${suffix}`
|
|
}
|