DNA
8/24/2018 - 5:31 PM

JS CPF validation

JS CPF validation

#! /usr/bin/env node

function validate_cpf(strCPF) {
    if (new Set(strCPF).size == 1) return false

    cpf = Array.from(strCPF).map(Number);

    return [9, 10].every(pos => {
        multiplier = pos + 1

        vd = cpf.slice(0, pos).reduce((total, amount, index) => total + amount * (multiplier - index), 0);
        vd = (vd * 10) % 11;
        vd = (vd > 9) ? 0 : vd
    
        if(vd == cpf[pos]) return true
    })
}

let strCPF = "12345678909";

console.log(validate_cpf(strCPF));