🇧🇷 CPF validation in Go
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Creates a Go module

+58 -36
+30 -5
README.md
··· 1 - # Go CPF 1 + # Go CPF ![Tests](https://github.com/cuducos/go-cpf/workflows/Tests/badge.svg) 2 + 3 + A Go module to validate CPF numbers (Brazilian unique identifier for the Federal Revenue). 4 + 5 + ```go 6 + package main 7 + 8 + import "github.com/cuducos/go-cpf" 9 + 10 + 11 + func main() { 12 + // these return true 13 + cpf.IsValid("23858488135") 14 + cpf.IsValid("238.584.881-35") 15 + 16 + // these return false 17 + cpf.IsValid("111.111.111-11") 18 + cpf.IsValid("123.456.769/01") 19 + cpf.IsValid("ABC.DEF.GHI-JK") 20 + cpf.IsValid("123") 21 + 22 + // this returns 11111111111 23 + cpf.Unmask("111.111.111-11") 24 + 25 + // this returns 111.111.111-11 26 + cpf.Mask("11111111111") 27 + } 28 + ``` 2 29 3 - Three things about this repo: 30 + ## A bit of story and thankfulness 4 31 5 - 1. I just started to learn [Go](https://golang.org/) with [_Learn Go With Tests_](https://quii.gitbook.io/learn-go-with-tests/) and this CPF (Brazilian unique identifier for the Federal Revenue) validation script is actually **my very first lines in Go** (except the ones from the book) 6 - 2. I'm sharing it here to get **feedback** ❤️ 7 - 3. It's not a proper or usable package yet, I just expect **`go test`** to pass : ) 32 + I started to learn [Go](https://golang.org/) with [_Learn Go With Tests_](https://quii.gitbook.io/learn-go-with-tests/) and this CPF (Brazilian unique identifier for the Federal Revenue) validation script is actually **my very first lines in Go** (except the ones from the book). I'm sharing it here to get **feedback** ❤️
+12 -18
cpf.go
··· 7 7 "strings" 8 8 ) 9 9 10 - //auxiliar type and variable to build a set 10 + //These are auxiliar type and variable to build a set 11 11 type void struct{} 12 12 13 13 var member void 14 14 15 - //Cpf type 16 - type Cpf string 17 - 18 - func (c Cpf) String() string { 19 - return string(c) 20 - } 21 15 22 16 func checksum(ds []int64) int64 { 23 17 var s int64 ··· 31 25 return r 32 26 } 33 27 34 - //IsValid checks whether Cpf number is valid or not 35 - func (c Cpf) IsValid() bool { 36 - u := c.Unmask() 28 + //IsValid checks whether CPF number is valid or not 29 + func IsValid(n string) bool { 30 + u := Unmask(n) 37 31 38 32 if len(u) != 11 { 39 33 return false ··· 50 44 s[c] = member 51 45 } 52 46 53 - //If all digits are the same, the Cpf is not valid 47 + //If all digits are the same, the CPF is not valid 54 48 if len(s) == 1 { 55 49 return false 56 50 } ··· 58 52 return checksum(ds[:9]) == ds[9] && checksum(ds[:10]) == ds[10] 59 53 } 60 54 61 - //Mask returns the Cpf number formatted 62 - func (c Cpf) Mask() string { 63 - u := c.Unmask() 55 + //Mask returns the CPF number formatted 56 + func Mask(n string) string { 57 + u := Unmask(n) 64 58 if len(u) != 11 { 65 - return string(c) 59 + return n 66 60 } 67 61 return fmt.Sprintf("%s.%s.%s-%s", u[:3], u[3:6], u[6:9], u[9:]) 68 62 } 69 63 70 - //Unmask removes any non-digit (numeric) from the Cpf 71 - func (c Cpf) Unmask() string { 72 - return regexp.MustCompile(`\D`).ReplaceAllString(string(c), "") 64 + //Unmask removes any non-digit (numeric) from the CPF number 65 + func Unmask(n string) string { 66 + return regexp.MustCompile(`\D`).ReplaceAllString(n, "") 73 67 }
+13 -13
cpf_test.go
··· 11 11 {"123456", "123456"}, 12 12 {"11223344556677889900", "11223344556677889900"}, 13 13 } { 14 - if got := Cpf(tc.cpf).Mask(); tc.expected != got { 15 - t.Errorf("Cpf(\"%s\").Mask() = %v; want %s", tc.cpf, got, tc.expected) 14 + if got := Mask(tc.cpf); tc.expected != got { 15 + t.Errorf("Mask(\"%s\") = %v; expected %s", tc.cpf, got, tc.expected) 16 16 } 17 17 } 18 18 } 19 19 20 20 func TestUnmask(t *testing.T) { 21 - if got := Cpf("111.111.111-11").Unmask(); "11111111111" != got { 22 - t.Errorf("Cpf(\"111.111.111-11\").Unmask() = %v; want 11111111111", got) 21 + if got := Unmask("111.111.111-11"); "11111111111" != got { 22 + t.Errorf("Unmask(\"111.111.111-11\") = %v; want 11111111111", got) 23 23 } 24 24 } 25 25 26 26 func TestIsValid(t *testing.T) { 27 27 for _, tc := range []struct { 28 - cpf Cpf 28 + cpf string 29 29 expected bool 30 30 }{ 31 - {Cpf("23858488135"), true}, 32 - {Cpf("238.584.881-35"), true}, 33 - {Cpf("123"), false}, 34 - {Cpf("111.111.111-11"), false}, 35 - {Cpf("123.456.769/01"), false}, 36 - {Cpf("ABC.DEF.GHI-JK"), false}, 31 + {"23858488135", true}, 32 + {"238.584.881-35", true}, 33 + {"123", false}, 34 + {"111.111.111-11", false}, 35 + {"123.456.769/01", false}, 36 + {"ABC.DEF.GHI-JK", false}, 37 37 } { 38 - if got := tc.cpf.IsValid(); tc.expected != got { 39 - t.Errorf("Cpf(%v).IsValid() = %v; want %v", tc.cpf, got, tc.expected) 38 + if got := IsValid(tc.cpf); tc.expected != got { 39 + t.Errorf("IsValid(%v) = %v; expected %v", tc.cpf, got, tc.expected) 40 40 } 41 41 } 42 42 }
+3
go.mod
··· 1 + module github.com/cuducos/go-cpf 2 + 3 + go 1.14