Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
bc982fa
Merge pull request #14 from potatowski/doc/update-readme
potatowski May 19, 2023
4ea6b9a
refactor: replace src package with utils package for document processing
potatowski Mar 26, 2025
8607cfa
test: update voter registration test to ignore voter output
potatowski Mar 26, 2025
e6bb7e0
refactor: add utility functions for document processing and validation
potatowski Mar 26, 2025
ec1a2b9
Merge pull request #15 from potatowski/refactor/utils
potatowski Mar 26, 2025
b246d47
feat: add Document interface for document operations
potatowski Mar 26, 2025
5120c1b
refactor: convert voter registration functions to methods of VoterReg…
potatowski Mar 26, 2025
93101b2
refactor: convert CPF functions to methods of CPF struct implementing…
potatowski Mar 26, 2025
e33aaff
refactor: convert CNPJ functions to methods of CNPJ struct implementi…
potatowski Mar 26, 2025
4084a9b
refactor: convert CNH functions to methods of CNH struct implementing…
potatowski Mar 26, 2025
529ba35
refactor: consolidate document operations into a unified interface wi…
potatowski Mar 26, 2025
0be39ac
docs: update README to reflect method changes for CNPJ and VoterRegis…
potatowski Mar 26, 2025
cb0cc12
feat: add IsValid, Format, and Generate methods for document validati…
potatowski Mar 26, 2025
48a2587
docs: update README to enhance clarity on package functionality and u…
potatowski Mar 26, 2025
c7dc19f
Merge pull request #16 from potatowski/improvement/interfaces
potatowski Mar 26, 2025
c869534
docs: add CHANGELOG to document notable changes and updates
potatowski Mar 26, 2025
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
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [2.0.0] - 2025-03-26

### Added
- `Document` interface for document operations.
- `IsValid`, `Format`, and `Generate` methods for document validation and formatting.

### Changed
- Converted voter registration functions to methods of `VoterRegistration` struct implementing the `Document` interface.
- Converted CPF functions to methods of `CPF` struct implementing the `Document` interface.
- Converted CNPJ functions to methods of `CNPJ` struct implementing the `Document` interface.
- Converted CNH functions to methods of `CNH` struct implementing the `Document` interface.
- Consolidated document operations into a unified interface with `IsValid`, `Format`, and `Generate` methods.
- Updated README to reflect method changes for `CNPJ` and `VoterRegistration`.
- Enhanced clarity on package functionality and usage examples in README.

### Fixed
- Updated voter registration test to ignore voter output.
54 changes: 38 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,65 @@
# Brazil-Code
# Brazil Code

This is validator, generator and formats the brazil code as CPF and CNPJ
The `brazilcode` package provides functionality to validate, generate, and format Brazilian identification codes, including CPF, CNPJ, CNH, and Título de Eleitor (Voter Registration).

> Formats to use
- CPF
- CNPJ
- CNH
- VoterRegistration(Título de Eleitor)
## Supported Formats
- **CPF**: Cadastro de Pessoas Físicas (Individual Taxpayer Registry)
- **CNPJ**: Cadastro Nacional de Pessoas Jurídicas (National Register of Legal Entities)
- **CNH**: Carteira Nacional de Habilitação (National Driver's License)
- **Voter Registration (Título de Eleitor)**: Brazilian Electoral Registration Number

## Development
Import package with command in shell
## Installation
To install the `brazilcode` package, run the following command in your shell:
```shell
$ go get github.com/potatowski/brazilcode
```
In code just use the function with import
> Example:
```code

## Usage
Once the package is installed, you can use the functions provided to generate, validate, and format documents.

### Example Code:
```go
package main

import (
"fmt"

"github.com/potatowski/brazilcode"
)

func main() {
doc, err := brazilcode.CNPJGenerate()
// Generate a CNPJ document
doc, err := brazilcode.CNPJ.Generate()
if err != nil {
panic(err)
}

docFormatted, err := brazilcode.CNPJFormat(doc)
// Format the CNPJ document
docFormatted, err := brazilcode.CNPJ.Format(doc)
if err != nil {
panic(err)
}

// Print both unformatted and formatted documents
fmt.Println(doc, docFormatted)
}
```

Alternatively, you can use the generic `Generate` and `Format` functions to generate and format any document type:
```go
doc, err := brazilcode.Generate("CNPJ")
if err != nil {
panic(err)
}

docFormatted, err := brazilcode.Format("CNPJ", doc)
if err != nil {
panic(err)
}

fmt.Println(doc, docFormatted)
```

## License
This project is licensed under the MIT License.

The MIT License © 2023 João Vitor Lima da Rocha
© 2023 João Vitor Lima da Rocha
169 changes: 55 additions & 114 deletions brazilcode.go
Original file line number Diff line number Diff line change
@@ -1,131 +1,72 @@
package brazilcode

import (
"errors"

"github.com/potatowski/brazilcode/src/cnh"
"github.com/potatowski/brazilcode/src/cnpj"
"github.com/potatowski/brazilcode/src/cpf"
iface "github.com/potatowski/brazilcode/src/interface"
"github.com/potatowski/brazilcode/src/voterRegistration"
)

var (
//Errors about CNPJ
ErrCNPJInvalid = cnpj.ErrCNPJInvalid
ErrCNPJInvalidLength = cnpj.ErrCNPJInvalidLength

//Errors about CPF
ErrCPFInvalidLength = cpf.ErrCPFInvalidLength
ErrCPFInvalid = cpf.ErrCPFInvalid

//Errors about CNH
ErrCNHInvalid = cnh.ErrCNHInvalid
ErrCNHInvalidLength = cnh.ErrCNHInvalidLength

//Errors about Voter Registration
ErrVoterRegistrationInvalid = voterRegistration.ErrVoterRegistrationInvalid
ErrVoterRegistrationInvalidLength = voterRegistration.ErrVoterRegistrationInvalidLength
ErrVoterRegistrationInvalidUF = voterRegistration.ErrVoterRegistrationInvalidUF
ErrVoterRegistrationLimit = voterRegistration.ErrVoterRegistrationLimit
)

/*
CNPJIsValid check if the CNPJ is valid
- @param {string} doc - CNPJ
- @return {error} - error
*/
func CNPJIsValid(doc string) error {
return cnpj.IsValid(doc)
}

/*
CNPJFormat format the an valid CNPJ
- @param {string} doc - CNPJ
- @return {string} - CNPJ formatted
- @return {error} - error
*/
func CNPJFormat(doc string) (string, error) {
return cnpj.Format(doc)
}

/*
CNPJGenerate generate a valid CNPJ
- @return {string} - CNPJ
- @return {error} - error
*/
func CNPJGenerate() (string, error) {
return cnpj.Generate()
}

/*
CPFIsValid check if the CPF is valid
- @param {string} doc - CPF
- @return {error} - error
*/
func CPFIsValid(doc string) error {
return cpf.IsValid(doc)
}

/*
CPFFormat format the a valid CPF
- @param {string} doc - CPF
- @return {string} - CPF formatted
- @return {error} - error
*/
func CPFFormat(doc string) (string, error) {
return cpf.Format(doc)
}

/*
CPFGenerate generate a valid CPF
- @return {string} - CPF
- @return {error} - error
*/
func CPFGenerate() (string, error) {
return cpf.Generate()
}

/*
CNHIsValid check if the CNH is valid
- @param {string} doc
- @return {error} - error
*/
func CNHIsValid(doc string) error {
return cnh.IsValid(doc)
}
var CPF = cpf.CPF{}
var CNPJ = cnpj.CNPJ{}
var CNH = cnh.CNH{}
var VoterRegistration = voterRegistration.VoterRegistration{}

/*
CNHGenerate generate a valid CNH
- @return {string} - CNH
- @return {error} - error
*/
func CNHGenerate() (string, error) {
return cnh.Generate()
var Documents = map[string]iface.Document{
"CPF": CPF,
"CNPJ": CNPJ,
"CNH": CNH,
"VoterRegistration": VoterRegistration,
}

/*
VoterRegistrationIsValid check if the Voter Registration is valid
- @param {string} doc - Voter Registration
- @return {error} - error
*/
func VoterRegistrationIsValid(doc string) error {
return voterRegistration.IsValid(doc)
// IsValid checks if the provided document is valid based on its type.
// It takes a document type (docType) and the document value (doc) as input.
// If the document type exists in the Documents map, it delegates the validation
// to the corresponding IsValid method of the document type.
// Returns nil if the document is valid, or an error if the document type is not supported
// or the document fails validation.
func IsValid(docType, doc string) error {
if d, exists := Documents[docType]; exists {
return d.IsValid(doc)
}
return errors.New("document type not supported")
}

/*
VoterRegistrationFormat format the a valid Voter Registration
- @param {string} doc - Voter Registration
- @return {string} - Voter Registration formatted
- @return {error} - error
*/
func VoterRegistrationFormat(doc string) (string, error) {
return voterRegistration.Format(doc)
// Format formats a given document string based on its type.
// It takes two parameters: docType, which specifies the type of the document
// (e.g., CPF, CNPJ), and doc, which is the document string to be formatted.
// If the document type is supported, it returns the formatted document string.
// Otherwise, it returns an error indicating that the document type is not supported.
//
// Parameters:
// - docType: A string representing the type of the document.
// - doc: A string representing the document to be formatted.
//
// Returns:
// - A formatted document string if the document type is supported.
// - An error if the document type is not supported.
func Format(docType, doc string) (string, error) {
if d, exists := Documents[docType]; exists {
return d.Format(doc)
}
return "", errors.New("document type not supported")
}

/*
VoterRegistrationGenerate generate a valid Voter Registration
- @param {string} uf - UF is the state of Brazil or ZZ for foreigners
- @return {string}
- @return {error}
*/
func VoterRegistrationGenerate(uf string) (string, error) {
return voterRegistration.Generate(uf)
// Generate generates a document based on the provided document type.
// It returns the generated document as a string and an error if the document type is not supported.
//
// Parameters:
// - docType: A string representing the type of document to generate.
//
// Returns:
// - string: The generated document.
// - error: An error if the document type is not supported.
func Generate(docType string) (string, error) {
if d, exists := Documents[docType]; exists {
return d.Generate()
}
return "", errors.New("document type not supported")
}
71 changes: 71 additions & 0 deletions brazilcode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package brazilcode_test

import (
"testing"

"github.com/potatowski/brazilcode"
)

func TestIsValid(t *testing.T) {
tests := []struct {
docType string
doc string
wantErr bool
}{
{"CPF", "12345678909", false},
{"InvalidType", "", true},
}

for _, tt := range tests {
t.Run(tt.docType, func(t *testing.T) {
err := brazilcode.IsValid(tt.docType, tt.doc)
if (err != nil) != tt.wantErr {
t.Errorf("IsValid() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestFormat(t *testing.T) {
tests := []struct {
docType string
doc string
want string
wantErr bool
}{
{"CPF", "12345678909", "123.456.789-09", false},
{"CPF", "12345678908", "", true},
{"InvalidType", "xpto", "", true},
}

for _, tt := range tests {
t.Run(tt.docType, func(t *testing.T) {
got, err := brazilcode.Format(tt.docType, tt.doc)
if (err != nil) != tt.wantErr {
t.Errorf("Format() error = %v, wantErr %v", err, tt.wantErr)
}
if got != tt.want {
t.Errorf("Format() = %v, want %v", got, tt.want)
}
})
}
}

func TestGenerate(t *testing.T) {
tests := []struct {
docType string
wantErr bool
}{
{"CPF", false},
{"InvalidType", true},
}

for _, tt := range tests {
t.Run(tt.docType, func(t *testing.T) {
_, err := brazilcode.Generate(tt.docType)
if (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading