Skip to content

Commit 0ebc938

Browse files
authored
Merge pull request #2899 from boterop/16-elixir
#16 - elixir
2 parents 2ba0517 + 8e843c8 commit 0ebc938

File tree

1 file changed

+117
-0
lines changed
  • Roadmap/16 - EXPRESIONES REGULARES/elixir

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
defmodule Boterop.Regex do
2+
@spec get_numbers(text :: String.t()) :: list(String.t()) | nil
3+
def get_numbers(text), do: Regex.scan(~r/\d+/, text)
4+
5+
@spec is_a_valid_email?(email :: String.t()) :: boolean()
6+
def is_a_valid_email?(email),
7+
do: Regex.match?(~r/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/, email)
8+
9+
@spec is_a_valid_phone_number?(phone_number :: String.t()) :: boolean()
10+
def is_a_valid_phone_number?(phone_number),
11+
do:
12+
Regex.match?(~r/^\+?\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}$/, phone_number)
13+
14+
@spec is_a_valid_url?(url :: String.t()) :: boolean()
15+
def is_a_valid_url?(url),
16+
do: Regex.match?(~r/https?:\/\/[^\s\/$.?#]+\.(?:[a-z]{2,})+(?:\/[^\s$.?#]+)*/, url)
17+
end
18+
19+
ExUnit.start()
20+
21+
defmodule Boterop.RegexTest do
22+
use ExUnit.Case
23+
24+
test "get numbers from text" do
25+
expected_result = "00100505"
26+
27+
"S0l0 qu1er0 l05 numer05"
28+
|> Boterop.Regex.get_numbers()
29+
|> Enum.join()
30+
|> Kernel.==(expected_result)
31+
|> assert
32+
end
33+
34+
describe "validating email" do
35+
test "with a valid email" do
36+
"mail@domain.com"
37+
|> Boterop.Regex.is_a_valid_email?()
38+
|> assert
39+
end
40+
41+
test "with an invalid tld" do
42+
"mail@domain.c"
43+
|> Boterop.Regex.is_a_valid_email?()
44+
|> refute
45+
end
46+
47+
test "with an invalid structure" do
48+
"mail.domain@com"
49+
|> Boterop.Regex.is_a_valid_email?()
50+
|> refute
51+
end
52+
end
53+
54+
describe "validating phone number" do
55+
test "with a valid country code" do
56+
"+573015201917"
57+
|> Boterop.Regex.is_a_valid_phone_number?()
58+
|> assert
59+
end
60+
61+
test "with a valid separator" do
62+
"301 520 3261"
63+
|> Boterop.Regex.is_a_valid_phone_number?()
64+
|> assert
65+
end
66+
67+
test "with an invalid number" do
68+
"911"
69+
|> Boterop.Regex.is_a_valid_phone_number?()
70+
|> refute
71+
end
72+
73+
test "with an invalid character" do
74+
"+5730i5203261"
75+
|> Boterop.Regex.is_a_valid_phone_number?()
76+
|> refute
77+
end
78+
end
79+
80+
describe "validating url" do
81+
test "with a valid url" do
82+
"http://www.boterop.io"
83+
|> Boterop.Regex.is_a_valid_url?()
84+
|> assert
85+
end
86+
87+
test "without subdomain" do
88+
"http://boterop.io"
89+
|> Boterop.Regex.is_a_valid_url?()
90+
|> assert
91+
end
92+
93+
test "with a valid https protocol" do
94+
"https://www.boterop.io"
95+
|> Boterop.Regex.is_a_valid_url?()
96+
|> assert
97+
end
98+
99+
test "with an invalid protocol" do
100+
"invalid://www.boterop.io"
101+
|> Boterop.Regex.is_a_valid_url?()
102+
|> refute
103+
end
104+
105+
test "with an invalid domain" do
106+
"https://boterop"
107+
|> Boterop.Regex.is_a_valid_url?()
108+
|> refute
109+
end
110+
111+
test "with an invalid tdl" do
112+
"https://boterop.1"
113+
|> Boterop.Regex.is_a_valid_url?()
114+
|> refute
115+
end
116+
end
117+
end

0 commit comments

Comments
 (0)