cnpf

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2020 License: BSD-3-Clause Imports: 2 Imported by: 0

README

Go CNPF TestsGoDocGo version

A Go module to validate CPF and CNPJ numbers (Brazilian people andcompanies unique identifier for the Federal Revenue).

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.

package main

import "github.com/cuducos/go-cnpf"


func main() {
	// these return true
	cnpf.IsValid("23858488135")
	cnpf.IsValid("238.584.881-35")
    cnpf.IsValid("11222333000181")
	cnpf.IsValid("11.222.333/0001-81")

	// these return false
	cnpf.IsValid("111.111.111-11")
	cnpf.IsValid("11.111.111/1111-11")
	cnpf.IsValid("123.456.769/01")
	cnpf.IsValid("12.345.678 9012-34")
    cnpf.IsValid("ABC.DEF.GHI-JK")
    cnpf.IsValid("AB.CDE.FGH/IJKL-MN")
	cnpf.IsValid("123")

	// these returns 11111111111 and 11111111111111
    cnpf.Unmask("111.111.111-11")
	cnpf.Unmask("11.111.111/1111-11")

	// this returns 111.111.111-11 and 11.111.111/1111-11
    cnpf.Mask("11111111111")
	cnpf.Mask("11111111111111")	
	
}

Based on Go CPF and Go CPNJ ❤️

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValid

func IsValid(n string) bool

IsValid checks whether a number is a valid CPF or CNPJ number

Example (MaskedInvalidCnpj)
fmt.Println(IsValid("11.111.111/1111-11"))
Output:

false
Example (MaskedInvalidCpf)
fmt.Println(IsValid("111.111.111-11"))
Output:

false
Example (MaskedLettersCnpj)
fmt.Println(IsValid("AB.CDE.FGH/IJKL-MN"))
Output:

false
Example (MaskedLettersCpf)
fmt.Println(IsValid("ABC.DEF.GHI-JK"))
Output:

false
Example (MaskedUnknownStringCnpj)
fmt.Println(IsValid("12.345.678 9012-34"))
Output:

false
Example (MaskedUnknownStringForCpf)
fmt.Println(IsValid("123.456.769/01"))
Output:

false
Example (MaskedValidCnpj)
fmt.Println(IsValid("11.222.333/0001-81"))
Output:

true
Example (MaskedValidCpf)
fmt.Println(IsValid("238.584.881-35"))
Output:

true
Example (NmaskedValidCnpj)
fmt.Println(IsValid("11222333000181"))
Output:

true
Example (UnmaskedValidCpf)
fmt.Println(IsValid("23858488135"))
Output:

true

func Mask

func Mask(n string) string

Mask returns the CPF or CNPJ number formatted

Example (UnmaskedCnpj)
fmt.Println(Mask("11111111111111"))
Output:

11.111.111/1111-11
Example (UnmaskedCpf)
fmt.Println(Mask("11111111111"))
Output:

111.111.111-11

func Unmask

func Unmask(n string) string

Unmask removes any non-digit (numeric) from a CPF or CNPJ number

Example (MaskedCnpj)
fmt.Println(Unmask("11.111.111/1111-11"))
Output:

11111111111111
Example (MaskedCpf)
fmt.Println(Unmask("111.111.111-11"))
Output:

11111111111

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL