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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@artrsousa/cpf-cnpj-validator

v1.0.4

Published

Valida e gera CPF e CNPJ válidos (Brasil)

Downloads

421

Readme

cpf-cnpj-validator

Biblioteca leve e tipada para validação, geração e formatação de CPF e CNPJ válidos (Brasil), utilizando o algoritmo oficial de dígitos verificadores da Receita Federal.

Características

  • Validação de CPF numérico
  • Validação de CNPJ numérico
  • Suporte a CNPJ alfanumérico (formato futuro da Receita Federal)
  • Escrita em TypeScript
  • ESM-only (Node.js 18+)
  • Sem dependências externas

Instalação

npm install @artrsousa/cpf-cnpj-validator

Uso

A biblioteca é ESM-only ("type": "module"). Utilize import em Node.js 18+ ou bundlers modernos (Vite, Webpack, etc).


Uso com JavaScript (ES Modules)

import { cpf, cnpj } from '@artrsousa/cpf-cnpj-validator';

// CPF
cpf.isValid('313.402.809-30');
cpf.isValid('31340280930');

cpf.generate(); 
cpf.generate({ formatted: false });

cpf.format('31340280930');

// CNPJ numérico
cnpj.isValid('11.222.333/0001-81');
cnpj.isValid('11222333000181');

cnpj.generate();
cnpj.generate({ formatted: false });

cnpj.format('11222333000181');

// CNPJ alfanumérico
cnpj.generate({ type: 'alphanumeric' });
cnpj.generate({ type: 'alphanumeric', formatted: false });

Uso com TypeScript

A biblioteca inclui definições de tipos (.d.ts).

import {
  cpf,
  cnpj,
  type CPF,
  type CNPJ,
  type CpfGenerateOptions,
  type CnpjGenerateOptions
} from '@artrsousa/cpf-cnpj-validator';

const cpfValue: string = '313.402.809-30';

if (cpf.isValid(cpfValue)) {
  const formatted: string | null = cpf.format(cpfValue);
  console.log(formatted);
}

const options: CnpjGenerateOptions = {
  type: 'alphanumeric',
  formatted: true,
};

const novoCnpj: string = cnpj.generate(options);

Tipos Exportados

| Tipo | Descrição | | --------------------- | ----------------------------- | | CPF | Interface da API de CPF | | CNPJ | Interface da API de CNPJ | | CpfGenerateOptions | Opções para cpf.generate() | | CnpjGenerateOptions | Opções para cnpj.generate() |


API

CPF

cpf.isValid(value: string): boolean

Valida um CPF.

  • Aceita com ou sem formatação
  • Rejeita sequências repetidas (ex: 111.111.111-11)
  • Valida os dígitos verificadores

cpf.generate(options?: CpfGenerateOptions): string

Gera um CPF válido aleatoriamente.

| Opção | Tipo | Padrão | | ----------- | --------- | ------ | | formatted | boolean | true |

Exemplo:

cpf.generate();
cpf.generate({ formatted: false });

cpf.format(value: string): string | null

Formata para o padrão:

000.000.000-00

Retorna null caso não possua 11 dígitos válidos.


CNPJ

cnpj.isValid(value: string): boolean

Valida CNPJ numérico.

  • Aceita com ou sem formatação
  • Rejeita sequências repetidas
  • Valida os dígitos verificadores

cnpj.generate(options?: CnpjGenerateOptions): string

Gera um CNPJ válido.

| Opção | Tipo | Padrão | | ----------- | ----------------------------- | ----------- | | type | 'numeric' \| 'alphanumeric' | 'numeric' | | formatted | boolean | true |

Exemplo:

cnpj.generate();

cnpj.generate({
  type: 'alphanumeric',
  formatted: false
});

cnpj.format(value: string): string | null

Formata para o padrão:

00.000.000/0000-00
  • Funciona para CNPJ numérico
  • Funciona para CNPJ alfanumérico
  • Retorna null se não possuir 14 caracteres

Requisitos

  • Node.js 18+
  • Ambiente com suporte a ES Modules

Licença

MIT