Skip to content

Commit

Permalink
add cnpj
Browse files Browse the repository at this point in the history
  • Loading branch information
ogilvieira committed Sep 28, 2023
1 parent 4702bf6 commit 812158c
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ yarn add validator-brasil
### TO DO

- [x] CPF
- [ ] CNPJ
- [x] CNPJ
- [ ] CEP
- [ ] RENAVAN
- [ ] CNH
Expand Down
7 changes: 3 additions & 4 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
declare const _default: {
isCPF: (value: string | number) => boolean;
};
export default _default;
import isCPF from './isCPF';
import isCNPJ from './isCNPJ';
export { isCPF, isCNPJ };
7 changes: 4 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isCNPJ = exports.isCPF = void 0;
const isCPF_1 = __importDefault(require("./isCPF"));
exports.default = {
isCPF: isCPF_1.default
};
exports.isCPF = isCPF_1.default;
const isCNPJ_1 = __importDefault(require("./isCNPJ"));
exports.isCNPJ = isCNPJ_1.default;
2 changes: 2 additions & 0 deletions dist/isCNPJ.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const isCNPJ: (value: string | number) => boolean;
export default isCNPJ;
47 changes: 47 additions & 0 deletions dist/isCNPJ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const isCNPJ = (value) => {
value = ('' + value).replace(/[\.\-\/]+/g, "");
if (value == "")
return false;
if (value.length != 14)
return false;
if (value == "00000000000000" ||
value == "11111111111111" ||
value == "22222222222222" ||
value == "33333333333333" ||
value == "44444444444444" ||
value == "55555555555555" ||
value == "66666666666666" ||
value == "77777777777777" ||
value == "88888888888888" ||
value == "99999999999999")
return false;
var size = value.length - 2;
var numbers = value.substring(0, size);
var digits = value.substring(size);
var sum = 0;
var pos = size - 7;
for (var i = size; i >= 1; i--) {
sum += +numbers.charAt(size - i) * pos--;
if (pos < 2)
pos = 9;
}
var result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
if (result !== +digits.charAt(0))
return false;
size = size + 1;
numbers = value.substring(0, size);
sum = 0;
pos = size - 7;
for (var i = size; i >= 1; i--) {
sum += +numbers.charAt(size - i) * pos--;
if (pos < 2)
pos = 9;
}
result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
if (result != +digits.charAt(1))
return false;
return true;
};
exports.default = isCNPJ;
2 changes: 1 addition & 1 deletion dist/isCPF.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const isCPF = (value) => {
value = ('' + value).replace(/\D/g, "");
value = ('' + value).replace(/[\.\-\/]+/g, "");
if (value == "")
return false;
if (value.length != 11 ||
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "validator-brasil",
"version": "0.0.1-beta",
"version": "0.0.2",
"description": "Extensão de validações brasileiras para o validator.js",
"main": "dist/index.js",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import isCPF from './isCPF';
import isCNPJ from './isCNPJ';

export default {
isCPF
export {
isCPF,
isCNPJ
}
23 changes: 23 additions & 0 deletions src/isCNPJ.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isCNPJ } from './index';

describe("cnpj validation", () => {
it("should return true to valid cnpj without hyphen and points", () => {
expect(isCNPJ("00933180000164")).toBeTruthy();
});

it("should return true to valid cnpj with hyphen and points", () => {
expect(isCNPJ("00.933.180/0001-64")).toBeTruthy();
});

it("should return false with letters", () => {
expect(isCNPJ("00933180000164a")).toBeFalsy();
});

it("should return false to invalid cnpj with hyphen and points", () => {
expect(isCNPJ("001112220000133")).toBeFalsy();
});

it("should return false to invalid cnpj", () => {
expect(isCNPJ("00000000000000")).toBeFalsy();
});
});
16 changes: 8 additions & 8 deletions src/isCNPJ.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const isCPF = ( value: string | number ): boolean => {
const isCNPJ = ( value: string | number ): boolean => {
value = (''+value).replace(/[\.\-\/]+/g, "");

if (value == "") return false;
Expand All @@ -20,30 +20,30 @@ const isCPF = ( value: string | number ): boolean => {
return false;

var size = value.length - 2;
var numbers = +value.substring(0, size);
var digits = +value.substring(size);
var numbers = value.substring(0, size);
var digits = value.substring(size);
var sum = 0;
var pos = size - 7;
for (var i = size; i >= 1; i--) {
sum += numbers.charAt(size - i) * pos--;
sum += +numbers.charAt(size - i) * pos--;
if (pos < 2) pos = 9;
}
var result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
if (result !== digits.charAt(0)) return false;
if (result !== +digits.charAt(0)) return false;

size = size + 1;
numbers = value.substring(0, size);
sum = 0;
pos = size - 7;
for (var i = size; i >= 1; i--) {
sum += numbers.charAt(size - i) * pos--;
sum += +numbers.charAt(size - i) * pos--;
if (pos < 2) pos = 9;
}
result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
if (result != digits.charAt(1)) return false;
if (result != +digits.charAt(1)) return false;

return true;
}


export default isCPF;
export default isCNPJ;
2 changes: 1 addition & 1 deletion src/isCPF.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isCPF from './isCPF';
import { isCPF } from './index';

describe("cpf validation", () => {
it("should return true to valid cpf wythout hyphen and points", () => {
Expand Down

0 comments on commit 812158c

Please sign in to comment.