npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@cosmixclub/docsafe-br

v1.0.0

Published

Validação e formatação de CPF e CNPJ no padrão brasileiro

Readme

@cosmixclub/docsafe-br

Este pacote fornece funções para validação e formatação de CPF e CNPJ, garantindo que os números estejam no formato correto e sejam válidos de acordo com as regras da Receita Federal do Brasil.

Índice

Instalação

pnpm add @cosmixclub/docsafe-br

ou

npm install @cosmixclub/docsafe-br

ou

yarn add @cosmixclub/docsafe-br

Uso

Validando um CPF

A função isValidCPF verifica se um CPF é válido de acordo com as regras da Receita Federal.

import { isValidCPF } from "@cosmixclub/docsafe-br";

console.log(isValidCPF("123.456.789-09")); // true
console.log(isValidCPF("111.111.111-11")); // false

Validando um CNPJ

A função isValidCNPJ verifica se um CNPJ é válido.

import { isValidCNPJ } from "@cosmixclub/docsafe-br";

console.log(isValidCNPJ("12.345.678/0001-95")); // true
console.log(isValidCNPJ("11.111.111/0001-11")); // false

Formatando um CPF

A função formatCPF transforma um CPF não formatado para o padrão XXX.XXX.XXX-XX.

import { formatCPF } from "@cosmixclub/docsafe-br";

console.log(formatCPF("11144477735")); // "111.444.777-35"
console.log(formatCPF("123")); // "123" (mantém se inválido)

Formatando um CNPJ

A função formatCNPJ transforma um CNPJ não formatado para o padrão XX.XXX.XXX/XXXX-XX.

import { formatCNPJ } from "@cosmixclub/docsafe-br";

console.log(formatCNPJ("54550752000155")); // "54.550.752/0001-55"
console.log(formatCNPJ("123456")); // "123456" (mantém se inválido)

API

Funções de Validação

isValidCPF(cpf: string): boolean

  • Parâmetro: cpf - Uma string representando um CPF, com ou sem formatação.
  • Retorno: true se o CPF for válido, false caso contrário.
isValidCPF("123.456.789-09"); // true
isValidCPF("111.111.111-11"); // false

isValidCNPJ(cnpj: string): boolean

  • Parâmetro: cnpj - Uma string representando um CNPJ, com ou sem formatação.
  • Retorno: true se o CNPJ for válido, false caso contrário.
isValidCNPJ("12.345.678/0001-95"); // true
isValidCNPJ("11.111.111/0001-11"); // false

Funções de Formatação

formatCPF(cpf: string): string

  • Parâmetro: cpf - Uma string representando um CPF, com ou sem formatação.
  • Retorno: O CPF formatado no padrão XXX.XXX.XXX-XX.
formatCPF("11144477735"); // "111.444.777-35"
formatCPF("12345678900"); // "123.456.789-00"

formatCNPJ(cnpj: string): string

  • Parâmetro: cnpj - Uma string representando um CNPJ, com ou sem formatação.
  • Retorno: O CNPJ formatado no padrão XX.XXX.XXX/XXXX-XX.
formatCNPJ("54550752000155"); // "54.550.752/0001-55"
formatCNPJ("12345678000199"); // "12.345.678/0001-99"

Contribuição

  1. Fork este repositório.
  2. Crie uma branch para sua funcionalidade (git checkout -b feature/nova-funcionalidade).
  3. Faça o commit das alterações (git commit -m "Adiciona nova funcionalidade").
  4. Envie para a branch (git push origin feature/nova-funcionalidade).
  5. Abra um Pull Request.

Se encontrar algum bug ou tiver sugestões de melhorias, sinta-se à vontade para abrir uma issue!


Este pacote foi desenvolvido para ser simples, eficiente e confiável na validação e formatação de CPF e CNPJ no Brasil. 🚀🇧🇷