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

Implementar função para remover símbolos específicos de uma string de CPF #444

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,23 @@ Argumentos:

Retorna:

- str: Uma nova string com os símbolos especificados removidos.
- str: Uma nova string com os símbolos especificados removidos. Caso o CPF
fornecido não seja válido, retornará None

Exemplo:

```python
>>> from brutils import remove_symbols_cpf
>>> remove_symbols_cpf('000.111.222-33')
'00011122233'
>>> remove_symbols_cpf('000.111.222-3')
None
>>> remove_symbols_cpf('000.111.222-333')
None
>>> remove_symbols_cpf('')
None
>>> remove_symbols_cpf(123)
None
```

### generate_cpf
Expand Down
11 changes: 10 additions & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ the '.', '-' characters from it.

Args:

- cpf (str): The CPF string containing symbols to be removed.
- cpf (str): The CPF string containing symbols to be removed. If the provided
CPF is invalid, it'll return None.

Returns:

Expand All @@ -163,6 +164,14 @@ Example:
>>> from brutils import remove_symbols_cpf
>>> remove_symbols_cpf('000.111.222-33')
'00011122233'
>>> remove_symbols_cpf('000.111.222-3')
None
>>> remove_symbols_cpf('000.111.222-333')
None
>>> remove_symbols_cpf('')
None
>>> remove_symbols_cpf(123)
None
```

### generate_cpf
Expand Down
4 changes: 4 additions & 0 deletions brutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
from brutils.cpf import (
remove_symbols as remove_symbols_cpf,
)
from brutils.cpf import (
remove_symbols_cpf as remove_symbols_from_cpf,
)

# Email Import
from brutils.email import is_valid as is_valid_email
Expand Down Expand Up @@ -141,6 +144,7 @@
"generate_cpf",
"is_valid_cpf",
"remove_symbols_cpf",
"remove_symbols_from_cpf",
# Email
"is_valid_email",
# Legal Process
Expand Down
38 changes: 38 additions & 0 deletions brutils/cpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,41 @@ def _checksum(basenum): # type: (str) -> str
verifying_digits += str(_hashdigit(basenum + verifying_digits, 11))

return verifying_digits


def remove_symbols_cpf(cpf: str) -> str:
"""
Compute the checksum digits for a given CPF base number.

This function removes the symbols dot and dash (`.` and `-`) from a CPF.

Args:
basenum (str): A formatted CPF string with standard visual aid symbols or None
if the input is invalid.

Returns:
str: A numbers-only CPF string. If it's not a valid number or format, it'll return None.

Example:
>>> from brutils import remove_symbols_cpf
>>> remove_symbols_cpf('000.111.222-33')
'00011122233'
>>> remove_symbols_cpf('')
None
>>> remove_symbols_cpf(123)
None
>>> remove_symbols_cpf('000.111.222')
None
>>> remove_symbols_cpf('000.111.222-333')
None
"""

result = None
cpf_no_symbols = ""

Choose a reason for hiding this comment

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

pode remover essa linha aqui, nao esta fazendo mais nada.

Copy link
Author

Choose a reason for hiding this comment

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

Fabio, qual linha? Desculpe, sou iniciante ainda.

Choose a reason for hiding this comment

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

@vmagueta claro sem problemas mano, pode remover a linha 276 onde esta sendo declarada a variavel cpf_no_symbols, não precisamos fazer essa atribuição aqui pois estamos declarando ela com o valor na linha 278.

if isinstance(cpf, str) and cpf != "":
cpf_no_symbols = "".join([char for char in cpf if char not in ".-"])

if len(cpf_no_symbols) == 11:
result = cpf_no_symbols

return result
19 changes: 19 additions & 0 deletions tests/test_cpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
generate,
is_valid,
remove_symbols,
remove_symbols_cpf,
sieve,
validate,
)
Expand Down Expand Up @@ -109,5 +110,23 @@ def test_when_cpf_is_not_valid_returns_none(self, mock_is_valid):
self.assertIsNone(format_cpf("11144477735"))


@patch("brutils.cpf.remove_symbols_cpf")
class TestRemoveSymbolsCPF(TestCase):
def test_remove_symbols_cpf_positive(self, mock_remove_symbols_cpf):
self.assertEqual(remove_symbols_cpf("123.456.789-10"), "12345678910")

def test_remove_symbols_cpf_negative_int(self, mock_remove_symbols_cpf):
self.assertIsNone(remove_symbols_cpf(123456789))

def test_remove_symbols_cpf_negative_empy(self, mock_remove_symbols_cpf):
self.assertIsNone(remove_symbols_cpf(""))

def test_remove_symbols_cpf_negative_longer(self, mock_remove_symbols_cpf):
self.assertIsNone(remove_symbols_cpf("111.222.333-444"))

def test_remove_symbols_cpf_negative_shorter(self, mock_remove_symbols_cpf):
self.assertIsNone(remove_symbols_cpf("111.222.333"))


if __name__ == "__main__":
main()