Adicionado validacao do cpf
This commit is contained in:
10
node_modules/brazilian-doc-validator/dist/index.js
generated
vendored
Normal file
10
node_modules/brazilian-doc-validator/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.cnpj = exports.cpf = void 0;
|
||||
const cpf_1 = __importDefault(require("./src/validator/cpf"));
|
||||
exports.cpf = cpf_1.default;
|
||||
const cnpj_1 = __importDefault(require("./src/validator/cnpj"));
|
||||
exports.cnpj = cnpj_1.default;
|
||||
136
node_modules/brazilian-doc-validator/dist/src/validator/cnpj.js
generated
vendored
Normal file
136
node_modules/brazilian-doc-validator/dist/src/validator/cnpj.js
generated
vendored
Normal file
@@ -0,0 +1,136 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
/**
|
||||
* CNPJ validation
|
||||
*
|
||||
* @param {string} cnpj
|
||||
*
|
||||
* @return {boolean}
|
||||
*
|
||||
*/
|
||||
const cnpjValidate = (cnpj) => {
|
||||
const invalidCnpjList = [
|
||||
'00000000000000',
|
||||
'11111111111111',
|
||||
'22222222222222',
|
||||
'33333333333333',
|
||||
'44444444444444',
|
||||
'55555555555555',
|
||||
'66666666666666',
|
||||
'77777777777777',
|
||||
'88888888888888',
|
||||
'99999999999999',
|
||||
];
|
||||
if (!Boolean(cnpj) || invalidCnpjList.includes(cnpj))
|
||||
return false;
|
||||
const onlyNumbers = cnpj.replace(/\D/g, '');
|
||||
if (onlyNumbers.length < 14)
|
||||
return false;
|
||||
let length = onlyNumbers.length - 2;
|
||||
let numbers = onlyNumbers.substring(0, length);
|
||||
const digits = onlyNumbers.substring(length);
|
||||
let addition = 0;
|
||||
let pos = length - 7;
|
||||
for (let i = length; i >= 1; i--) {
|
||||
addition += parseInt(numbers.charAt(length - i)) * pos--;
|
||||
if (pos < 2)
|
||||
pos = 9;
|
||||
}
|
||||
let result = addition % 11 < 2 ? 0 : 11 - addition % 11;
|
||||
if (result !== parseInt(digits.charAt(0)))
|
||||
return false;
|
||||
length = length + 1;
|
||||
numbers = onlyNumbers.substring(0, length);
|
||||
addition = 0;
|
||||
pos = length - 7;
|
||||
for (let i = length; i >= 1; i--) {
|
||||
addition += parseInt(numbers.charAt(length - i)) * pos--;
|
||||
if (pos < 2)
|
||||
pos = 9;
|
||||
}
|
||||
result = addition % 11 < 2 ? 0 : 11 - addition % 11;
|
||||
if (result !== parseInt(digits.charAt(1)))
|
||||
return false;
|
||||
return true;
|
||||
};
|
||||
/**
|
||||
* CNPJ Mask
|
||||
*
|
||||
* @param {string} cnpj
|
||||
*
|
||||
* @return {string}
|
||||
*
|
||||
*/
|
||||
const cnpjMask = (cnpj) => {
|
||||
if (cnpjValidate(cnpj)) {
|
||||
const onlyNumbers = cnpj.replace(/\D/g, '');
|
||||
return onlyNumbers
|
||||
.replace(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})*/, '$1.$2.$3/$4-$5');
|
||||
}
|
||||
return '';
|
||||
};
|
||||
/**
|
||||
* CNPJ Unmask
|
||||
*
|
||||
* @param {string} cnpj
|
||||
*
|
||||
* @return {string}
|
||||
*
|
||||
*/
|
||||
const cnpjUnmask = (cnpj) => {
|
||||
if (cnpjValidate(cnpj)) {
|
||||
return cnpj.replace(/\D/g, '');
|
||||
}
|
||||
return '';
|
||||
};
|
||||
/**
|
||||
* CNPJ Generation - Generates a valid CNPJ document.
|
||||
*
|
||||
* @param {Record<string, any>} options
|
||||
*
|
||||
* @return {string}
|
||||
*
|
||||
*/
|
||||
const cnpjGenerate = (options = { mask: true }) => {
|
||||
const { mask } = options;
|
||||
const createCnpjNumber = () => {
|
||||
const numbersList = createListNumber();
|
||||
numbersList.push(calculateDigitCnpj(numbersList));
|
||||
return `${numbersList.join('')}${calculateDigitCnpj(numbersList)}`;
|
||||
};
|
||||
const createListNumber = () => {
|
||||
const numbersList = [];
|
||||
for (let i = 0; i < 8; i++) {
|
||||
numbersList.push(randomNumber());
|
||||
}
|
||||
for (let i = 0; i < 3; i++) {
|
||||
numbersList.push(0);
|
||||
}
|
||||
numbersList.push(1);
|
||||
return numbersList;
|
||||
};
|
||||
const randomNumber = () => Math.floor(Math.random() * 9);
|
||||
const calculateRestCnpj = (sumDigit) => sumDigit % 11;
|
||||
const calculateSumDigit = (numbersList) => {
|
||||
let sumDigit = 0;
|
||||
let salt = 9;
|
||||
for (let i = numbersList.length - 1; i >= 0; i--) {
|
||||
sumDigit += numbersList[i] * salt--;
|
||||
if (salt < 2)
|
||||
salt = 9;
|
||||
}
|
||||
return sumDigit;
|
||||
};
|
||||
const calculateDigitCnpj = (numbersList) => {
|
||||
const digit = calculateRestCnpj(calculateSumDigit(numbersList));
|
||||
return (digit >= 10) ? 0 : digit;
|
||||
};
|
||||
const newCnpj = createCnpjNumber();
|
||||
return (mask) ? cnpjMask(newCnpj) : newCnpj;
|
||||
};
|
||||
exports.default = {
|
||||
validate: cnpjValidate,
|
||||
mask: cnpjMask,
|
||||
unmask: cnpjUnmask,
|
||||
generate: cnpjGenerate,
|
||||
};
|
||||
104
node_modules/brazilian-doc-validator/dist/src/validator/cpf.js
generated
vendored
Normal file
104
node_modules/brazilian-doc-validator/dist/src/validator/cpf.js
generated
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
"use strict";
|
||||
/**
|
||||
* CPF validation
|
||||
*
|
||||
* @param {string} cpf
|
||||
*
|
||||
* @returns {boolean}
|
||||
*
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const cpfValidation = (cpf) => {
|
||||
if (!Boolean(cpf))
|
||||
return false;
|
||||
const onlyNumbers = cpf.replace(/\D/g, '');
|
||||
if (onlyNumbers.length < 11)
|
||||
return false;
|
||||
let adition;
|
||||
let remainder;
|
||||
adition = 0;
|
||||
if (onlyNumbers === '00000000000')
|
||||
return false;
|
||||
for (let i = 1; i <= 9; i++) {
|
||||
adition = adition + parseInt(onlyNumbers.substring(i - 1, i)) * (11 - i);
|
||||
}
|
||||
remainder = (adition * 10) % 11;
|
||||
if ((remainder == 10) || (remainder == 11))
|
||||
remainder = 0;
|
||||
if (remainder != parseInt(onlyNumbers.substring(9, 10)))
|
||||
return false;
|
||||
adition = 0;
|
||||
for (let i = 1; i <= 10; i++) {
|
||||
adition = adition + parseInt(onlyNumbers.substring(i - 1, i)) * (12 - i);
|
||||
}
|
||||
remainder = (adition * 10) % 11;
|
||||
if ((remainder == 10) || (remainder == 11))
|
||||
remainder = 0;
|
||||
if (remainder != parseInt(onlyNumbers.substring(10, 11)))
|
||||
return false;
|
||||
return true;
|
||||
};
|
||||
/**
|
||||
* CPF remove mask
|
||||
*
|
||||
* @param {string} cpf
|
||||
*
|
||||
* @returns {string}
|
||||
*
|
||||
*/
|
||||
const cpfAddMask = (cpf) => {
|
||||
if (cpfValidation(cpf)) {
|
||||
const onlyNumbers = cpf.replace(/\D/g, '');
|
||||
return onlyNumbers.replace(/^(\d{3})(\d{3})(\d{3})(\d{2})*/, '$1.$2.$3-$4');
|
||||
}
|
||||
return '';
|
||||
};
|
||||
/**
|
||||
* CPF add mask
|
||||
*
|
||||
* @param {string} cpf
|
||||
*
|
||||
* @returns {string}
|
||||
*
|
||||
*/
|
||||
const cpfRemoveMask = (cpf) => {
|
||||
if (cpfValidation(cpf))
|
||||
return cpf.replace(/\D/g, '');
|
||||
return '';
|
||||
};
|
||||
/**
|
||||
* CPF Generator
|
||||
*
|
||||
* @param {Record<string, any>} options
|
||||
*
|
||||
* @return {string}
|
||||
*
|
||||
*/
|
||||
const cpfGenerate = (options = { mask: true }) => {
|
||||
const { mask } = options;
|
||||
const createArray = (total, numero) => Array.from(Array(total), () => numberRandom(numero));
|
||||
const numberRandom = (number) => (Math.round(Math.random() * number));
|
||||
const mod = (dividendo, divisor) => Math.round(dividendo - (Math.floor(dividendo / divisor) * divisor));
|
||||
const array = createArray(9, 9);
|
||||
const formulaReducer = (number, array) => {
|
||||
return array.reduce((prevVal, elem, index) => {
|
||||
return prevVal + elem * (number - index);
|
||||
}, 0);
|
||||
};
|
||||
let d1 = formulaReducer(10, array);
|
||||
d1 = 11 - (mod(d1, 11));
|
||||
if (d1 >= 10)
|
||||
d1 = 0;
|
||||
let d2 = (d1 * 2) + formulaReducer(11, array);
|
||||
d2 = 11 - (mod(d2, 11));
|
||||
if (d2 >= 10)
|
||||
d2 = 0;
|
||||
const newCpf = `${array.join('')}${d1}${d2}`;
|
||||
return (mask) ? cpfAddMask(newCpf) : newCpf;
|
||||
};
|
||||
exports.default = {
|
||||
validate: cpfValidation,
|
||||
mask: cpfAddMask,
|
||||
unmask: cpfRemoveMask,
|
||||
generate: cpfGenerate,
|
||||
};
|
||||
65
node_modules/brazilian-doc-validator/dist/tests/cnpj.test.js
generated
vendored
Normal file
65
node_modules/brazilian-doc-validator/dist/tests/cnpj.test.js
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const cnpj_1 = __importDefault(require("../src/validator/cnpj"));
|
||||
const emptyCnpj = '';
|
||||
const validCnpjMasked = '31.257.435/0001-40';
|
||||
const validCnpjUnmasked = '31257435000140';
|
||||
const invalidCnpjMasked = '31.257.435/0001-4';
|
||||
const invalidCnpjUnmasked = '3125743500014';
|
||||
describe('CNPJ Validation Test', () => {
|
||||
it('Should validate a valid CNPJ number', () => {
|
||||
const isValid = cnpj_1.default.validate(emptyCnpj);
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
it('Should validate a valid CNPJ masked number', () => {
|
||||
const isValid = cnpj_1.default.validate(validCnpjMasked);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
it('Should validate a valid CNPJ unmasked number', () => {
|
||||
const isValid = cnpj_1.default.validate(validCnpjUnmasked);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
it('Should validate a invalid CNPJ masked number', () => {
|
||||
const isValid = cnpj_1.default.validate(invalidCnpjMasked);
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
it('Should validate a invalid CNPJ unmasked number', () => {
|
||||
const isValid = cnpj_1.default.validate(invalidCnpjUnmasked);
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
});
|
||||
describe('CNPJ Mask Test', () => {
|
||||
it('Should have mask on a valid cnpj or return empty string', () => {
|
||||
const newCnpj = cnpj_1.default.mask(validCnpjUnmasked);
|
||||
expect(newCnpj).toBe(validCnpjMasked);
|
||||
});
|
||||
it('Should return empty string on invalid CNPJ', () => {
|
||||
const newCnpj = cnpj_1.default.mask(invalidCnpjUnmasked);
|
||||
expect(newCnpj).toBe('');
|
||||
});
|
||||
});
|
||||
describe('CNPJ Unmask Test', () => {
|
||||
it('Should be unmasked string on a valid cnpj or return empty string', () => {
|
||||
const newCnpj = cnpj_1.default.unmask(validCnpjMasked);
|
||||
expect(newCnpj).toBe(validCnpjUnmasked);
|
||||
});
|
||||
it('Should return empty string on invalid CNPJ', () => {
|
||||
const newCnpj = cnpj_1.default.unmask(invalidCnpjUnmasked);
|
||||
expect(newCnpj).toBe('');
|
||||
});
|
||||
});
|
||||
describe('CNPJ Generator Test', () => {
|
||||
it('Should generate a valid cnpj', () => {
|
||||
const newCnpj = cnpj_1.default.generate();
|
||||
const isValid = cnpj_1.default.validate(newCnpj);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
it('Should generate a valid cnpj and formmat', () => {
|
||||
const newCnpj = cnpj_1.default.generate({ format: true });
|
||||
const isValid = cnpj_1.default.validate(newCnpj);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
});
|
||||
74
node_modules/brazilian-doc-validator/dist/tests/cpf.test.js
generated
vendored
Normal file
74
node_modules/brazilian-doc-validator/dist/tests/cpf.test.js
generated
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const cpf_1 = __importDefault(require("../src/validator/cpf"));
|
||||
const emptyCpf = '';
|
||||
const validCpf = '047.145.880-52';
|
||||
const validOnlyNumbers = '04714588052';
|
||||
const invalidCpfLength = '047.145.880-5';
|
||||
const invalidCpf = '047.145.880-54';
|
||||
const invalidOnlyNumbers = '04714588054';
|
||||
describe('CPF Validation test', () => {
|
||||
it('Correct check if CPF is null or empty', () => {
|
||||
const isEmpty = cpf_1.default.validate(emptyCpf);
|
||||
expect(isEmpty).toBeFalsy();
|
||||
});
|
||||
it('Correct check if CPF provided is valid', () => {
|
||||
const isValid = cpf_1.default.validate(validCpf);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
it('Correct check if CPF provided is invalid', () => {
|
||||
const isValid = cpf_1.default.validate(invalidCpf);
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
it('Correct check if CPF provided has correct length', () => {
|
||||
const isValid = cpf_1.default.validate(invalidCpfLength);
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
it('Correct check if CPF provided is 00000000000 ', () => {
|
||||
const isValid = cpf_1.default.validate('00000000000');
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
});
|
||||
describe('CPF Mask test', () => {
|
||||
it('Correct check if CPF is null or empty', () => {
|
||||
const isEmpty = cpf_1.default.mask(emptyCpf);
|
||||
expect(isEmpty).toBe('');
|
||||
});
|
||||
it('Correct check if CPF is masked in the right format ', () => {
|
||||
const valid = cpf_1.default.mask(validOnlyNumbers);
|
||||
expect(valid).toBe(validCpf);
|
||||
});
|
||||
it('Correct check if invalid CPF returns empty string', () => {
|
||||
const invalid = cpf_1.default.mask(invalidOnlyNumbers);
|
||||
expect(invalid).toBe('');
|
||||
});
|
||||
});
|
||||
describe('CPF Unmask test', () => {
|
||||
it('Correct check if CPF is null or empty', () => {
|
||||
const isEmpty = cpf_1.default.unmask(emptyCpf);
|
||||
expect(isEmpty).toBe('');
|
||||
});
|
||||
it('Correct check if CPF unmasked (only numbers)', () => {
|
||||
const valid = cpf_1.default.unmask(validCpf);
|
||||
expect(valid).toBe(validOnlyNumbers);
|
||||
});
|
||||
it('Correct check if invalid CPF returns empty string', () => {
|
||||
const invalid = cpf_1.default.unmask(invalidOnlyNumbers);
|
||||
expect(invalid).toBe('');
|
||||
});
|
||||
});
|
||||
describe('CPF Generator test', () => {
|
||||
it('Correct check if generated CPF is valid and unmasked', () => {
|
||||
const cpfGenerated = cpf_1.default.generate({ mask: true });
|
||||
const isValid = cpf_1.default.validate(cpfGenerated);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
it('Correct check if generated CPF is valid and masked', () => {
|
||||
const cpfGenerated = cpf_1.default.generate({ mask: false });
|
||||
const isValid = cpf_1.default.validate(cpfGenerated);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user