Skip to content

Commit

Permalink
Email Regex Improve (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed Dec 14, 2021
1 parent 8b55fbf commit 0a744af
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/email_checker/tools.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
defmodule EmailChecker.Tools do
@moduledoc false

@email_regex ~r/^(?<user>[^\s]+)@(?<domain>[^\s]+\.[^\s]+)$/
regex_file = Application.app_dir(:email_checker, "priv/email_regex")
@external_resource regex_file
@email_regex regex_file |> File.read!() |> Regex.compile!()

@spec domain_name(String.t()) :: String.t() | nil
def domain_name(email) do
case Regex.named_captures(email_regex(), email) do
case Regex.named_captures(@email_regex, email) do
%{"domain" => domain} ->
domain

Expand All @@ -14,9 +16,7 @@ defmodule EmailChecker.Tools do
end
end

def email_regex do
@email_regex
end
def email_regex, do: @email_regex

@spec lookup(String.t() | nil) :: String.t() | nil
def lookup(nil), do: nil
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule EmailChecker.Mixfile do

defp package do
[
files: ["lib", "mix.exs", "README*", "LICENSE*"],
files: ["lib", "mix.exs", "README*", "LICENSE*", "priv"],
maintainers: ["Kevin Disneur"],
licenses: ["MIT"],
links: %{
Expand Down
1 change: 1 addition & 0 deletions priv/email_regex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?<domain>(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))
15 changes: 8 additions & 7 deletions test/email_checker/check/format_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ defmodule EmailChecker.Check.FormatTest do
doctest EmailChecker.Check.Format

describe "valid?" do
test "a valid email format returns true" do
assert true == EmailChecker.Check.Format.valid?("user@domain.com")
assert true == EmailChecker.Check.Format.valid?("user+addition@domain.com")
assert true == EmailChecker.Check.Format.valid?("user.name+addition@domain.com")
for email <- ["user@domain.com", "user+addition@domain.com", "user.name+addition@domain.com"] do
test "#{email} format returns true" do
assert true == EmailChecker.Check.Format.valid?(unquote(email))
end
end

test "an invalid email format returns false" do
assert false == EmailChecker.Check.Format.valid?("user.domain.com")
assert false == EmailChecker.Check.Format.valid?("user name@domain.com")
for email <- ["user.domain.com", "test@gmail.."] do
test "#{email} format returns false" do
refute EmailChecker.Check.Format.valid?(unquote(email))
end
end
end
end

0 comments on commit 0a744af

Please sign in to comment.