generated from techmmunity/base-project-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90c29f5
commit 718d0d7
Showing
13 changed files
with
221 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,6 +107,7 @@ | |
|
||
// Spell Checker | ||
"cSpell.words": [ | ||
"cnpj", | ||
"DMYS", | ||
"injectables", | ||
"Luma", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
|
||
import { getTypeof } from "../get-typeof"; | ||
|
||
/* | ||
* CPF validation according to Receita Federal | ||
* More info: https://www.geradorcnpj.com/algoritmo_do_cnpj.htm | ||
*/ | ||
|
||
const notCnpj = [ | ||
"00000000000000", | ||
"11111111111111", | ||
"22222222222222", | ||
"33333333333333", | ||
"44444444444444", | ||
"55555555555555", | ||
"66666666666666", | ||
"77777777777777", | ||
"88888888888888", | ||
"99999999999999", | ||
]; | ||
|
||
/** | ||
* Check if a string is a valid cpf | ||
* - 55357314047 | ||
*/ | ||
export const isCnpj = (cnpj: string) => { | ||
if (getTypeof(cnpj) !== "string") return false; | ||
|
||
if (cnpj === "") return false; | ||
|
||
if (cnpj.length !== 14) return false; | ||
|
||
if (notCnpj.includes(cnpj)) return false; | ||
|
||
let length = cnpj.length - 2; | ||
let numbers = cnpj.substring(0, length); | ||
const digits = cnpj.substring(length); | ||
let sum = 0; | ||
let pos = length - 7; | ||
|
||
for (let i = length; i >= 1; i--) { | ||
const nbr = parseInt(numbers.charAt(length - i), 10); | ||
|
||
sum += nbr * pos--; | ||
|
||
if (pos < 2) pos = 9; | ||
} | ||
|
||
let result = sum % 11 < 2 ? 0 : 11 - (sum % 11); | ||
|
||
const firstChar = parseInt(digits.charAt(0), 10); | ||
|
||
if (result !== firstChar) return false; | ||
|
||
length += 1; | ||
numbers = cnpj.substring(0, length); | ||
sum = 0; | ||
pos = length - 7; | ||
|
||
for (let i = length; i >= 1; i--) { | ||
const nbr = parseInt(numbers.charAt(length - i), 10); | ||
|
||
sum += nbr * pos--; | ||
|
||
if (pos < 2) pos = 9; | ||
} | ||
|
||
result = sum % 11 < 2 ? 0 : 11 - (sum % 11); | ||
|
||
const secondChar = parseInt(digits.charAt(1), 10); | ||
|
||
if (result !== secondChar) return false; | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { getTypeof } from "../get-typeof"; | ||
import { isCnpj } from "../is-cnpj"; | ||
|
||
const MASKED_CNPJ = /^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/; | ||
|
||
export const isMaskedCNPJ = (maskedCNPJ: string) => | ||
getTypeof(maskedCNPJ) === "string" && | ||
MASKED_CNPJ.test(maskedCNPJ) && | ||
isCnpj(maskedCNPJ.replace(/[^\d]+/g, "")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { isCnpj } from "../lib/is-cnpj"; | ||
|
||
/** | ||
* | ||
* True | ||
* | ||
*/ | ||
|
||
describe("isCnpj (return True)", () => { | ||
it("with valid cnpj (1)", () => { | ||
expect(isCnpj("25024527000187")).toBe(true); | ||
}); | ||
|
||
it("with valid cnpj (2)", () => { | ||
expect(isCnpj("46884275000136")).toBe(true); | ||
}); | ||
|
||
it("with valid cnpj (3)", () => { | ||
expect(isCnpj("60722144000183")).toBe(true); | ||
}); | ||
|
||
it("with valid cnpj (4)", () => { | ||
expect(isCnpj("82304005000172")).toBe(true); | ||
}); | ||
|
||
it("with valid cnpj (5)", () => { | ||
expect(isCnpj("67141300000161")).toBe(true); | ||
}); | ||
}); | ||
|
||
/** | ||
* | ||
* False | ||
* | ||
*/ | ||
|
||
describe("isCnpj (return False)", () => { | ||
it("with empty cnpj", () => { | ||
expect(isCnpj("")).toBe(false); | ||
}); | ||
|
||
it("with masked cnpj", () => { | ||
expect(isCnpj("67.141.300/0001-61")).toBe(false); | ||
}); | ||
|
||
it("with invalid cnpj (1)", () => { | ||
expect(isCnpj("00000000000000")).toBe(false); | ||
}); | ||
|
||
it("with invalid cnpj (2)", () => { | ||
expect(isCnpj("38048166000151")).toBe(false); | ||
}); | ||
|
||
it("with invalid type", () => { | ||
expect(isCnpj(32457606000147 as any)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { isMaskedCNPJ } from "../lib/is-masked-cnpj"; | ||
|
||
/** | ||
* | ||
* True | ||
* | ||
*/ | ||
|
||
describe("isMaskedCNPJ (return True)", () => { | ||
it("with valid cnpj (1)", () => { | ||
expect(isMaskedCNPJ("30.424.723/0001-89")).toBe(true); | ||
}); | ||
|
||
it("with valid cnpj (2)", () => { | ||
expect(isMaskedCNPJ("17.024.335/0001-42")).toBe(true); | ||
}); | ||
|
||
it("with valid cnpj (3)", () => { | ||
expect(isMaskedCNPJ("56.440.046/0001-86")).toBe(true); | ||
}); | ||
|
||
it("with valid cnpj (4)", () => { | ||
expect(isMaskedCNPJ("63.551.672/0001-05")).toBe(true); | ||
}); | ||
|
||
it("with valid cnpj (5)", () => { | ||
expect(isMaskedCNPJ("58.410.040/0001-91")).toBe(true); | ||
}); | ||
}); | ||
|
||
/** | ||
* | ||
* False | ||
* | ||
*/ | ||
|
||
describe("isMaskedCNPJ (return False)", () => { | ||
it("with unmasked cnpj (1)", () => { | ||
expect(isMaskedCNPJ("58410040000191")).toBe(false); | ||
}); | ||
|
||
it("with invalid cnpj (1)", () => { | ||
expect(isMaskedCNPJ("00.000.000/0000-00")).toBe(false); | ||
}); | ||
|
||
it("with invalid cnpj (2)", () => { | ||
expect(isMaskedCNPJ("18.424.560/0001-31")).toBe(false); | ||
}); | ||
}); |