CNPJ
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
const cnpj = require("@fnando/cnpj/commonjs");
import * as cnpj from "@fnando/cnpj";
import { isValid as isValidCnpj } from "@fnando/cnpj";
cnpj.isValid("41.381.074/6738-65");
cnpj.isValid("41381074673865");
cnpj.strip("41.381.074/6738-65");
cnpj.format("41381074673865");
cnpj.generate(true);
cnpj.generate();
On the web, without transformation, just use web/cnpj.min.js
.
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");
cnpj.strip("41#381#074-----6738\n\n65");
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);