@marcuth/cpfcnpj 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +101 -0
- package/dist/cnpj.d.ts +13 -0
- package/dist/cnpj.js +119 -0
- package/dist/cpf.d.ts +11 -0
- package/dist/cpf.js +55 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +40 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Marcuth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# cpfcnpj
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/cpfcnpj)
|
|
4
|
+
[](https://www.npmjs.com/package/cpfcnpj)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
A lightweight, high-performance TypeScript library for validation, generation, and formatting of Brazilian documents (CPF and CNPJ).
|
|
8
|
+
|
|
9
|
+
## ✨ Features
|
|
10
|
+
|
|
11
|
+
- 🏎️ **Ultra-fast**: Minimal overhead and highly optimized algorithms.
|
|
12
|
+
- 🛡️ **Type-safe**: Built with TypeScript for excellent IDE support and reliability.
|
|
13
|
+
- 📦 **Zero Dependencies**: Keeps your bundle size small.
|
|
14
|
+
- 🛠️ **Full Toolkit**: Validate, generate, format, and unformat with ease.
|
|
15
|
+
- 📍 **Region-Aware**: CNPJ generation support for specific Brazilian states.
|
|
16
|
+
- 🆔 **Alphanumeric CNPJ**: Full support for the new 2026 CNPJ format.
|
|
17
|
+
|
|
18
|
+
## 🚀 Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install cpfcnpj
|
|
22
|
+
# or
|
|
23
|
+
yarn add cpfcnpj
|
|
24
|
+
# or
|
|
25
|
+
pnpm add cpfcnpj
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 📖 Usage
|
|
29
|
+
|
|
30
|
+
### CPF (Cadastro de Pessoas Físicas)
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { cpf } from 'cpfcnpj';
|
|
34
|
+
|
|
35
|
+
// Validation
|
|
36
|
+
cpf.validate('123.456.789-00'); // false
|
|
37
|
+
cpf.validate('52998224725'); // true
|
|
38
|
+
|
|
39
|
+
// Generation
|
|
40
|
+
cpf.generate(); // '52998224725'
|
|
41
|
+
cpf.generate({ formatted: true }); // '529.982.247-25'
|
|
42
|
+
|
|
43
|
+
// Formatting
|
|
44
|
+
cpf.format('52998224725'); // '529.982.247-25'
|
|
45
|
+
cpf.unformat('529.982.247-25'); // '52998224725'
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### CNPJ (Cadastro Nacional da Pessoa Jurídica)
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { cnpj } from 'cpfcnpj';
|
|
52
|
+
|
|
53
|
+
// Validation
|
|
54
|
+
cnpj.validate('12.345.678/0001-00'); // false
|
|
55
|
+
cnpj.validate('11.222.333/0001-81'); // true
|
|
56
|
+
cnpj.validate('ABC1D23E/0001-91'); // true (Alphanumeric example)
|
|
57
|
+
|
|
58
|
+
// Generation
|
|
59
|
+
cnpj.generate(); // '11222333000181'
|
|
60
|
+
cnpj.generate({ formatted: true }); // '11.222.333/0001-81'
|
|
61
|
+
cnpj.generate({ alphanumeric: true }); // 'ABC1D23E000191'
|
|
62
|
+
cnpj.generate({ state: 'SP', formatted: true }); // SP region CNPJ
|
|
63
|
+
|
|
64
|
+
// Formatting
|
|
65
|
+
cnpj.format('11222333000181'); // '11.222.333/0001-81'
|
|
66
|
+
cnpj.unformat('11.222.333/0001-81'); // '11222333000181'
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 🛠️ API Reference
|
|
70
|
+
|
|
71
|
+
### `cpf` & `cnpj`
|
|
72
|
+
|
|
73
|
+
Both modules export the following methods:
|
|
74
|
+
|
|
75
|
+
| Method | Signature | Description |
|
|
76
|
+
| :--- | :--- | :--- |
|
|
77
|
+
| `validate` | `(value: string) => boolean` | Validates the check digits and format. |
|
|
78
|
+
| `generate` | `(options?: GenerateOptions) => string` | Generates a valid document. |
|
|
79
|
+
| `format` | `(value: string) => string` | Applies standard formatting (mask). |
|
|
80
|
+
| `unformat` | `(value: string) => string` | Removes all non-digit characters. |
|
|
81
|
+
|
|
82
|
+
#### `GenerateOptions`
|
|
83
|
+
|
|
84
|
+
- **CPF**: `{ formatted?: boolean }`
|
|
85
|
+
- **CNPJ**: `{ formatted?: boolean, state?: string, alphanumeric?: boolean }`
|
|
86
|
+
|
|
87
|
+
## 🧪 Testing
|
|
88
|
+
|
|
89
|
+
The library is fully tested with Vitest.
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
npm test
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## 📄 License
|
|
96
|
+
|
|
97
|
+
Distributed under the [MIT License](LICENSE).
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
Developed with ❤️ by [Marcuth](https://github.com/marcuth)
|
package/dist/cnpj.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const regex: {
|
|
2
|
+
formatted: RegExp;
|
|
3
|
+
raw: RegExp;
|
|
4
|
+
};
|
|
5
|
+
export declare function unformat(formatted: string): string;
|
|
6
|
+
export declare function format(raw: string): string;
|
|
7
|
+
export declare function validate(raw: string): boolean;
|
|
8
|
+
export type GenerateOptions = {
|
|
9
|
+
formatted?: boolean;
|
|
10
|
+
state?: string;
|
|
11
|
+
alphanumeric?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare function generate(options?: GenerateOptions): string;
|
package/dist/cnpj.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.regex = void 0;
|
|
4
|
+
exports.unformat = unformat;
|
|
5
|
+
exports.format = format;
|
|
6
|
+
exports.validate = validate;
|
|
7
|
+
exports.generate = generate;
|
|
8
|
+
const STATE_TO_DIGIT = {
|
|
9
|
+
AC: "2",
|
|
10
|
+
AL: "4",
|
|
11
|
+
AM: "2",
|
|
12
|
+
AP: "2",
|
|
13
|
+
BA: "5",
|
|
14
|
+
CE: "3",
|
|
15
|
+
DF: "1",
|
|
16
|
+
ES: "7",
|
|
17
|
+
GO: "1",
|
|
18
|
+
MA: "3",
|
|
19
|
+
MG: "6",
|
|
20
|
+
MS: "1",
|
|
21
|
+
MT: "1",
|
|
22
|
+
PA: "2",
|
|
23
|
+
PB: "4",
|
|
24
|
+
PE: "4",
|
|
25
|
+
PI: "3",
|
|
26
|
+
PR: "9",
|
|
27
|
+
RJ: "7",
|
|
28
|
+
RN: "4",
|
|
29
|
+
RO: "2",
|
|
30
|
+
RR: "2",
|
|
31
|
+
RS: "0",
|
|
32
|
+
SC: "9",
|
|
33
|
+
SE: "5",
|
|
34
|
+
SP: "8",
|
|
35
|
+
TO: "1",
|
|
36
|
+
};
|
|
37
|
+
exports.regex = {
|
|
38
|
+
formatted: /^[A-Z0-9]{2}\.[A-Z0-9]{3}\.[A-Z0-9]{3}\/[A-Z0-9]{4}-\d{2}$/i,
|
|
39
|
+
raw: /^[A-Z0-9]{12}\d{2}$/i,
|
|
40
|
+
};
|
|
41
|
+
function unformat(formatted) {
|
|
42
|
+
return formatted.replace(/[^A-Z0-9]/gi, "").toUpperCase();
|
|
43
|
+
}
|
|
44
|
+
function format(raw) {
|
|
45
|
+
const digits = unformat(raw);
|
|
46
|
+
if (!exports.regex.raw.test(digits)) {
|
|
47
|
+
throw new Error("Invalid CNPJ format");
|
|
48
|
+
}
|
|
49
|
+
return `${digits.slice(0, 2)}.${digits.slice(2, 5)}.${digits.slice(5, 8)}/${digits.slice(8, 12)}-${digits.slice(12)}`;
|
|
50
|
+
}
|
|
51
|
+
function validate(raw) {
|
|
52
|
+
const value = unformat(raw);
|
|
53
|
+
if (!exports.regex.raw.test(value))
|
|
54
|
+
return false;
|
|
55
|
+
if (/^(\w)\1{13}$/.test(value))
|
|
56
|
+
return false;
|
|
57
|
+
const calc = (n, weightStart) => {
|
|
58
|
+
let sum = 0;
|
|
59
|
+
let weight = weightStart;
|
|
60
|
+
for (let i = 0; i < n; i++) {
|
|
61
|
+
const charValue = value.charCodeAt(i) - 48;
|
|
62
|
+
sum += charValue * weight;
|
|
63
|
+
weight--;
|
|
64
|
+
if (weight < 2) {
|
|
65
|
+
weight = 9;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const remainder = sum % 11;
|
|
69
|
+
return remainder < 2 ? 0 : 11 - remainder;
|
|
70
|
+
};
|
|
71
|
+
return calc(12, 5) === parseInt(value[12], 10) && calc(13, 6) === parseInt(value[13], 10);
|
|
72
|
+
}
|
|
73
|
+
function generate(options = {}) {
|
|
74
|
+
const values = [];
|
|
75
|
+
for (let i = 0; i < 8; i++) {
|
|
76
|
+
if (i === 7 && options.state) {
|
|
77
|
+
const stateDigit = STATE_TO_DIGIT[options.state.toUpperCase()];
|
|
78
|
+
if (!stateDigit) {
|
|
79
|
+
throw new Error(`Invalid state: ${options.state}`);
|
|
80
|
+
}
|
|
81
|
+
values.push(parseInt(stateDigit, 10));
|
|
82
|
+
}
|
|
83
|
+
else if (options.alphanumeric) {
|
|
84
|
+
const isLetter = Math.random() > 0.5;
|
|
85
|
+
if (isLetter) {
|
|
86
|
+
values.push(Math.floor(Math.random() * 26) + 17);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
values.push(Math.floor(Math.random() * 10));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
values.push(Math.floor(Math.random() * 10));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
values.push(0, 0, 0, 1);
|
|
97
|
+
const calc = (n, weightStart) => {
|
|
98
|
+
let sum = 0;
|
|
99
|
+
let weight = weightStart;
|
|
100
|
+
for (let i = 0; i < n; i++) {
|
|
101
|
+
sum += values[i] * weight;
|
|
102
|
+
weight--;
|
|
103
|
+
if (weight < 2)
|
|
104
|
+
weight = 9;
|
|
105
|
+
}
|
|
106
|
+
const remainder = sum % 11;
|
|
107
|
+
return remainder < 2 ? 0 : 11 - remainder;
|
|
108
|
+
};
|
|
109
|
+
values.push(calc(12, 5));
|
|
110
|
+
values.push(calc(13, 6));
|
|
111
|
+
const raw = values
|
|
112
|
+
.map((v) => {
|
|
113
|
+
if (v >= 17)
|
|
114
|
+
return String.fromCharCode(v + 48);
|
|
115
|
+
return String(v);
|
|
116
|
+
})
|
|
117
|
+
.join("");
|
|
118
|
+
return options.formatted ? format(raw) : raw;
|
|
119
|
+
}
|
package/dist/cpf.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const regex: {
|
|
2
|
+
formatted: RegExp;
|
|
3
|
+
raw: RegExp;
|
|
4
|
+
};
|
|
5
|
+
export declare function unformat(formatted: string): string;
|
|
6
|
+
export declare function format(raw: string): string;
|
|
7
|
+
export declare function validate(raw: string): boolean;
|
|
8
|
+
export type GenerateOptions = {
|
|
9
|
+
formatted?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare function generate(options?: GenerateOptions): string;
|
package/dist/cpf.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.regex = void 0;
|
|
4
|
+
exports.unformat = unformat;
|
|
5
|
+
exports.format = format;
|
|
6
|
+
exports.validate = validate;
|
|
7
|
+
exports.generate = generate;
|
|
8
|
+
exports.regex = {
|
|
9
|
+
formatted: /^\d{3}\.\d{3}\.\d{3}-\d{2}$/,
|
|
10
|
+
raw: /^\d{11}$/,
|
|
11
|
+
};
|
|
12
|
+
function unformat(formatted) {
|
|
13
|
+
return formatted.replace(/\D/g, "");
|
|
14
|
+
}
|
|
15
|
+
function format(raw) {
|
|
16
|
+
const digits = unformat(raw);
|
|
17
|
+
if (!exports.regex.raw.test(digits)) {
|
|
18
|
+
throw new Error("Invalid CPF format");
|
|
19
|
+
}
|
|
20
|
+
return `${digits.slice(0, 3)}.${digits.slice(3, 6)}.${digits.slice(6, 9)}-${digits.slice(9)}`;
|
|
21
|
+
}
|
|
22
|
+
function validate(raw) {
|
|
23
|
+
const digits = unformat(raw);
|
|
24
|
+
if (!exports.regex.raw.test(digits))
|
|
25
|
+
return false;
|
|
26
|
+
if (/^(\d)\1{10}$/.test(digits))
|
|
27
|
+
return false;
|
|
28
|
+
const calc = (n, weightStart) => {
|
|
29
|
+
let sum = 0;
|
|
30
|
+
for (let i = 0; i < n; i++) {
|
|
31
|
+
sum += parseInt(digits[i], 10) * (weightStart - i);
|
|
32
|
+
}
|
|
33
|
+
const remainder = sum % 11;
|
|
34
|
+
return remainder < 2 ? 0 : 11 - remainder;
|
|
35
|
+
};
|
|
36
|
+
return calc(9, 10) === parseInt(digits[9], 10) && calc(10, 11) === parseInt(digits[10], 10);
|
|
37
|
+
}
|
|
38
|
+
function generate(options = {}) {
|
|
39
|
+
const digits = Array.from({ length: 9 }, () => Math.floor(Math.random() * 10));
|
|
40
|
+
while (digits.every((d) => d === digits[0])) {
|
|
41
|
+
digits[0] = Math.floor(Math.random() * 10);
|
|
42
|
+
}
|
|
43
|
+
const calc = (n, weightStart) => {
|
|
44
|
+
let sum = 0;
|
|
45
|
+
for (let i = 0; i < n; i++) {
|
|
46
|
+
sum += digits[i] * (weightStart - i);
|
|
47
|
+
}
|
|
48
|
+
const remainder = sum % 11;
|
|
49
|
+
return remainder < 2 ? 0 : 11 - remainder;
|
|
50
|
+
};
|
|
51
|
+
digits.push(calc(9, 10));
|
|
52
|
+
digits.push(calc(10, 11));
|
|
53
|
+
const raw = digits.join("");
|
|
54
|
+
return options.formatted ? format(raw) : raw;
|
|
55
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.cpf = exports.cnpj = void 0;
|
|
37
|
+
const cnpj = __importStar(require("./cnpj"));
|
|
38
|
+
exports.cnpj = cnpj;
|
|
39
|
+
const cpf = __importStar(require("./cpf"));
|
|
40
|
+
exports.cpf = cpf;
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@marcuth/cpfcnpj",
|
|
3
|
+
"description": "A lightweight, high-performance TypeScript library for validation, generation, and formatting of Brazilian documents (CPF and CNPJ).",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/marcuth/cpfcnpj.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/marcuth/cpfcnpj#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/marcuth/cpfcnpj/issues"
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist/*",
|
|
19
|
+
"!/**/__tests__"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc",
|
|
23
|
+
"dev": "ts-node ./src/index.ts",
|
|
24
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
25
|
+
"lint": "eslint \"src/**/*.ts\" --fix",
|
|
26
|
+
"test": "vitest",
|
|
27
|
+
"test:cov": "vitest run --coverage"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"cpf validator",
|
|
34
|
+
"cpf generator",
|
|
35
|
+
"cpf formatter",
|
|
36
|
+
"cnpj validator",
|
|
37
|
+
"cnpj generator",
|
|
38
|
+
"cnpj formatter"
|
|
39
|
+
],
|
|
40
|
+
"author": "Marcuth",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^25.5.2",
|
|
44
|
+
"@vitest/coverage-v8": "^4.1.3",
|
|
45
|
+
"eslint": "^10.2.0",
|
|
46
|
+
"eslint-config-prettier": "^10.1.8",
|
|
47
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
48
|
+
"eslint-plugin-unused-imports": "^4.4.1",
|
|
49
|
+
"prettier": "^3.8.1",
|
|
50
|
+
"prettier-plugin-sort-imports": "^1.8.11",
|
|
51
|
+
"ts-node": "^10.9.2",
|
|
52
|
+
"typescript": "^6.0.2",
|
|
53
|
+
"vitest": "^4.1.3"
|
|
54
|
+
}
|
|
55
|
+
}
|