Mask String
Masks a portion of a string for privacy while allowing specified characters at the beginning and/or end to remain visible.
maskString
The maskString function masks a portion of a string for privacy, replacing characters with a mask character while preserving a visible prefix and suffix.
Function Signature
function maskString(input: string, options?: MaskOptions): stringParameters
input(string): The string to mask.options(MaskOptions, optional): Configuration options:start(number): Number of characters to keep visible at the start. Defaults to1.end(number): Number of characters to keep visible at the end. Defaults to1.maskCharacter(string): Character used to mask the string. Defaults to'*'.trim(boolean): Whether to trim all whitespace characters before masking. Defaults tofalse.
Types
MaskOptions
interface MaskOptions {
start?: number;
end?: number;
maskCharacter?: string;
trim?: boolean;
}Return Value
string: The masked string. If the string is too short to apply the start and end retention options, it is fully masked.
Note
- The input string is trimmed of all (not just leading and trailing) whitespaces before masking when
'trim'option istrue. - Ideal for obfuscating sensitive fields like emails, phone numbers, or tokens.
Example Usage
playground.ts
Last updated: Sun, Jun 28, 2026 08:49:40AM (UTC)
