Skip to content

Fix extract email#510

Merged
elie222 merged 2 commits intomainfrom
fix/extract-email-address
Jun 18, 2025
Merged

Fix extract email#510
elie222 merged 2 commits intomainfrom
fix/extract-email-address

Conversation

@elie222
Copy link
Copy Markdown
Owner

@elie222 elie222 commented Jun 18, 2025

Summary by CodeRabbit

  • New Features
    • Improved extraction and validation of email addresses, supporting a wider range of formats and edge cases.
  • Bug Fixes
    • Enhanced handling of email addresses with hyphens, underscores, numeric characters, and complex patterns.
  • Tests
    • Added extensive new test cases to verify correct extraction of diverse and complex email formats.
  • Chores
    • Updated version to v1.6.0.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 18, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
inbox-zero ✅ Ready (Inspect) Visit Preview Jun 18, 2025 1:23pm

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 18, 2025

Walkthrough

The changes refactor the extractEmailAddress function to improve email extraction and validation, introducing schema-based checks using Zod and a new helper function. The test suite is expanded with diverse cases for hyphens and special characters. The project version is incremented from v1.5.7 to v1.6.0.

Changes

File(s) Change Summary
apps/web/utils/email.ts Refactored extractEmailAddress to use improved extraction logic and Zod-based validation; added isValidEmail helper.
apps/web/utils/email.test.ts Added extensive new test cases for emails with hyphens, underscores, numbers, plus addressing, and edge cases.
version.txt Updated version from v1.5.7 to v1.6.0.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant EmailUtils

    Caller->>EmailUtils: extractEmailAddress(input)
    alt Input contains angle-bracketed email at end
        EmailUtils->>EmailUtils: isValidEmail(candidate)
        alt Valid
            EmailUtils-->>Caller: Return extracted email
        else Invalid
            EmailUtils->>EmailUtils: Check if trimmed input is valid email
            alt Valid
                EmailUtils-->>Caller: Return input
            else Invalid
                EmailUtils->>EmailUtils: Find all email-like substrings
                EmailUtils->>EmailUtils: isValidEmail(each candidate)
                alt Found valid
                    EmailUtils-->>Caller: Return first valid email
                else None valid
                    EmailUtils-->>Caller: Return empty string
                end
            end
        end
    end
Loading

Poem

A hop, a skip, through emails I go,
With hyphens and pluses, in lines that flow.
Zod helps me check, with schema so neat,
Extracting addresses—no easy feat!
Version hops up, the tests now abound,
A rabbit’s delight in each bug I have found.
🐇📧

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

apps/web/utils/email.test.ts

Oops! Something went wrong! :(

ESLint: 9.28.0

ESLint couldn't find an eslint.config.(js|mjs|cjs) file.

From ESLint v9.0.0, the default configuration file is now eslint.config.js.
If you are using a .eslintrc.* file, please follow the migration guide
to update your configuration file to the new format:

https://eslint.org/docs/latest/use/configure/migration-guide

If you still have problems after following the migration guide, please stop by
https://eslint.org/chat/help to chat with the team.

apps/web/utils/email.ts

Oops! Something went wrong! :(

ESLint: 9.28.0

ESLint couldn't find an eslint.config.(js|mjs|cjs) file.

From ESLint v9.0.0, the default configuration file is now eslint.config.js.
If you are using a .eslintrc.* file, please follow the migration guide
to update your configuration file to the new format:

https://eslint.org/docs/latest/use/configure/migration-guide

If you still have problems after following the migration guide, please stop by
https://eslint.org/chat/help to chat with the team.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
apps/web/utils/email.ts (1)

34-45: Regex under-matches international addresses & emails ending with punctuation

/\b[^\s<>]+@[^\s<>]+\.[^\s<>]+\b/ excludes:

  • IDN domains (用户@例子.公司.cn) because \w-like classes don’t include Unicode by default.
  • Addresses that appear at sentence end (hello@example.com.) because the trailing . becomes part of the match and subsequently fails isValidEmail.

Consider relaxing the pattern and rely on isValidEmail for the heavy lifting:

-const emailPattern = /\b[^\s<>]+@[^\s<>]+\.[^\s<>]+\b/gu;
+// Allow almost anything up to the next whitespace / angle-bracket.
+// Unicode flag (`u`) enables IDN capture; we’ll filter with isValidEmail().
+const emailPattern = /[^\s<>]+@[^\s<>]+/gu;
apps/web/utils/email.test.ts (1)

141-163: Nice coverage, but consider adding a negative test

You added a wide range of complex addresses 👍. Adding a deliberate invalid case with similar punctuation (e.g. "user-@example..com") will guard against future regex regressions that accidentally over-match.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 72781e3 and b3d73d0.

📒 Files selected for processing (3)
  • apps/web/utils/email.test.ts (1 hunks)
  • apps/web/utils/email.ts (2 hunks)
  • version.txt (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (3)
version.txt (1)

1-1: Version bump looks correct but please tag the repository.

The change to v1.6.0 follows SemVer for a feature-level update.
Don’t forget to create a matching Git tag / release note before merging so downstream tooling can pick up the new version.

apps/web/utils/email.ts (1)

2-2: Verify zod is listed as a dependency

import { z } from "zod"; adds a runtime dependency. Ensure the workspace root or the apps/web package.json declares "zod": "^3.x" (or desired range); otherwise consumers will face a runtime module-not-found error.

apps/web/utils/email.test.ts (1)

88-121: A couple of “hyphen edge” test cases are actually invalid per RFC

"-test@example.com" and "test-@example.com" violate RFC 5322 (local part cannot start or end with ‘.’ but may with ‘-’; however many validators – including Zod’s – reject trailing hyphens).
If the objective is to accept what Zod accepts, keep them; otherwise mark them as invalid and expect "" to avoid false positives.

Comment thread apps/web/utils/email.ts
Comment on lines +49 to 52
// Use Zod's built-in email validation
function isValidEmail(email: string): boolean {
return z.string().email().safeParse(email).success;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Cache the Zod schema to avoid re-allocating on every call

z.string().email() allocates a new schema object each invocation, which is unnecessary and can become hot in tight loops.

-// Use Zod's built-in email validation
-function isValidEmail(email: string): boolean {
-  return z.string().email().safeParse(email).success;
-}
+const emailSchema = z.string().email();
+
+function isValidEmail(email: string): boolean {
+  return emailSchema.safeParse(email).success;
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Use Zod's built-in email validation
function isValidEmail(email: string): boolean {
return z.string().email().safeParse(email).success;
}
const emailSchema = z.string().email();
function isValidEmail(email: string): boolean {
return emailSchema.safeParse(email).success;
}
🤖 Prompt for AI Agents
In apps/web/utils/email.ts around lines 49 to 52, the isValidEmail function
creates a new Zod schema on every call, causing unnecessary allocations. To fix
this, define the Zod email schema once outside the function as a constant, then
reuse this cached schema inside isValidEmail to perform the validation. This
avoids repeated schema creation and improves performance.

Comment thread apps/web/utils/email.ts
Comment on lines +20 to 33
// Try to extract from angle brackets first
const bracketMatch = email.match(/<([^<>]+)>$/);
if (bracketMatch) {
const candidate = bracketMatch[1].trim();
if (isValidEmail(candidate)) {
return candidate;
}
}

// If no brackets or invalid email in brackets, try the whole string
if (isValidEmail(email.trim())) {
return email.trim();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Trim the input once to avoid bracket-match edge cases and double work

/<([^<>]+)>$/ only matches when the closing > is the last non-newline character.
Strings such as "John <john@example.com> " (trailing space) or " John <john@example.com>" (leading space) will miss the fast-path even though they are perfectly valid.

-export function extractEmailAddress(email: string): string {
-  if (!email) return "";
+export function extractEmailAddress(email: string): string {
+  if (!email) return "";
+  const trimmed = email.trim();
 
-  // Try to extract from angle brackets first
-  const bracketMatch = email.match(/<([^<>]+)>$/);
+  // Try to extract from angle brackets first
+  const bracketMatch = trimmed.match(/<([^<>]+)>\s*$/);

Besides tightening correctness, this avoids calling trim() repeatedly further down the function.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Try to extract from angle brackets first
const bracketMatch = email.match(/<([^<>]+)>$/);
if (bracketMatch) {
const candidate = bracketMatch[1].trim();
if (isValidEmail(candidate)) {
return candidate;
}
}
// If no brackets or invalid email in brackets, try the whole string
if (isValidEmail(email.trim())) {
return email.trim();
}
export function extractEmailAddress(email: string): string {
if (!email) return "";
const trimmed = email.trim();
// Try to extract from angle brackets first
const bracketMatch = trimmed.match(/<([^<>]+)>\s*$/);
if (bracketMatch) {
const candidate = bracketMatch[1].trim();
if (isValidEmail(candidate)) {
return candidate;
}
}
// If no brackets or invalid email in brackets, try the whole string
if (isValidEmail(email.trim())) {
return email.trim();
}
// …rest of function…
}
🤖 Prompt for AI Agents
In apps/web/utils/email.ts around lines 20 to 33, the email input should be
trimmed once at the start to handle cases with leading or trailing spaces that
cause the regex /<([^<>]+)>$/ to miss valid emails inside angle brackets. Trim
the email string once before applying the regex and subsequent validation to
improve correctness and avoid multiple trim calls.

@elie222 elie222 merged commit 7da3124 into main Jun 18, 2025
10 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Dec 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant