@studiovisual/brazilian-validator-js
Version:
A lightweight and efficient JavaScript library for validating Brazilian documents and phone numbers
34 lines (33 loc) • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValidCPFLength = isValidCPFLength;
exports.isValidCPF = isValidCPF;
const utils_1 = require("./utils");
/**
* Checks if the provided CPF (Brazilian identification number) has a valid length.
* @param value The CPF value to validate.
* @returns A boolean indicating whether the length is valid.
*/
function isValidCPFLength(value) {
value = (0, utils_1.extractDigits)(value);
// Checks if the CPF number has 11 digits
return value.length === 11;
}
/**
* Checks if the provided CPF (Brazilian identification number) is valid.
* @param value The CPF value to validate.
* @returns A boolean indicating whether the CPF is valid.
*/
function isValidCPF(value) {
value = (0, utils_1.extractDigits)(value);
if (!isValidCPFLength(value))
return false;
const values = value.split("").map((el) => +el);
const rest = (count) => ((values
.slice(0, count - 12)
.reduce((sum, el, index) => sum + el * (count - index), 0) *
10) %
11) %
10;
return rest(10) === values[9] && rest(11) === values[10];
}