From 812158c2bdfcd997f1074513fde8bcb54b847918 Mon Sep 17 00:00:00 2001 From: Gil Vieira Date: Thu, 28 Sep 2023 13:00:43 -0300 Subject: [PATCH] add cnpj --- README.md | 2 +- dist/index.d.ts | 7 +++---- dist/index.js | 7 ++++--- dist/isCNPJ.d.ts | 2 ++ dist/isCNPJ.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++ dist/isCPF.js | 2 +- package.json | 2 +- src/index.ts | 6 ++++-- src/isCNPJ.test.ts | 23 +++++++++++++++++++++++ src/isCNPJ.ts | 16 ++++++++-------- src/isCPF.test.ts | 2 +- 11 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 dist/isCNPJ.d.ts create mode 100644 dist/isCNPJ.js create mode 100644 src/isCNPJ.test.ts diff --git a/README.md b/README.md index 9b18537..7be33c0 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ yarn add validator-brasil ### TO DO - [x] CPF -- [ ] CNPJ +- [x] CNPJ - [ ] CEP - [ ] RENAVAN - [ ] CNH diff --git a/dist/index.d.ts b/dist/index.d.ts index 44f80e0..ca39e34 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -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 }; diff --git a/dist/index.js b/dist/index.js index 7f36868..c13a826 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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; diff --git a/dist/isCNPJ.d.ts b/dist/isCNPJ.d.ts new file mode 100644 index 0000000..b994363 --- /dev/null +++ b/dist/isCNPJ.d.ts @@ -0,0 +1,2 @@ +declare const isCNPJ: (value: string | number) => boolean; +export default isCNPJ; diff --git a/dist/isCNPJ.js b/dist/isCNPJ.js new file mode 100644 index 0000000..4996bda --- /dev/null +++ b/dist/isCNPJ.js @@ -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; diff --git a/dist/isCPF.js b/dist/isCPF.js index f04687a..af997ab 100644 --- a/dist/isCPF.js +++ b/dist/isCPF.js @@ -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 || diff --git a/package.json b/package.json index 995bfa4..f382671 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.ts b/src/index.ts index c1aeedf..64241e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ import isCPF from './isCPF'; +import isCNPJ from './isCNPJ'; -export default { - isCPF +export { + isCPF, + isCNPJ } diff --git a/src/isCNPJ.test.ts b/src/isCNPJ.test.ts new file mode 100644 index 0000000..66eda7c --- /dev/null +++ b/src/isCNPJ.test.ts @@ -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(); + }); +}); diff --git a/src/isCNPJ.ts b/src/isCNPJ.ts index 67ee0ad..137eb36 100644 --- a/src/isCNPJ.ts +++ b/src/isCNPJ.ts @@ -1,4 +1,4 @@ -const isCPF = ( value: string | number ): boolean => { +const isCNPJ = ( value: string | number ): boolean => { value = (''+value).replace(/[\.\-\/]+/g, ""); if (value == "") return false; @@ -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; diff --git a/src/isCPF.test.ts b/src/isCPF.test.ts index be3658c..94d33ab 100644 --- a/src/isCPF.test.ts +++ b/src/isCPF.test.ts @@ -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", () => {