这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function create_cnh(value: string) {
}

export function create_cnpj(cnpj: string) {
cnpj = cnpj.replace(/[^\d]+/g, '');
cnpj = cnpj.replace(/[^0-9A-Z]/g, "");

if (cnpj === '') {
return false;
Expand All @@ -144,13 +144,21 @@ export function create_cnpj(cnpj: string) {
return false;
}

function valorDecimal(input: string, index: number): number {
const code = input.charCodeAt(index);

if (47 < code && code < 58) return parseInt(input.charAt(index), 10); // numeric (0-9)

return code - 48; // alphanumeric
}

// Valida DVs
let tamanho: number = cnpj.length - 2
let numeros: any = cnpj.substring(0, tamanho);
let tamanho: number = cnpj.length - 2;
let entrada: any = cnpj.substring(0, tamanho);
let soma: any = 0;
let pos = tamanho - 7;
for (let i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
soma += valorDecimal(entrada, (tamanho - i)) * pos--;
if (pos < 2) {
pos = 9;
}
Expand All @@ -160,11 +168,11 @@ export function create_cnpj(cnpj: string) {
resultados[0] = soma % 11 < 2 ? 0 : 11 - soma % 11;

tamanho = tamanho + 1;
numeros = cnpj.substring(0, tamanho);
entrada = cnpj.substring(0, tamanho);
soma = 0;
pos = tamanho - 7;
for (let i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
soma += valorDecimal(entrada, (tamanho - i)) * pos--;
if (pos < 2) {
pos = 9;
}
Expand Down
8 changes: 4 additions & 4 deletions src/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const MASKS: BigObject<MaskType> = {
},
cnpj: {
text: '00.000.000/0000-00',
textMask: [/\d/, /\d/, '.', /\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/]
textMask: [/[0-9A-Z]/, /[0-9A-Z]/, '.', /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, '.', /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, '/', /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, '-', /\d/, /\d/]
},
cns: {
text: '000 0000 0000 00-00',
Expand All @@ -85,15 +85,15 @@ export const MASKS: BigObject<MaskType> = {
},
cpfcnpj: {
text: '00.000.000/0000-00',
textMask: [/\d/, /\d/, '.', /\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/],
textMask: [/[0-9A-Z]/, /[0-9A-Z]/, '.', /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, '.', /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, '/', /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, '-', /\d/, /\d/],
textMaskFunction: function mask(userInput: any) {
const numbers = userInput.match(/\d/g);
const numbers = userInput.match(/[0-9A-Z]/g);
let numberLength = 0;
if (numbers) {
numberLength = numbers.join('').length;
}
if (!userInput || numberLength > 12) {
return [/\d/, /\d/, '.', /\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/];
return [/[0-9A-Z]/, /[0-9A-Z]/, '.', /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, '.', /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, '/', /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, /[0-9A-Z]/, '-', /\d/, /\d/];
} else {
return [/\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, '-', /\d/, /\d/];
}
Expand Down
21 changes: 14 additions & 7 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,26 @@ export function validate_cnh(value: string) {

export function validate_cnpj(cnpj: any) {
// Valida se tem apenas número, - ou .
let precisaFicarVazio = cnpj.replace(/^[0-9./-]*$/gm, '')
if (precisaFicarVazio != '')
return false
let precisaFicarVazio = cnpj.replace(/^[0-9A-Z./-]*$/gm, "");
if (precisaFicarVazio != "") return false;

function valorDecimal(input: string, index: number): number {
const code = input.charCodeAt(index);

if (47 < code && code < 58) return parseInt(input.charAt(index), 10); // numeric (0-9)

cnpj = cnpj.replace(/[^\d]+/g, '');
return code - 48; // alphanumeric
}

cnpj = cnpj.replace(/[^0-9A-Z]/g, '');
let tamanho = cnpj.length - 2
const digitos = cnpj.substring(tamanho);
const resultados = create_cnpj(cnpj);
if (!resultados ||
resultados[0] !== parseInt(digitos.charAt(0), 10) ||
resultados[1] !== parseInt(digitos.charAt(1), 10)
resultados[0] !== valorDecimal(digitos, 0) ||
resultados[1] !== valorDecimal(digitos, 1)
) {
return false
return false;
}

return true;
Expand Down
12 changes: 12 additions & 0 deletions test/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ describe('Mask test', () => {
const cnpj = '83529443183182';
expect(maskBr.cnpj(cnpj)).to.be.equal('83.529.443/1831-82');
expect(maskBr.cnpj('123')).to.exist;

const cnpjAlfanumerico = "12ABC34501DE35";
expect(maskBr.cnpj(cnpjAlfanumerico)).to.be.equal('12.ABC.345/01DE-35');
expect(maskBr.cnpj("12ABC34501DEAB")).to.be.equal('12.ABC.345/01DE-__');
expect(maskBr.cnpj("12ABC34501DEA1")).to.be.equal('12.ABC.345/01DE-1_');
expect(maskBr.cnpj('ABC')).to.exist;
});

it('CNS', () => {
Expand All @@ -90,6 +96,12 @@ describe('Mask test', () => {
const cnpj = '83529443183182';
expect(maskBr.cpfcnpj(cnpj)).to.be.equal('83.529.443/1831-82');
expect(maskBr.cpfcnpj('123456')).to.exist;

const cnpjAlfanumerico = "12ABC34501DE35";
expect(maskBr.cpfcnpj(cnpjAlfanumerico)).to.be.equal('12.ABC.345/01DE-35');
expect(maskBr.cpfcnpj("12ABC34501DEAB")).to.be.equal('12.ABC.345/01DE-__');
expect(maskBr.cpfcnpj("12ABC34501DEA1")).to.be.equal('12.ABC.345/01DE-1_');
expect(maskBr.cpfcnpj('ABC')).to.exist;
});

it('cartaocredito - TODO', () => {
Expand Down
4 changes: 4 additions & 0 deletions test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ describe('Validate test', () => {
expect(validateBr.cnpj('56.853.433/0001-44')).to.be.true;
expect(validateBr.cnpj('56.853.433/0001-55')).to.be.false;
expect(validateBr.cnpj('1234')).to.be.false;
expect(validateBr.cnpj("12.ABC.345/01DE-35")).to.be.true;
expect(validateBr.cnpj("12.ABC.345/01DE-33")).to.be.false;
});

it('CNS', () => {
Expand Down Expand Up @@ -123,6 +125,8 @@ describe('Validate test', () => {
expect(validateBr.cpfcnpj('127.529.875-37')).to.be.false;
expect(validateBr.cpfcnpj('00.000.000/0000-00')).to.be.false;
expect(validateBr.cpfcnpj('090.988.020-44')).to.be.true;
expect(validateBr.cpfcnpj("12.ABC.345/01DE-35")).to.be.true;
expect(validateBr.cpfcnpj("12.ABC.345/01DE-33")).to.be.false;
});

describe('Cartão de Crédito', () => {
Expand Down