in modules/util.py [0:0]
def normalize_phone_number(self, phone: str, default_country_code="1") -> str:
"""
Given a phone number in some format, +1(xxx)-xxx-xxxx or something similar, it will strip the phone number
to only the <country-code>xxxxxxxxxx format and return it.
Regex is to remove phone number extensions, e.g 800-555-5555 x555
Regex explanations: https://docs.python.org/3/library/re.html#regular-expression-syntax
...
Attributes
----------
phone : str
The phone number to be normalized
default_country_code: str
By default this is '1' for Canadian and US codes.
Returns
-------
str
The normalized version of the phone number in the <country code>xxxxxxxxxx format
"""
# 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)
ret_val = ""
# if the phone already contains the area code, ensure we only return the last 10 digits, otherwise a 10 length number is valid
if len(digits) > 10:
ret_val = digits[-10:]
elif len(digits) == 10:
ret_val = digits
else:
logging.warning("No valid phone number could be generated.")
return ""
# return with the country code and the normalized phone number
return default_country_code + ret_val