Created
July 3, 2019 14:29
-
-
Save themakunga/ddd1040ff394421da724b4cf6bc4fde8 to your computer and use it in GitHub Desktop.
Validador de RUT chileno en js ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const RutLengthException = (message) => { | |
this.message = message; | |
this.type = 'RutLengthException'; | |
}; | |
RutLengthException.prototype.toString = () => `${this.type}: ${this.message}`; | |
const validadorRut = (rut) => { | |
let sum = 0; | |
let mul = 2; | |
let index = 0; | |
const value = String(rut).replace('.', '').replace('-', ''); | |
const body = value.slice(0, -1); | |
let dv = value.slice(-1).toUpperCase(); | |
const format = `${body}-${dv}`; | |
try { | |
if (body.length <= 6) { | |
throw new RutLengthException('Largo de RUT no válido'); | |
} | |
value.forEach((num) => { | |
index = mul * parseInt(num, 10); | |
sum += index; | |
if (mul < 7) { | |
mul += 1; | |
} else { | |
mul = 2; | |
} | |
}); | |
const dvCalc = 11 - (sum % 11); | |
dv = (dv === 'K') ? 10 : dv; | |
dv = (dv === 0) ? 11 : dv; | |
if (dv !== dvCalc) { | |
throw new RutLengthException('Digito verificador incorrecto'); | |
} | |
return format; | |
} catch (e) { | |
console.error(e); | |
} | |
return false; | |
}; | |
module.exports = validadorRut; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment