-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalida-cpf.js
55 lines (46 loc) · 1.39 KB
/
valida-cpf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class ValidaCpf {
constructor(cpfEnviado) {
Object.defineProperties(this, {
cpfLimpo: {
configurable: false,
enumerable: false,
writable: false,
value: cpfEnviado.replace(/\D+/g, ""),
},
generateDigit: {
enumerable: false,
writable: false,
configurable: false,
value: (cpfParcial) => {
if (!cpfParcial) return false;
if (typeof cpfParcial !== "string") return false;
if (cpfParcial.length > 10 || cpfParcial.length < 9) return false;
cpfParcial = Array.from(cpfParcial);
let counter = cpfParcial.length + 1;
let digit = cpfParcial.reduce((amount, item) => {
amount += item * counter;
counter--;
return amount;
}, 0);
digit = 11 - (digit % 11);
return digit > 9 ? 0 : digit;
},
},
});
}
isSequence() {
return this.cpfLimpo.charAt(0).repeat(11) === this.cpfLimpo;
}
valida() {
if (this.isSequence()) return false;
if (!this.cpfLimpo) return false;
if (typeof this.cpfLimpo !== "string") return false;
if (this.cpfLimpo.length != 11) return false;
let cpfParcial = this.cpfLimpo.slice(0, -2);
let digit = String(this.generateDigit(cpfParcial));
digit = digit.concat(String(this.generateDigit(cpfParcial.concat(digit))));
return cpfParcial.concat(digit) === this.cpfLimpo;
}
}
let validaCpf = new ValidaCpf("003.700.153-12");
console.log(validaCpf.valida());