🇧🇷 CPF validation in Go
0
fork

Configure Feed

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

Merge branch 'master' into master

authored by

Eduardo Cuducos and committed by
GitHub
f365301d 0b441761

+13 -7
+13 -7
cpf.go
··· 7 7 "strings" 8 8 ) 9 9 10 + //Cpf type 11 + type Cpf string 12 + 13 + func (c Cpf) String() string { 14 + return string(c) 15 + } 16 + 10 17 func checksum(ds []int64) int64 { 11 18 var s int64 12 19 for i, n := range ds { ··· 19 26 return r 20 27 } 21 28 22 - type Cpf string 23 - 24 - func (c Cpf) String() string { 25 - return string(c) 26 - } 27 - 29 + //Validate check if Cpf is in a valid format 28 30 func (c Cpf) Validate() bool { 29 31 u := c.Unmask() 30 - if len(u) != 11 { 32 + 33 + if len(u) != 11 { 31 34 return false 32 35 } 33 36 ··· 43 46 ds[i] = c 44 47 m[c] = true 45 48 } 49 + 46 50 if len(m) == 1 { 47 51 return false 48 52 } ··· 50 54 return checksum(ds[:9]) == ds[9] && checksum(ds[:10]) == ds[10] 51 55 } 52 56 57 + //Mask return the formated value 53 58 func (c Cpf) Mask() string { 54 59 u := c.Unmask() 55 60 if len(u) < 11 { ··· 58 63 return fmt.Sprintf("%s.%s.%s-%s", u[:3], u[3:6], u[6:9], u[9:]) 59 64 } 60 65 66 + //Unmask remove format and return the raw data 61 67 func (c Cpf) Unmask() string { 62 68 return regexp.MustCompile(`\D`).ReplaceAllString(string(c), "") 63 69 }