Initial Commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import type { MaskRule } from "../types/mask-types.js";
|
||||
|
||||
/**
|
||||
* Phone-number masking strategy.
|
||||
*
|
||||
* Keeps the last 4 digits of a phone number and replaces all preceding digit
|
||||
* positions with `*`, while preserving non-digit separator characters
|
||||
* (hyphens, spaces, parentheses, dots, `+`) so the output remains formatted
|
||||
* consistently with the input.
|
||||
*
|
||||
* Example: `+1-555-123-4567` → `*-***-***-4567`
|
||||
*
|
||||
* @param value - The original phone number string.
|
||||
* @param rule - The `MaskRule` (not consumed by this strategy).
|
||||
* @returns The masked phone number.
|
||||
*/
|
||||
export function maskPhone(value: string, rule: MaskRule): string {
|
||||
void rule;
|
||||
// Extract only the digit characters to determine the last-4 boundary.
|
||||
const digits = value.replace(/\D/g, "");
|
||||
if (digits.length === 0) return value;
|
||||
|
||||
const keepFrom = Math.max(0, digits.length - 4);
|
||||
// Walk the original string, replacing digits that are NOT in the last 4.
|
||||
let digitsSeen = 0;
|
||||
let result = "";
|
||||
for (const ch of value) {
|
||||
if (/\d/.test(ch)) {
|
||||
if (digitsSeen < keepFrom) {
|
||||
result += "*";
|
||||
} else {
|
||||
result += ch;
|
||||
}
|
||||
digitsSeen++;
|
||||
} else {
|
||||
result += ch;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user