fix: update CPF and CNPJ document formatting and parsing logic in demo sdk

This commit is contained in:
Claudecio Martins
2026-10-11 21:10:47 -03:00
parent 5f4a212786
commit f4adc99a01
+6 -5
View File
@@ -1037,6 +1037,7 @@
if (rawName.includes(':')) {
const parts = rawName.split(':');
displayName = parts[0].trim();
if (!docNum && parts[1]) {
docNum = parts[1].trim();
}
}
@@ -1044,14 +1045,14 @@
const digits = docNum ? String(docNum).replace(/\D/g, '') : '';
let docFormatted = '';
if (digits.length === 14) {
docFormatted = `CNPJ ${digits.replace(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/, '../-')}`;
docFormatted = 'CNPJ ' + digits.substring(0, 2) + '.' + digits.substring(2, 5) + '.' + digits.substring(5, 8) + '/' + digits.substring(8, 12) + '-' + digits.substring(12, 14);
} else if (digits.length === 11) {
docFormatted = `CPF ${digits.replace(/^(\d{3})(\d{3})(\d{3})(\d{2})$/, '..-')}`;
docFormatted = 'CPF ' + digits.substring(0, 3) + '.' + digits.substring(3, 6) + '.' + digits.substring(6, 9) + '-' + digits.substring(9, 11);
} else if (docNum) {
docFormatted = docNum;
}
const typeStr = `[${c.type || 'A1'}]`;
const typeStr = '[' + (c.type || 'A1') + ']';
const text = docFormatted ? `${displayName}, ${docFormatted} ${typeStr}` : `${displayName} ${typeStr}`;
opt.textContent = text;
certSelect.appendChild(opt);
@@ -1080,9 +1081,9 @@
const digits = String(rawDoc).replace(/\D/g, '');
let docStr = '';
if (digits.length === 14) {
docStr = `CNPJ: ${digits.replace(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/, '../-')}`;
docStr = 'CNPJ: ' + digits.substring(0, 2) + '.' + digits.substring(2, 5) + '.' + digits.substring(5, 8) + '/' + digits.substring(8, 12) + '-' + digits.substring(12, 14);
} else if (digits.length === 11) {
docStr = `CPF: ${digits.replace(/^(\d{3})(\d{3})(\d{3})(\d{2})$/, '..-')}`;
docStr = 'CPF: ' + digits.substring(0, 3) + '.' + digits.substring(3, 6) + '.' + digits.substring(6, 9) + '-' + digits.substring(9, 11);
} else {
docStr = cert.type === 'A3' ? 'Token Físico A3' : 'ICP-Brasil';
}