diff --git a/src/create.ts b/src/create.ts index e91a826..0fbfde6 100644 --- a/src/create.ts +++ b/src/create.ts @@ -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; @@ -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; } @@ -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; } diff --git a/src/mask.ts b/src/mask.ts index 3bfe6cd..e4c050f 100644 --- a/src/mask.ts +++ b/src/mask.ts @@ -68,7 +68,7 @@ export const MASKS: BigObject = { }, 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', @@ -85,15 +85,15 @@ export const MASKS: BigObject = { }, 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/]; } diff --git a/src/validate.ts b/src/validate.ts index 346ef43..ca94c51 100644 --- a/src/validate.ts +++ b/src/validate.ts @@ -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; diff --git a/test/mask.ts b/test/mask.ts index a554e53..b047e08 100644 --- a/test/mask.ts +++ b/test/mask.ts @@ -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', () => { @@ -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', () => { diff --git a/test/validate.ts b/test/validate.ts index 5a28d44..c94a81a 100644 --- a/test/validate.ts +++ b/test/validate.ts @@ -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', () => { @@ -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', () => {