Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: utilizando RegEX no arquivo phone.py #154

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Gera um CEP válido aleatório.

### is_valid_phone

Verifica se o número de telefone é valido, podendo ser telefone fixo ou celular. Apenas números, com DDD e sem o prefixo internacional, formatados como string. ***Exemplo: +55 48 9999 9999 ficaria '4899999999'.*** Esta função valida apenas números de telefone brasileiros e não verifica se o número realmente existe.
Verifica se o número de telefone é valido, podendo ser telefone fixo ou celular. Apenas números, com DDD e sem o prefixo internacional, formatados como string. ***Exemplo: +55 48 9999 9999 deve ser chamado como is_valid_phone('4899999999').*** Esta função valida apenas números de telefone brasileiros e não verifica se o número realmente existe.



Expand Down
43 changes: 27 additions & 16 deletions brutils/phone.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

# FORMATTING
############

Expand All @@ -7,26 +9,35 @@


def is_valid_landline(phone_number): # type: (str) -> bool
return (
isinstance(phone_number, str)
and phone_number.isdigit()
and len(phone_number) == 10
and phone_number[0] != "0"
and phone_number[1] != "0"
and phone_number[2] in ["2", "3", "4", "5"]
)
"""
Returns whether or not the verifying first 3 digits are a
match.This function validates only Brazilian landline numbers
and does not verify if the number actually exists.
Input should be a digit string of proper length.

"""
pattern = re.compile(r"^[1-9][1-9][2-5]\d{7}$")
return isinstance(phone_number, str) and re.match(pattern, phone_number)


def is_valid_mobile(phone_number): # type: (str) -> bool
return (
isinstance(phone_number, str)
and phone_number.isdigit()
and len(phone_number) == 11
and phone_number[0] != "0"
and phone_number[1] != "0"
and phone_number[2] == "9"
)
"""
Returns whether or not the verifying first 3 digits are a
match.This function validates only Brazilian mobile numbers
and does not verify if the number actually exists.
Input should be a digit string of proper length.

"""
pattern = re.compile(r"^[1-9][1-9][9]\d{8}$")
return isinstance(phone_number, str) and re.match(pattern, phone_number)


def is_valid(phone_number): # type: (str) -> bool
"""
Returns whether or not the verifying first 3 digits are a
match.This function validates only Brazilian phone numbers
and does not verify if the number actually exists.
Input should be a digit string of proper length.

"""
return is_valid_landline(phone_number) or is_valid_mobile(phone_number)
2 changes: 1 addition & 1 deletion tests/test_phone.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TestPhone(TestCase):
def test_is_valid_landline(self):
# When landline phone is not string, returns False
self.assertFalse(is_valid_landline(1938814933))
self.assertIs(is_valid_landline(1938814933), False)

# When landline phone doesn't contain only digits, returns False
self.assertFalse(is_valid_landline("(19)388149"))
Expand Down