Repository: fterh/heimdall (ts)

Issue:
Email without sender name should not enclose sender email in brackets

Pull request description:
Improve how sender's name and email address are represented
Closes #30.

Patch:
diff --git a/lib/forwardInbound.ts b/lib/forwardInbound.ts
index 9d59083..0924877 100644
--- a/lib/forwardInbound.ts
+++ b/lib/forwardInbound.ts
@@ -6,6 +6,16 @@ import repackageReceivedAttachments from "./repackageReceivedAttachments";
 import sendEmail from "./sendEmail";
 import senderAddressEncodeDecode from "./senderAddressEncodeDecode";
 
+export const representNameAndEmailAddress = (
+  name: string,
+  emailAddress: string
+): string => {
+  if (name === "") {
+    return emailAddress;
+  }
+  return `${name} [${emailAddress}]`;
+};
+
 /**
  * Generates forwarded email's "from" header information
  * that encapsualtes original sender's name and email address.
@@ -30,7 +40,7 @@ export const generateFromHeader = (
   }
 
   return {
-    name: `${replyToName} [${replyToEmailAddress}]`,
+    name: representNameAndEmailAddress(replyToName, replyToEmailAddress),
     address: senderAddressEncodeDecode.encodeEmailAddress(
       aliasValue,
       replyToEmailAddress


Test patch:
diff --git a/tests/lib/forwardInbound.test.ts b/tests/lib/forwardInbound.test.ts
index a46e6dd..19b5a14 100644
--- a/tests/lib/forwardInbound.test.ts
+++ b/tests/lib/forwardInbound.test.ts
@@ -1,5 +1,8 @@
 import { email as myEmail, operationalDomain } from "../../lib/env";
-import forwardInbound, { generateFromHeader } from "../../lib/forwardInbound";
+import forwardInbound, {
+  generateFromHeader,
+  representNameAndEmailAddress
+} from "../../lib/forwardInbound";
 // @ts-ignore: We're using Jest's ES6 class mocks (https://jestjs.io/docs/en/es6-class-mocks)
 import { didReceiveEmailSpy } from "../../lib/models/Alias";
 import { INVALID_ALIAS } from "../../lib/models/__mocks__/Alias";
@@ -162,3 +165,9 @@ it("should not throw if the alias does not exist", async () => {
 
   await expect(res).resolves.toBeUndefined();
 });
+
+it("should not bracket the email address if there is no name", () => {
+  expect(representNameAndEmailAddress("", "user@domain.com")).toBe(
+    "user@domain.com"
+  );
+});