Skip to content

redact ​

Censors sensitive string content using a masking character (such as *) while optionally preserving visible characters at the start and end.

Signature ​

typescript
function redact(value: string | null | undefined): string;
function redact(value: string | null | undefined, replacement: string): string;
function redact(value: string | null | undefined, visibleStart: number): string;
function redact(value: string | null | undefined, options: RedactOptions): string;

Parameters ​

ParameterTypeDescription
valuestring | null | undefinedThe string to redact.
optionsstring | number | RedactOptionsReplacement string, visible start character count, or a configuration options object.

Options ​

OptionTypeDefaultDescription
visibleStartnumber0Characters to keep visible at the start.
visibleEndnumber0Characters to keep visible at the end.
replacementstring"*"Replacement character or string for masked positions.
modeCharacterSegmentMode"utf16"Determines how characters are counted and segmented ("utf16", "codePoint", or "grapheme").

Returns ​

string — The redacted string, or an empty string "" when value is empty, null, or undefined.

Examples ​

typescript
import { redact } from "@rcpthongta/string-utils";

// Full redaction (default)
redact("0812345678");
// => "**********"

// Keep start characters visible
redact("0812345678", 2);
// => "08********"

// Keep both start and end characters visible
redact("0812345678", { visibleStart: 3, visibleEnd: 2 });
// => "081*****78"

// Custom replacement character
redact("1234567890", { visibleStart: 4, visibleEnd: 4, replacement: "#" });
// => "1234##7890"

redact("");
// => ""

redact(null);
// => ""

redact(undefined);
// => ""

Released under the MIT License.