A utility for formatting Brazilian CPF and CNPJ document numbers. Version 1.0.6 is the latest stable release. It automatically detects the document type based on input length (≤11 digits for CPF, >11 for CNPJ) and applies the standard formatting masks (XXX.XXX.XXX-XX for CPF, XX.XXX.XXX/XXXX-XX for CNPJ). The package removes non-numeric characters and limits input size. It includes TypeScript type definitions and has optional peer dependencies for React Hook Form integration. Unlike validation-focused libraries, it purely handles formatting without validation of check digits.
npm install cpf-cnpj-formatterNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows formatting CPF (11 digits) and CNPJ (14 digits) with automatic detection and partial formatting.
Use a separate validation library (e.g., @brazilian-utils/cpf) for full document validation.
Always sanitize input to only digits before formatting to avoid unexpected truncation.
Strip all non-digit characters using .replace(/\D/g, '') before calling the formatter.
Ensure input is a non-null string before calling formatCPFCNPJ.
Use: const formatCPFCNPJ = require('cpf-cnpj-formatter'); (no .default)Convert to string: formatCPFCNPJ(String(input))
Run: npm install cpf-cnpj-formatter