🇧🇷 CPF and CNPJ validation in Go

1st commit

+202
+30
.github/workflows/tests.yml
··· 1 + name: Tests 2 + 3 + on: [pull_request, push] 4 + 5 + jobs: 6 + 7 + build: 8 + name: Build 9 + runs-on: ubuntu-latest 10 + steps: 11 + 12 + - name: Set up Go 1.14 13 + uses: actions/setup-go@v1 14 + with: 15 + go-version: 1.14 16 + id: go 17 + 18 + - name: Check out code into the Go module directory 19 + uses: actions/checkout@v2 20 + 21 + - name: Get dependencies 22 + run: | 23 + go get -v -t -d ./... 24 + if [ -f Gopkg.toml ]; then 25 + curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh 26 + dep ensure 27 + fi 28 + 29 + - name: Build 30 + run: go test
+12
LICENSE
··· 1 + Copyright (c) 2020 Eduardo Cuducos 2 + All rights reserved. 3 + 4 + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 + 6 + 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 + 8 + 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 + 10 + 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 + 12 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+40
README.md
··· 1 + # Go CNPF [![Tests](https://github.com/cuducos/go-cnpf/workflows/Tests/badge.svg)]()[![GoDoc](https://godoc.org/github.com/cuducos/go-cnpf?status.svg)](https://godoc.org/github.com/cuducos/go-cnpf)![Go version](https://img.shields.io/github/go-mod/go-version/cuducos/go-cnpj) 2 + 3 + A Go module to validate CPF and CNPJ numbers (Brazilian people andcompanies unique identifier for the Federal Revenue). 4 + 5 + > The pseudo-acronym _CNPF_ is a sort of tong-twister and a common typo when developers discuss the implementation of objects that could hold either a CPF or a CNPJ numbers. 6 + 7 + ```go 8 + package main 9 + 10 + import "github.com/cuducos/go-cnpf" 11 + 12 + 13 + func main() { 14 + // these return true 15 + cnpf.IsValid("23858488135") 16 + cnpf.IsValid("238.584.881-35") 17 + cnpf.IsValid("11222333000181") 18 + cnpf.IsValid("11.222.333/0001-81") 19 + 20 + // these return false 21 + cnpf.IsValid("111.111.111-11") 22 + cnpf.IsValid("11.111.111/1111-11") 23 + cnpf.IsValid("123.456.769/01") 24 + cnpf.IsValid("12.345.678 9012-34") 25 + cnpf.IsValid("ABC.DEF.GHI-JK") 26 + cnpf.IsValid("AB.CDE.FGH/IJKL-MN") 27 + cnpf.IsValid("123") 28 + 29 + // these returns 11111111111 and 11111111111111 30 + cnpf.Unmask("111.111.111-11") 31 + cnpf.Unmask("11.111.111/1111-11") 32 + 33 + // this returns 111.111.111-11 and 11.111.111/1111-11 34 + cnpf.Mask("11111111111") 35 + cnpf.Mask("11111111111111") 36 + 37 + } 38 + ``` 39 + 40 + Based on [Go CPF](https://github.com/cuducos/go-cpf) and [Go CPNJ](https://github.com/cuducos/go-cnpj) ❤️
+28
cnpf.go
··· 1 + package cnpf 2 + 3 + import ( 4 + cpf "github.com/cuducos/go-cpf" 5 + cnpj "github.com/cuducos/go-cnpj" 6 + ) 7 + 8 + //Unmask removes any non-digit (numeric) from a CPF or CNPJ number 9 + func Unmask(n string) string { 10 + return cpf.Unmask(n) 11 + } 12 + 13 + //Mask returns the CPF or CNPJ number formatted 14 + func Mask(n string) string { 15 + u := Unmask(n) 16 + if len(u) == 11 { 17 + return cpf.Mask(u) 18 + } 19 + if len(u) == 14 { 20 + return cnpj.Mask(u) 21 + } 22 + return n 23 + } 24 + 25 + //IsValid checks whether a number is a valid CPF or CNPJ number 26 + func IsValid(n string) bool { 27 + return cpf.IsValid(n) || cnpj.IsValid(n) 28 + }
+80
cnpf_test.go
··· 1 + package cnpf 2 + 3 + import "fmt" 4 + 5 + func ExampleIsValid_unmaskedValidCpf() { 6 + fmt.Println(IsValid("23858488135")) 7 + // Output: true 8 + } 9 + 10 + func ExampleIsValid_maskedValidCpf(){ 11 + fmt.Println(IsValid("238.584.881-35")) 12 + // Output: true 13 + } 14 + 15 + func ExampleIsValid_nmaskedValidCnpj(){ 16 + fmt.Println(IsValid("11222333000181")) 17 + // Output: true 18 + } 19 + 20 + func ExampleIsValid_maskedValidCnpj(){ 21 + fmt.Println(IsValid("11.222.333/0001-81")) 22 + // Output: true 23 + } 24 + 25 + func ExampleIsValid_maskedInvalidCpf(){ 26 + fmt.Println(IsValid("111.111.111-11")) 27 + // Output: false 28 + } 29 + 30 + func ExampleIsValid_maskedInvalidCnpj(){ 31 + fmt.Println(IsValid("11.111.111/1111-11")) 32 + // Output: false 33 + } 34 + 35 + func ExampleIsValid_maskedUnknownStringForCpf(){ 36 + fmt.Println(IsValid("123.456.769/01")) 37 + // Output: false 38 + } 39 + 40 + func ExampleIsValid_maskedUnknownStringCnpj(){ 41 + fmt.Println(IsValid("12.345.678 9012-34")) 42 + // Output: false 43 + } 44 + 45 + func ExampleIsValid_maskedLettersCpf(){ 46 + fmt.Println(IsValid("ABC.DEF.GHI-JK")) 47 + // Output: false 48 + } 49 + 50 + func ExampleIsValid_maskedLettersCnpj(){ 51 + fmt.Println(IsValid("AB.CDE.FGH/IJKL-MN")) 52 + // Output: false 53 + } 54 + 55 + func ExampleIsValid_IncompleteNumber(){ 56 + fmt.Println(IsValid("123")) 57 + // Output: false 58 + } 59 + 60 + 61 + func ExampleUnmask_maskedCpf(){ 62 + fmt.Println(Unmask("111.111.111-11")) 63 + // Output: 11111111111 64 + } 65 + 66 + func ExampleUnmask_maskedCnpj(){ 67 + fmt.Println(Unmask("11.111.111/1111-11")) 68 + // Output: 11111111111111 69 + } 70 + 71 + 72 + func ExampleMask_unmaskedCpf(){ 73 + fmt.Println(Mask("11111111111")) 74 + // Output: 111.111.111-11 75 + } 76 + 77 + func ExampleMask_unmaskedCnpj(){ 78 + fmt.Println(Mask("11111111111111") ) 79 + // Output: 11.111.111/1111-11 80 + }
+8
go.mod
··· 1 + module github.com/cuducos/go-cnpf 2 + 3 + go 1.14 4 + 5 + require ( 6 + github.com/cuducos/go-cnpj v0.0.1 7 + github.com/cuducos/go-cpf v0.0.1 8 + )
+4
go.sum
··· 1 + github.com/cuducos/go-cnpj v0.0.1 h1:qx3CbujuZaugm/1HE0sN65BoqAO8bzRZtP7qEvvwMT8= 2 + github.com/cuducos/go-cnpj v0.0.1/go.mod h1:Oe0M530eHl9eXExbNWriqTD9kMv618ScjlGtEey/f4Y= 3 + github.com/cuducos/go-cpf v0.0.1 h1:LCyzmLSMKv49Qu+M3sozypOvT3WfZfj1/10fUUkdrdw= 4 + github.com/cuducos/go-cpf v0.0.1/go.mod h1:+K4ER/oqiFbNHrTgg4ZZsAGdkP5C6Cj2+Dwo4Lv9p+A=