🇧🇷 CNPJ validation in Go

1st commit

+207
+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.
+30
README.md
··· 1 + # Go CNPJ [![Tests](https://github.com/cuducos/go-cnpj/workflows/Tests/badge.svg)]()[![GoDoc](https://godoc.org/github.com/cuducos/go-cnpj?status.svg)](https://godoc.org/github.com/cuducos/go-cnpj)![Go version](https://img.shields.io/github/go-mod/go-version/cuducos/go-cnpj) 2 + 3 + A Go module to validate CNPJ numbers (Brazilian companies unique identifier for the Federal Revenue). 4 + 5 + ```go 6 + package main 7 + 8 + import "github.com/cuducos/go-cnpj" 9 + 10 + 11 + func main() { 12 + // these return true 13 + cnpj.IsValid("11222333000181") 14 + cnpj.IsValid("11.222.333/0001-81") 15 + 16 + // these return false 17 + cnpj.IsValid("11.111.111/1111-11") 18 + cnpj.IsValid("12.345.678 9012-34") 19 + cnpj.IsValid("AB.CDE.FGH/IJKL-MN") 20 + cnpj.IsValid("123") 21 + 22 + // this returns 11111111111111 23 + cnpj.Unmask("11.111.111/1111-11") 24 + 25 + // this returns 11.111.111/1111-11 26 + cnpj.Mask("11111111111111") 27 + } 28 + ``` 29 + 30 + Based on [Go CPF](https://github.com/cuducos/go-cpf) ❤️
+57
cnpj.go
··· 1 + package cnpj 2 + 3 + import ( 4 + "fmt" 5 + "regexp" 6 + "strconv" 7 + "strings" 8 + ) 9 + 10 + 11 + 12 + func checksum(ds []int64, ref []int64) int64 { 13 + var s int64 14 + for i, n := range ref { 15 + s += n * ds[i] 16 + } 17 + r := s % 11 18 + if r < 2 { 19 + return 0 20 + } 21 + return 11 - r 22 + } 23 + 24 + //IsValid checks whether CNPJ number is valid or not 25 + func IsValid(n string) bool { 26 + u := Unmask(n) 27 + if len(u) != 14 { 28 + return false 29 + } 30 + 31 + ds := make([]int64, 14) 32 + for i, v := range strings.Split(u, "") { 33 + c, err := strconv.ParseInt(v, 10, 32) 34 + if err != nil { 35 + return false 36 + } 37 + ds[i] = c 38 + } 39 + 40 + r1 := []int64{5,4,3,2,9,8,7,6,5,4,3,2} 41 + r2 := []int64{6,5,4,3,2,9,8,7,6,5,4,3,2} 42 + return checksum(ds, r1) == ds[12] && checksum(ds, r2) == ds[13] 43 + } 44 + 45 + //Mask returns the CNPJ number formatted 46 + func Mask(n string) string { 47 + u := Unmask(n) 48 + if len(u) != 14 { 49 + return n 50 + } 51 + return fmt.Sprintf("%s.%s.%s/%s-%s", u[:2], u[2:5], u[5:8], u[8:12], u[12:]) 52 + } 53 + 54 + //Unmask removes any non-digit (numeric) from the CNPJ number 55 + func Unmask(n string) string { 56 + return regexp.MustCompile(`\D`).ReplaceAllString(n, "") 57 + }
+75
cnpj_test.go
··· 1 + package cnpj 2 + 3 + import ( 4 + "fmt" 5 + "testing" 6 + ) 7 + 8 + func TestMask(t *testing.T) { 9 + for _, tc := range []struct { 10 + cpf string 11 + expected string 12 + }{ 13 + {"11111111111111", "11.111.111/1111-11"}, 14 + {"123456", "123456"}, 15 + {"11223344556677889900", "11223344556677889900"}, 16 + } { 17 + if got := Mask(tc.cpf); tc.expected != got { 18 + t.Errorf("Mask(\"%s\") = %v; expected %s", tc.cpf, got, tc.expected) 19 + } 20 + } 21 + } 22 + 23 + func TestUnmask(t *testing.T) { 24 + if got := Unmask("11.111.111/1111-11"); "11111111111111" != got { 25 + t.Errorf("Unmask(\"11.111.111/1111-11\") = %v; want 11111111111111", got) 26 + } 27 + } 28 + 29 + func TestIsValid(t *testing.T) { 30 + for _, tc := range []struct { 31 + cpf string 32 + expected bool 33 + }{ 34 + {"11222333000181", true}, 35 + {"11.222.333/0001-81", true}, 36 + {"123", false}, 37 + {"11.111.111/1111-11", false}, 38 + {"12.345.678 9012-34", false}, 39 + {"AB.CDE.FGH/IJKL-MN", false}, 40 + } { 41 + if got := IsValid(tc.cpf); tc.expected != got { 42 + t.Errorf("IsValid(%v) = %v; expected %v", tc.cpf, got, tc.expected) 43 + } 44 + } 45 + } 46 + 47 + func ExampleIsValid_validUnmasked() { 48 + fmt.Println(IsValid("11222333000181")) 49 + // Output: true 50 + } 51 + 52 + func ExampleIsValid_validMasked() { 53 + fmt.Println(IsValid("11.222.333/0001-81")) 54 + // Output: true 55 + } 56 + 57 + func ExampleIsValid_invalid() { 58 + fmt.Println(IsValid("11.111.111/1111-11")) 59 + // Output: false 60 + } 61 + 62 + func ExampleMask_valid() { 63 + fmt.Println(Mask("11111111111111")) 64 + // Output: 11.111.111/1111-11 65 + } 66 + 67 + func ExampleMask_invalid() { 68 + fmt.Println(Mask("42")) 69 + // Output: 42 70 + } 71 + 72 + func ExampleUnmask() { 73 + fmt.Println(Unmask("11.111.111/1111-11")) 74 + // Output: 11111111111111 75 + }
+3
go.mod
··· 1 + module github.com/cuducos/go-cnpj 2 + 3 + go 1.14