Returns a string with a specific number of characters masked by an asterisk or custom character. You can also choose between a leading, trailing, or middle filler (default: trailing) and can choose to ignore certain characters when masking.
function mask(
str: string,
config?: {
maskWith?: string
maskLength?: number
style?: "leading" | "trailing" | "middle"
ignore?: string[]
}
)
// Example:
mask("Password") //=> "********"
mask("Password", { maskWith: "." }) //=> "........"
mask("Password", { maskWith: "@", maskLength: 4, style: "trailing" }) //=> "Pass@@@@"
mask("Password", { maskWith: "@", maskLength: 4, style: "leading" }) //=> "@@@@word"
mask("Password", { maskLength: 4, style: "middle" }) //=> "Pa****rd"
const secretPhrase = "This is a secret phrase."
mask(secretPhrase) //=> "************************"
mask(secretPhrase, { ignore: [" "] }) //=> "**** ** * ****** *******"