docsbr
A dependency-free TypeScript library to validate, format, and generate Brazilian documents such as CPF and CNPJ.
About the Project
docsbr is a TypeScript-first library for validating, formatting, and generating Brazilian documents. Handling CPF and CNPJ numbers means manually implementing the Mod-11 algorithm, formatting masks, and generation logic; the library brings all of these operations together in a minimal, predictable API that stays consistent across modules.
The package is lightweight, has no external dependencies, and supports ESM natively, working on Node.js 18 or later, in modern bundlers such as Vite and esbuild, and in the browser. It is published on npm, with Vitest tests covering every module.
Key features:
- CPF and CNPJ validation with the Mod-11 algorithm, faithful to the official Brazilian Federal Revenue specifications
- Formatting and unformatting, accepting values with or without punctuation
- Random generation of valid documents, formatted or not
- Namespace or subpath imports, with tree-shaking support
- Full TypeScript typing and zero external dependencies
A library designed to solve, in a simple way, a common problem when building Brazilian systems.