From b1cc66b506219e6b4b824e4bd6838c2f3afaac5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Ant=C3=B4nia=20Maia?= Date: Sat, 1 Apr 2023 07:24:08 -0300 Subject: [PATCH] adding format cpf method --- README.md | 4 ++-- README_PT_BR.md | 4 ++-- brutils/__init__.py | 2 +- brutils/cpf.py | 8 ++++++++ tests/test_cpf.py | 7 +++++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a276ce43..62563a81 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,9 @@ False ### What if I want to format a numbers only string? ``` ->>> cpf.display('00011122233') +>>> from brutils import format_cpf, format_cnpj +>>> format_cpf('00011122233') '000.111.222-33' ->>> from brutils import format_cnpj >>> format_cnpj('00111222000100') '00.111.222/0001-00' ``` diff --git a/README_PT_BR.md b/README_PT_BR.md index 107f6076..5b852f30 100644 --- a/README_PT_BR.md +++ b/README_PT_BR.md @@ -54,9 +54,9 @@ False ### E se eu quiser formatar uma string numérica? ``` ->>> cpf.display('00011122233') +>>> from brutils import format_cpf, format_cnpj +>>> format_cpf('00011122233') '000.111.222-33' ->>> from brutils import format_cnpj >>> format_cnpj('00111222000100') '00.111.222/0001-00' ``` diff --git a/brutils/__init__.py b/brutils/__init__.py index bace502c..b6f839ae 100644 --- a/brutils/__init__.py +++ b/brutils/__init__.py @@ -1,2 +1,2 @@ -from brutils.cpf import is_valid as is_valid_cpf +from brutils.cpf import is_valid as is_valid_cpf, format_cpf from brutils.cnpj import is_valid as is_valid_cnpj, format_cnpj diff --git a/brutils/cpf.py b/brutils/cpf.py index b8d3eade..5013f8c5 100644 --- a/brutils/cpf.py +++ b/brutils/cpf.py @@ -26,6 +26,14 @@ def display(cpf): # type: (str) -> str return "{}.{}.{}-{}".format(cpf[:3], cpf[3:6], cpf[6:9], cpf[9:]) +def format_cpf(cpf): # type: (str) -> str + """ + Will format an adequately formatted numbers-only CPF string, + adding in standard formatting visual aid symbols for display. + """ + return display(cpf) + + # CALCULATORS ############# diff --git a/tests/test_cpf.py b/tests/test_cpf.py index 782161df..ddb53e7a 100644 --- a/tests/test_cpf.py +++ b/tests/test_cpf.py @@ -16,6 +16,7 @@ validate, generate, is_valid, + format_cpf, ) from unittest import TestCase, main @@ -35,6 +36,12 @@ def test_display(self): assert display("0000000000a") is None assert display("000000000000") is None + def test_format_cpf(self): + assert format_cpf("00000000011") == "000.000.000-11" + assert format_cpf("00000000000") is None + assert format_cpf("0000000000a") is None + assert format_cpf("000000000000") is None + def test_hashdigit(self): assert hashdigit("000000000", 10) == 0 assert hashdigit("0000000000", 11) == 0