Skip to content

Commit 718d0d7

Browse files
Add isCnpj (#28)
1 parent 90c29f5 commit 718d0d7

File tree

13 files changed

+221
-5
lines changed

13 files changed

+221
-5
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107

108108
// Spell Checker
109109
"cSpell.words": [
110+
"cnpj",
110111
"DMYS",
111112
"injectables",
112113
"Luma",

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Removed
1515

16+
## [1.6.0] - 2021-11-03
17+
18+
### Added
19+
20+
- `isCnpj`
21+
- `isMaskedCnpj`
22+
23+
### Changed
24+
25+
### Removed
26+
1627
## [1.5.0] - 2021-10-27
1728

1829
### Added

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ npm i @techmmunity/utils
7878
| -------------------- | ----------------------------------------------------------------- |
7979
| `isBetween` | Return true if value is a number between 2 values |
8080
| `isBrazilianPhone` | Return true if value is a brazilian phone |
81+
| `isCnpj` | Return true if value is a CNPJ |
8182
| `isCpf` | Return true if value is a CPF |
8283
| `isDarkHexColor` | Return true if value is a dark hex color |
8384
| `isDate` | Return true if value is a valid date in the specified format |
@@ -102,6 +103,7 @@ npm i @techmmunity/utils
102103
| `isIsoDate` | Return true if value is a ISO Date |
103104
| `isLeap` | Return true if value is a leap year |
104105
| `isLightHexColor` | Return true if value is a light hex color |
106+
| `isMaskedCnpj` | Return true if value is a masked CNPJ |
105107
| `isMaskedCpf` | Return true if value is a masked CPF |
106108
| `isNumeric` | Return true if value is a numeric string |
107109
| `isOdd` | Return true if value is a odd number |

jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ module.exports = {
2121
testTimeout: 15000,
2222
coverageThreshold: {
2323
global: {
24-
statements: 99.17,
25-
branches: 98.14,
24+
statements: 99.04,
25+
branches: 97.86,
2626
functions: 100,
27-
lines: 99.64,
27+
lines: 99.68,
2828
},
2929
},
3030
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@techmmunity/utils",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"main": "index.js",
55
"types": "index.d.ts",
66
"license": "Apache-2.0",

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export * from "./lib/has-url";
4646

4747
export * from "./lib/is-between";
4848
export * from "./lib/is-brazilian-phone";
49+
export * from "./lib/is-cnpj";
4950
export * from "./lib/is-cpf";
5051
export * from "./lib/is-dark-hex-color";
5152
export * from "./lib/is-date";
@@ -68,6 +69,10 @@ export * from "./lib/is-int";
6869
export * from "./lib/is-ipv4";
6970
export * from "./lib/is-ipv4-with-mask";
7071
export * from "./lib/is-iso-date";
72+
export * from "./lib/is-leap";
73+
export * from "./lib/is-light-hex-color";
74+
export * from "./lib/is-masked-cnpj";
75+
export * from "./lib/is-masked-cpf";
7176
export * from "./lib/is-numeric";
7277
export * from "./lib/is-odd";
7378
export * from "./lib/is-package-installed";

src/lib/is-cnpj/index.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* eslint-disable @typescript-eslint/no-magic-numbers */
2+
3+
import { getTypeof } from "../get-typeof";
4+
5+
/*
6+
* CPF validation according to Receita Federal
7+
* More info: https://www.geradorcnpj.com/algoritmo_do_cnpj.htm
8+
*/
9+
10+
const notCnpj = [
11+
"00000000000000",
12+
"11111111111111",
13+
"22222222222222",
14+
"33333333333333",
15+
"44444444444444",
16+
"55555555555555",
17+
"66666666666666",
18+
"77777777777777",
19+
"88888888888888",
20+
"99999999999999",
21+
];
22+
23+
/**
24+
* Check if a string is a valid cpf
25+
* - 55357314047
26+
*/
27+
export const isCnpj = (cnpj: string) => {
28+
if (getTypeof(cnpj) !== "string") return false;
29+
30+
if (cnpj === "") return false;
31+
32+
if (cnpj.length !== 14) return false;
33+
34+
if (notCnpj.includes(cnpj)) return false;
35+
36+
let length = cnpj.length - 2;
37+
let numbers = cnpj.substring(0, length);
38+
const digits = cnpj.substring(length);
39+
let sum = 0;
40+
let pos = length - 7;
41+
42+
for (let i = length; i >= 1; i--) {
43+
const nbr = parseInt(numbers.charAt(length - i), 10);
44+
45+
sum += nbr * pos--;
46+
47+
if (pos < 2) pos = 9;
48+
}
49+
50+
let result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
51+
52+
const firstChar = parseInt(digits.charAt(0), 10);
53+
54+
if (result !== firstChar) return false;
55+
56+
length += 1;
57+
numbers = cnpj.substring(0, length);
58+
sum = 0;
59+
pos = length - 7;
60+
61+
for (let i = length; i >= 1; i--) {
62+
const nbr = parseInt(numbers.charAt(length - i), 10);
63+
64+
sum += nbr * pos--;
65+
66+
if (pos < 2) pos = 9;
67+
}
68+
69+
result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
70+
71+
const secondChar = parseInt(digits.charAt(1), 10);
72+
73+
if (result !== secondChar) return false;
74+
75+
return true;
76+
};

src/lib/is-cpf/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const notCpf = [
2727
export const isCpf = (cpf: string) => {
2828
if (getTypeof(cpf) !== "string") return false;
2929

30+
if (cpf === "") return false;
31+
3032
let temp: string;
3133
let count: number;
3234
let total: number;

src/lib/is-masked-cnpj/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { getTypeof } from "../get-typeof";
2+
import { isCnpj } from "../is-cnpj";
3+
4+
const MASKED_CNPJ = /^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/;
5+
6+
export const isMaskedCNPJ = (maskedCNPJ: string) =>
7+
getTypeof(maskedCNPJ) === "string" &&
8+
MASKED_CNPJ.test(maskedCNPJ) &&
9+
isCnpj(maskedCNPJ.replace(/[^\d]+/g, ""));

src/lib/is-masked-cpf/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ const MASKED_CPF = /^\d{3}\.\d{3}\.\d{3}-\d{2}$/;
66
export const isMaskedCpf = (maskedCPF: string) =>
77
getTypeof(maskedCPF) === "string" &&
88
MASKED_CPF.test(maskedCPF) &&
9-
isCpf(maskedCPF.replace(/\D/g, ""));
9+
isCpf(maskedCPF.replace(/[^\d]+/g, ""));

0 commit comments

Comments
 (0)