cnpj-cpf-validator
Version:
Uma biblioteca TypeScript/JavaScript/Node para validar e formatar números de CPF e CNPJ já com o suporte aos novos CNPJ alfanuméricos.
85 lines (84 loc) • 3.52 kB
TypeScript
/**
* CPF/CNPJ Validator and Formatter
* A TypeScript/JavaScript library for validating and formatting Brazilian CPF and CNPJ numbers
*
* CNPJ Format Support:
* - Traditional numeric CNPJ (14 digits): XX.XXX.XXX/XXXX-XX
* - New alphanumeric CNPJ format (effective July 2026):
* - 14 characters total
* - First 12 positions can contain letters and numbers
* - Last 2 positions (verification digits) remain numeric
* - Format: XX.XXX.XXX/XXXX-XX (can include letters)
* - Example: A1B2.C3D4.E5F6/G7H8-01
*
* The library supports both formats simultaneously, automatically detecting
* which format is being used and applying the appropriate validation rules.
*/
/**
* Removes all non-numeric characters from a string
* @param value The string to clean
* @returns A string containing only numbers
*/
export declare function cleanNumbers(value: string): string;
/**
* Removes all non-alphanumeric characters from a string
* @param value The string to clean
* @returns A string containing only letters and numbers
*/
export declare function cleanAlphanumeric(value: string): string;
/**
* Validates if a CPF number is valid
* @param cpf The CPF number to validate (can be formatted or just numbers)
* @returns True if the CPF is valid, false otherwise
*/
export declare function isValidCPF(cpf: string): boolean;
/**
* Formats a CPF number with the standard mask (XXX.XXX.XXX-XX)
* @param cpf The CPF number to format (can be formatted or just numbers)
* @returns The formatted CPF or an empty string if invalid
*/
export declare function formatCPF(cpf: string): string;
/**
* Removes any non-numeric characters from a CPF
* @param cpf The CPF to clean
* @returns A string containing only the CPF numbers
*/
export declare function cleanCPF(cpf: string): string;
/**
* Checks if a CNPJ is in the new alphanumeric format
* @param cnpj The cleaned CNPJ string
* @returns True if the CNPJ contains letters, false otherwise
*/
export declare function isAlphanumericCNPJ(cnpj: string): boolean;
/**
* Validates if a CNPJ number is valid (supports both numeric and alphanumeric formats)
* @param cnpj The CNPJ number to validate (can be formatted or just numbers/letters)
* @returns True if the CNPJ is valid, false otherwise
*/
export declare function isValidCNPJ(cnpj: string): boolean;
/**
* Formats a CNPJ number with the standard mask (XX.XXX.XXX/XXXX-XX)
* @param cnpj The CNPJ number to format (can be formatted or just numbers/letters)
* @returns The formatted CNPJ or an empty string if invalid
*/
export declare function formatCNPJ(cnpj: string): string;
/**
* Cleans a CNPJ by removing formatting characters
* For numeric CNPJs: removes all non-numeric characters
* For alphanumeric CNPJs: removes all non-alphanumeric characters
* @param cnpj The CNPJ to clean
* @returns A cleaned CNPJ string
*/
export declare function cleanCNPJ(cnpj: string): string;
/**
* Validates if a document number is a valid CPF or CNPJ
* @param document The document number to validate (can be formatted or just numbers/letters)
* @returns True if the document is a valid CPF or CNPJ, false otherwise
*/
export declare function isValidDocument(document: string): boolean;
/**
* Formats a document number as CPF or CNPJ based on its characteristics
* @param document The document number to format (can be formatted or just numbers/letters)
* @returns The formatted document or an empty string if invalid
*/
export declare function formatDocument(document: string): string;