A lightweight, high-performance TypeScript library for validation, generation, and formatting of Brazilian documents (CPF and CNPJ).
- 🏎️ Ultra-fast: Minimal overhead and highly optimized algorithms.
- 🛡️ Type-safe: Built with TypeScript for excellent IDE support and reliability.
- 📦 Zero Dependencies: Keeps your bundle size small.
- 🛠️ Full Toolkit: Validate, generate, format, and unformat with ease.
- 📍 Region-Aware: CPF generation support for specific Brazilian states.
- 🆔 Alphanumeric CNPJ: Full support for the new 2026 CNPJ format.
npm install @marcuth/cpfcnpj
# or
yarn add @marcuth/cpfcnpj
# or
pnpm add @marcuth/cpfcnpjimport { cpf } from '@marcuth/cpfcnpj';
// Validation
cpf.validate('123.456.789-00'); // false
cpf.validate('52998224725'); // true
// Generation
cpf.generate(); // '52998224725'
cpf.generate({ formatted: true }); // '529.982.247-25'
cpf.generate({ state: 'SP', formatted: true }); // SP region CPFimport { cnpj } from '@marcuth/cpfcnpj';
// Validation
cnpj.validate('12.345.678/0001-00'); // false
cnpj.validate('11.222.333/0001-81'); // true
cnpj.validate('ABC1D23E/0001-91'); // true (Alphanumeric example)
// Generation
cnpj.generate(); // '11222333000181'
cnpj.generate({ formatted: true }); // '11.222.333/0001-81'
cnpj.generate({ alphanumeric: true }); // 'ABC1D23E000191'Both modules export the following methods:
| Method | Signature | Description |
|---|---|---|
validate |
(value: string) => boolean |
Validates the check digits and format. |
generate |
(options?: GenerateOptions) => string |
Generates a valid document. |
format |
(value: string) => string |
Applies standard formatting (mask). |
unformat |
(value: string) => string |
Removes all non-digit characters. |
- CPF:
{ formatted?: boolean, state?: string } - CNPJ:
{ formatted?: boolean, alphanumeric?: boolean }
The library is fully tested with Vitest.
npm testDistributed under the MIT License.
Developed with ❤️ by Marcuth