def normalize_regional_phone_numbers()

in modules/util.py [0:0]


    def normalize_regional_phone_numbers(self, phone: str, region: str) -> str:
        """
        Normalizes a phone number by separating the country prefix and verifying the rest of the number as an integer.
        This is used for localization (l10n) regional tests.
        Parameters:
        -----------
        phone : str
            The phone number to be normalized.
        region : str
            The region (country) code to determine the correct country prefix.
        Returns:
        --------
        str
            The normalized phone number in the format <country-code><number>.
        """

        # Country code mapping for different regions
        country_codes = {
            "US": "1",
            "CA": "1",
            "FR": "33",
            "DE": "49",
        }

        # Sub out anything that matches this regex statement with an empty string to get rid of extensions in generated phone numbers
        phone = re.sub(r"\s*(?:x|ext)\s*\d*$", "", phone, flags=re.IGNORECASE)
        # Sub out anything that is not a digit with the empty string to ensure the phone number is formatted with no spaces or special characters
        digits = re.sub(r"\D", "", phone)

        # Determine country code
        country_code = country_codes.get(
            region, "1"
        )  # Default to "1" (US/CA) if region is unknown
        local_number = digits

        # Check if phone already contains a valid country code
        for code in country_codes.values():
            if digits.startswith(code):
                country_code = code
                # Remove country code from local number
                local_number = digits[len(code) :]
                break

        # Handle leading zero in local numbers (France & Germany)
        if region in ["FR", "DE"] and local_number.startswith("0"):
            # Remove the leading zero
            local_number = local_number[1:]

        # Validate local number length
        if len(local_number) < 6:  # Too short to be valid
            logging.warning(f"Invalid phone number format: {phone}")
            return ""

        # Return formatted phone number with correct country code
        return f"{country_code}{local_number}"