Commit c19c7afa authored by rinpatch's avatar rinpatch

Refactor url?/2 to use opts

parent c8026cd1
Pipeline #13430 passed with stages
in 2 minutes and 4 seconds
......@@ -262,7 +262,7 @@ defmodule AutoLinker.Parser do
def check_and_link(buffer, opts, _user_acc) do
str = strip_parens(buffer)
if url?(str, opts[:scheme], opts[:validate_tld]) do
if url?(str, opts) do
case parse_link(str, opts) do
^buffer -> link_url(buffer, opts)
url -> String.replace(buffer, url, link_url(url, opts))
......@@ -315,16 +315,15 @@ defmodule AutoLinker.Parser do
end
# @doc false
def url?(buffer, scheme, validate_tld \\ true)
def url?(buffer, true, validate_tld) do
valid_url?(buffer) && Regex.match?(@match_scheme, buffer) &&
(!validate_tld or validate_tld == :no_scheme || valid_tld?(buffer))
end
def url?(buffer, _, validate_tld) do
valid_url?(buffer) && Regex.match?(@match_url, buffer) &&
(validate_tld == false || valid_tld?(buffer))
def url?(buffer, opts) do
if opts[:scheme] do
valid_url?(buffer) && Regex.match?(@match_scheme, buffer) &&
(!opts[:validate_tld] or opts[:validate_tld] == :no_scheme || valid_tld?(buffer))
else
valid_url?(buffer) && Regex.match?(@match_url, buffer) &&
(opts[:validate_tld] == false || valid_tld?(buffer))
end
end
def email?(buffer) do
......
......@@ -4,74 +4,74 @@ defmodule AutoLinker.ParserTest do
import AutoLinker.Parser
describe "url?/3" do
describe "url?/2" do
test "valid scheme true" do
valid_scheme_urls()
|> Enum.each(fn url ->
assert url?(url, true)
assert url?(url, scheme: true, validate_tld: true)
end)
end
test "invalid scheme true" do
invalid_scheme_urls()
|> Enum.each(fn url ->
refute url?(url, true)
refute url?(url, scheme: true, validate_tld: true)
end)
end
test "valid scheme false" do
valid_non_scheme_urls()
|> Enum.each(fn url ->
assert url?(url, false)
assert url?(url, scheme: false, validate_tld: true)
end)
end
test "invalid scheme false" do
invalid_non_scheme_urls()
|> Enum.each(fn url ->
refute url?(url, false)
refute url?(url, scheme: false, validate_tld: true)
end)
end
test "checks the tld for url with a scheme when validate_tld: true" do
custom_tld_scheme_urls()
|> Enum.each(fn url ->
refute url?(url, true, true)
refute url?(url, scheme: true, validate_tld: true)
end)
end
test "does not check the tld for url with a scheme when validate_tld: false" do
custom_tld_scheme_urls()
|> Enum.each(fn url ->
assert url?(url, true, false)
assert url?(url, scheme: true, validate_tld: false)
end)
end
test "does not check the tld for url with a scheme when validate_tld: :no_scheme" do
custom_tld_scheme_urls()
|> Enum.each(fn url ->
assert url?(url, true, :no_scheme)
assert url?(url, scheme: true, validate_tld: :no_scheme)
end)
end
test "checks the tld for url without a scheme when validate_tld: true" do
custom_tld_non_scheme_urls()
|> Enum.each(fn url ->
refute url?(url, false, true)
refute url?(url, scheme: false, validate_tld: true)
end)
end
test "checks the tld for url without a scheme when validate_tld: :no_scheme" do
custom_tld_non_scheme_urls()
|> Enum.each(fn url ->
refute url?(url, false, :no_scheme)
refute url?(url, scheme: false, validate_tld: :no_scheme)
end)
end
test "does not check the tld for url without a scheme when validate_tld: false" do
custom_tld_non_scheme_urls()
|> Enum.each(fn url ->
assert url?(url, false, false)
assert url?(url, scheme: false, validate_tld: false)
end)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment