impl/typescript.ts
1,099 bytes · the TypeScript 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.
import { isEmail } from "./validation_email.ts"; ← from validation.email ^1.0.0 · built alongside by fune
/** ASCII whitespace only: String.prototype.trim also strips no-break spaces and other Unicode spaces, which Python and Rust would each treat differently. */
function isAsciiSpace(ch: string): boolean {
return ch === " " || ch === "\t" || ch === "\n" || ch === "\r" || ch === "\f" || ch === "\v";
}
/**
* The stored form of an email address: trimmed, domain lower-cased, local
* part as typed; null when the trimmed text is not a plausible address.
*/
export function normaliseEmail(value: string): string | null {
if (typeof value !== "string") return null;
let start = 0;
let end = value.length;
while (start < end && isAsciiSpace(value[start])) start++;
while (end > start && isAsciiSpace(value[end - 1])) end--;
const trimmed = value.slice(start, end);
if (!isEmail(trimmed)) return null;
const at = trimmed.indexOf("@");
let domain = "";
for (const ch of trimmed.slice(at + 1)) {
domain += ch >= "A" && ch <= "Z" ? String.fromCharCode(ch.charCodeAt(0) + 32) : ch;
}
return trimmed.slice(0, at + 1) + domain;
}