diff --git a/README.md b/README.md index 73769b04..13ed333f 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,8 @@ 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: @@ -162,6 +163,14 @@ Exemplo: >>> 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 diff --git a/README_EN.md b/README_EN.md index 7917b673..2d38b429 100644 --- a/README_EN.md +++ b/README_EN.md @@ -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: @@ -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 diff --git a/brutils/__init__.py b/brutils/__init__.py index 6959a0d0..d45395a0 100644 --- a/brutils/__init__.py +++ b/brutils/__init__.py @@ -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 @@ -141,6 +144,7 @@ "generate_cpf", "is_valid_cpf", "remove_symbols_cpf", + "remove_symbols_from_cpf", # Email "is_valid_email", # Legal Process diff --git a/brutils/cpf.py b/brutils/cpf.py index d5d76721..5ccccd2e 100644 --- a/brutils/cpf.py +++ b/brutils/cpf.py @@ -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 + """ + CPF_LENGHT = 11 + + if not cpf: + return None + + if isinstance(cpf, str) and cpf != "": + cpf_no_symbols = "".join([char for char in cpf if char.isalnum()]) + + if len(cpf_no_symbols) == CPF_LENGHT: + return cpf_no_symbols diff --git a/tests/test_cpf.py b/tests/test_cpf.py index 84f49e70..dd03923b 100644 --- a/tests/test_cpf.py +++ b/tests/test_cpf.py @@ -9,6 +9,7 @@ generate, is_valid, remove_symbols, + remove_symbols_cpf, sieve, validate, ) @@ -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()