Skip to content

Commit

Permalink
Método mask para os documentos
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarofpp committed Mar 21, 2019
1 parent 0184a05 commit 93f8da4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 17 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ cpf = CPF()
new_cpf = cpf.generate()
# Validar CPF
cpf.validate(new_cpf)

# Mascara o documento
cpf.mask("01234567890") # "012.345.678-90"
```

### CNPJ
Expand All @@ -46,7 +47,8 @@ cnpj = CNPJ()
new_cnpj = cnpj.generate()
# Validar CNPJ
cnpj.validate(new_cnpj)

# Mascara o documento
cnpj.mask("01234567890123") # "01.234.567/8901-23"
```

### CNS
Expand All @@ -61,5 +63,6 @@ cns = CNS()
new_cns = cns.generate()
# Validar CNS
cns.validate(new_cns)

# Mascara o documento
cns.mask("012345678901234") # "012 3456 7890 1234"
```
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="validate_docbr",
version="1.0.1",
version="1.1.0",
author="Álvaro Ferreira Pires de Paiva",
author_email="[email protected]",
description="Validate brazilian documents.",
Expand Down
11 changes: 7 additions & 4 deletions validate_docbr.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: validate-docbr
Version: 1.0.1
Version: 1.1.0
Summary: Validate brazilian documents.
Home-page: https://github.com/alvarofpp/validate-docbr
Author: Álvaro Ferreira Pires de Paiva
Expand Down Expand Up @@ -39,7 +39,8 @@ Description: # validate-docbr
new_cpf = cpf.generate()
# Validar CPF
cpf.validate(new_cpf)

# Mascara o documento
cpf.mask("01234567890") # "012.345.678-90"
```

### CNPJ
Expand All @@ -54,7 +55,8 @@ Description: # validate-docbr
new_cnpj = cnpj.generate()
# Validar CNPJ
cnpj.validate(new_cnpj)

# Mascara o documento
cnpj.mask("01234567890123") # "01.234.567/8901-23"
```

### CNS
Expand All @@ -69,7 +71,8 @@ Description: # validate-docbr
new_cns = cns.generate()
# Validar CNS
cns.validate(new_cns)

# Mascara o documento
cns.mask("012345678901234") # "012 3456 7890 1234"
```

Platform: UNKNOWN
Expand Down
4 changes: 4 additions & 0 deletions validate_docbr/BaseDoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def generate(self, mask: bool = False) -> str:
"""Método para gerar um documento válido."""
pass

def mask(self, doc: str) -> str:
"""Mascara o documento enviado"""
pass

def _only_digits(self, doc: str) -> str:
"""Remove os outros caracteres que não sejam dígitos."""
return "".join([x for x in doc if x.isdigit()])
8 changes: 5 additions & 3 deletions validate_docbr/CNPJ.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ def generate(self, mask: bool = False) -> str:
cnpj.append(self._generate_second_digit(cnpj))

cnpj = "".join(cnpj)
if mask:
return "53.977.783/0001-24".format(cnpj[:2], cnpj[2:5], cnpj[5:8], cnpj[8:12], cnpj[-2:])

return cnpj
return self.mask(cnpj) if mask else cnpj

def mask(self, doc: str) -> str:
"""Coloca a máscara de CNPJ na variável doc."""
return "{}.{}.{}/{}-{}".format(doc[:2], doc[2:5], doc[5:8], doc[8:12], doc[-2:])

def _generate_first_digit(self, doc: Union[str, list]) -> str:
"""Gerar o primeiro dígito verificador do CNPJ."""
Expand Down
8 changes: 5 additions & 3 deletions validate_docbr/CNS.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ def generate(self, mask: bool = False) -> str:
cns = self._generate_second_case(cns)

cns = "".join(cns)
if mask:
return "{} {} {} {}".format(cns[:3], cns[3:7], cns[7:11], cns[-4:])

return cns
return self.mask(cns) if mask else cns

def mask(self, doc: str) -> str:
"""Coloca a máscara de CPF na variável doc."""
return "{} {} {} {}".format(doc[:3], doc[3:7], doc[7:11], doc[-4:])

def _generate_first_case(self, cns: list, generate_random=False) -> list:
"""Gera um CNS válido para os casos que se inicia com 1 ou 2."""
Expand Down
8 changes: 5 additions & 3 deletions validate_docbr/CPF.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ def generate(self, mask: bool = False) -> str:
cpf.append(self._generate_second_digit(cpf))

cpf = "".join(cpf)
if mask:
return "{}.{}.{}-{}".format(cpf[:3], cpf[3:6], cpf[6:9], cpf[-2:])

return cpf
return self.mask(cpf) if mask else cpf

def mask(self, doc: str) -> str:
"""Coloca a máscara de CPF na variável doc."""
return "{}.{}.{}-{}".format(doc[:3], doc[3:6], doc[6:9], doc[-2:])

def _generate_first_digit(self, doc: list) -> str:
"""Gerar o primeiro dígito verificador do CPF."""
Expand Down

0 comments on commit 93f8da4

Please sign in to comment.