interface PhoneNumberDisplayProps { phoneNumber: string; className?: string; } export const formatPhoneNumber = (value: string): string => { if (!value) return ''; // Remove all non-digit characters const cleaned = ('' + value).replace(/\D/g, ''); // Match a 10-digit phone number, with or without the +1 country code const match = cleaned.replace(/^1(?=\d{10}$)/, '').match(/^(\d{3})(\d{3})(\d{4})$/); if (match) { return `(${match[1]}) ${match[2]}-${match[3]}`; } return value; }; export const usePhoneNumber = (value: string): string => { return formatPhoneNumber(value); }; export const formatPhoneNumberForInput = (value: string): string => { if (!value) return ''; // Remove all non-digit characters const cleaned = ('' + value).replace(/\D/g, ''); // Format as XXX-XXX-XXXX for input display const match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/); if (match) { return `${match[1]}-${match[2]}-${match[3]}`; } return cleaned; }; export const isValidPhoneNumber = (value: string): boolean => { if (!value) return false; // Remove all non-digit characters const cleaned = ('' + value).replace(/\D/g, ''); // Check if it's a 10-digit number return /^\d{10}$/.test(cleaned); }; /** * Normalize a user-entered phone number to E.164 (e.g. "+15551234567"). * Cognito's standard `phone_number` attribute requires E.164, and E.164 is the * canonical form the SMS pipeline and dedup matcher both want. US-centric: a bare * 10-digit number is assumed +1. Returns null when the input can't be confidently * normalized, so callers can treat it as an invalid phone rather than store garbage. */ export const toE164 = (value: string): string | null => { if (!value) return null; const trimmed = ('' + value).trim(); // Already E.164 (leading + and a valid country/subscriber number). if (/^\+[1-9]\d{1,14}$/.test(trimmed)) return trimmed; const digits = trimmed.replace(/\D/g, ''); if (digits.length === 10) return `+1${digits}`; // US national number if (digits.length === 11 && digits.startsWith('1')) return `+${digits}`; // US with country code return null; }; export const PhoneNumberDisplay: React.FC = ({ phoneNumber, className }) => { const formattedPhoneNumber = formatPhoneNumber(phoneNumber); return ( {formattedPhoneNumber} ); };