Skip to content
forked from fnando/cnpj

🇧🇷 Validate, generate and format CNPJ numbers

License

Notifications You must be signed in to change notification settings

danillobarbosa/cnpj

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CNPJ

Build Status NPM package version License: MIT Minified size Minified+Gzip size

This package does some CNPJ magic. It allows you to create, validate and format CNPJ documents.

HINT: Check out the CPF counter part available at https://github.com/fnando/cpf.

Installation

This lib is available as a NPM package. To install it, use the following command:

npm install @fnando/cnpj --save

If you're using Yarn (and you should):

yarn add @fnando/cnpj

Usage

// Node.js-specific
const cnpj = require("@fnando/cnpj/dist/node");

// @import
import * as cnpj from "@fnando/cnpj"; // import the whole library
import {isValid as isValidCnpj} from "@fnando/cnpj"; // import just one function

// import via <script>; the lib will available as window.cnpj
// <script src="cnpj.js"></script>

cnpj.isValid("41.381.074/6738-65");
//=> true

cnpj.isValid("41381074673865");
//=> true

cnpj.strip("41.381.074/6738-65");
//=> 41381074673865

cnpj.format("41381074673865");
//=> 41.381.074/6738-65

cnpj.generate(true); // generate formatted number
//=> 54.385.406/3140-07

cnpj.generate(); // generate unformatted number
//=> 07033324230766

Strict Validation

By default, validations will strip any characters you provide. This means that the following is valid, because only numbers will be considered:

cnpj.isValid("41#381#074-----6738\n\n65");
//=> true

cnpj.strip("41#381#074-----6738\n\n65");
//=> 41381074673865

If you want to strict validate strings, use the following signature:

cnpj.isValid(number, strict);

The same example would now return false:

cnpj.isValid("41#381#074-----6738\n\n65", true);
//=> false

About

🇧🇷 Validate, generate and format CNPJ numbers

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%