my-utils-engine
Version:
Funções utilitárias para projetos em Angola (BI, telefone, currency, etc.) Comentários: os comentários no código estão em português. Nomes de funções em inglês.
31 lines (30 loc) • 1.09 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateBI = validateBI;
/**
* Validação do BI de Angola.
* - Local: usa regex simples -> 9 dígitos + 2 letras + 3 dígitos
* - Remoto: usa a API oficial (https://angolaapi.herokuapp.com)
*/
async function validateBI(bi, options) {
const normalized = String(bi).trim().toUpperCase();
const localRegex = /^\d{9}[A-Z]{2}\d{3}$/;
if (!(options === null || options === void 0 ? void 0 : options.useRemote)) {
return localRegex.test(normalized);
}
const apiUrl = `https://angolaapi.herokuapp.com/api/v1/validate/bi/${bi}`;
try {
const url = apiUrl.replace("{bi}", encodeURIComponent(normalized));
const res = await fetch(url);
if (!res.ok)
return localRegex.test(normalized);
const data = await res.json();
if (typeof data.message === "string") {
return data.message.includes("valid");
}
return localRegex.test(normalized);
}
catch {
return localRegex.test(normalized);
}
}
;