from typing import Optional from .validation_email import is_email #: ASCII whitespace only; str.strip() with no argument also strips no-break #: spaces and other Unicode spaces. _ASCII_SPACE = " \t\n\r\f\v" def normalise_email(value: str) -> Optional[str]: """The stored form of an email address: trimmed, domain lower-cased, local part as typed; None when the trimmed text is not a plausible address.""" if not isinstance(value, str): return None trimmed = value.strip(_ASCII_SPACE) if not is_email(trimmed): return None at = trimmed.index("@") domain = "".join(chr(ord(ch) + 32) if "A" <= ch <= "Z" else ch for ch in trimmed[at + 1 :]) return trimmed[: at + 1] + domain