Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fnando/cnpj
🇧🇷 Validate, generate and format CNPJ numbers
https://github.com/fnando/cnpj
cnpj javascript npm
Last synced: 5 days ago
JSON representation
🇧🇷 Validate, generate and format CNPJ numbers
- Host: GitHub
- URL: https://github.com/fnando/cnpj
- Owner: fnando
- License: mit
- Created: 2018-04-04T13:47:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-04-03T02:34:34.000Z (over 2 years ago)
- Last Synced: 2024-04-26T03:21:15.401Z (8 months ago)
- Topics: cnpj, javascript, npm
- Language: TypeScript
- Homepage:
- Size: 256 KB
- Stars: 83
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# CNPJ
[![Build Status](https://travis-ci.org/fnando/cnpj.svg?branch=master)](https://travis-ci.org/fnando/cnpj)
[![NPM package version](https://img.shields.io/npm/v/@fnando/cnpj.svg)](https://www.npmjs.com/package/@fnando/cnpj)
![License: MIT](https://img.shields.io/npm/l/@fnando/cnpj.svg)
![Minified size](http://img.badgesize.io/fnando/cnpj/master/web/cnpj.min.js.svg?label=min+size)
![Minified+Gzip size](http://img.badgesize.io/fnando/cnpj/master/web/cnpj.min.js.svg?compression=gzip&label=min%2Bgzip+size)This package does some [CNPJ](http://en.wikipedia.org/wiki/CNPJ) magic. It
allows you to create, validate and format CNPJ documents.**HINT:** Check out the CPF counter part available at
.## 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
```js
// Node.js-specific
const cnpj = require("@fnando/cnpj/commonjs");// @import
import * as cnpj from "@fnando/cnpj"; // import the whole library
import { isValid as isValidCnpj } from "@fnando/cnpj"; // import just one function// import via ; the lib will available as window.CNPJ
// <script src="cnpj.js">cnpj.isValid("41.381.074/6738-65");
//=> truecnpj.isValid("41381074673865");
//=> truecnpj.strip("41.381.074/6738-65");
//=> 41381074673865cnpj.format("41381074673865");
//=> 41.381.074/6738-65cnpj.generate(true); // generate formatted number
//=> 54.385.406/3140-07cnpj.generate(); // generate unformatted number
//=> 07033324230766
```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:```js
cnpj.isValid("41#381#074-----6738\n\n65");
//=> truecnpj.strip("41#381#074-----6738\n\n65");
//=> 41381074673865
```If you want to strict validate strings, use the following signature:
```js
cnpj.isValid(number, strict);
```The same example would now return `false`:
```js
cnpj.isValid("41#381#074-----6738\n\n65", true);
//=> false
```