Functional Weave
Code in Rust

auth.normalise-email@1.0.0

impl/python.py

725 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import Optional

from .validation_email import is_email  ← from validation.email ^1.0.0 · built alongside by fune

#: 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