refactor(domain): own sender display name on EmailAddress
Push the "Name <addr>" display-name parsing onto the EmailAddress VO (displayName field + label() = displayName ?? normalized) and delete the ad-hoc parseFromAddress helper in feed-generator. The feed builder now parses the from-address once via EmailAddress and reuses it for the site base URL, the [Sender] title prefix, and the entry author — one parser, on the type that owns the data. Co-Authored-By: Claude Opus 4.7 <[email protected]>
This commit is contained in:
1 parent
16029460bc
commit
757dd3a53f
3 files changed
+41
-18
No files matched your search
@@ -30,4 +30,20 @@ describe("EmailAddress", () => {
|
||||
"https://example.com/",
|
||||
);
|
||||
});
|
||||
|
||||
it("captures the display name verbatim from a display form", () => {
|
||||
const email = EmailAddress.parse("Alice B <[email protected]>")!;
|
||||
expect(email.displayName).toBe("Alice B");
|
||||
expect(email.label()).toBe("Alice B");
|
||||
});
|
||||
|
||||
it("has no display name for a bare address and labels by the address", () => {
|
||||
const email = EmailAddress.parse("[email protected]")!;
|
||||
expect(email.displayName).toBeUndefined();
|
||||
expect(email.label()).toBe("[email protected]");
|
||||
});
|
||||
|
||||
it("falls back to the address as the label when the display name is empty", () => {
|
||||
expect(EmailAddress.parse("<[email protected]>")?.label()).toBe("[email protected]");
|
||||
});
|
||||
});
|
||||
@@ -3,12 +3,16 @@ import { Domain } from "./domain";
|
||||
/**
|
||||
* A normalised email address. `parse` accepts a bare address (`[email protected]`) or a
|
||||
* display form (`Name <[email protected]>`), lowercasing the local part and normalising
|
||||
* the domain. Returns null when no plausible address can be found.
|
||||
* the domain. When the input carries a display name it is captured (verbatim,
|
||||
* not normalised — names are case-sensitive). Returns null when no plausible
|
||||
* address can be found.
|
||||
*/
|
||||
export class EmailAddress {
|
||||
private constructor(
|
||||
readonly normalized: string,
|
||||
readonly domain: Domain,
|
||||
/** The sender's display name from a `Name <addr>` input, if any. */
|
||||
readonly displayName?: string,
|
||||
) {}
|
||||
|
||||
static parse(raw: string): EmailAddress | null {
|
||||
@@ -17,7 +21,17 @@ export class EmailAddress {
|
||||
const domain = Domain.parse(match[2]);
|
||||
if (!domain) return null;
|
||||
const local = match[1].trim().toLowerCase();
|
||||
return new EmailAddress(`${local}@${domain.value}`, domain);
|
||||
const displayName =
|
||||
raw.match(/^\s*(.+?)\s*<[^>]+>\s*$/)?.[1].trim() || undefined;
|
||||
return new EmailAddress(`${local}@${domain.value}`, domain, displayName);
|
||||
}
|
||||
|
||||
/**
|
||||
* The best human-readable label for this sender: the display name when the
|
||||
* address came in `Name <addr>` form, else the normalised address.
|
||||
*/
|
||||
label(): string {
|
||||
return this.displayName ?? this.normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user