This repository was archived by the owner on Nov 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (97 loc) · 2.63 KB
/
Copy pathindex.js
File metadata and controls
115 lines (97 loc) · 2.63 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use strict"
/*
Andrés Fuenzalida - 2021
https://github.com/afuenzalida
*/
/**
* @param {(string|number)} rut
* @returns {boolean}
*/
function validateRUT(rut) {
if (typeof rut !== 'string' && typeof rut !== 'number') {
console.log(typeof rut)
throw new TypeError('Input parameter must be of type string or integer')
}
const cleanRUT = typeof rut === 'string' ? clearRUT(rut) : String(rut)
const checkDigit = [...cleanRUT].slice(-1)[0]
const withoutCheckDigitRUT = cleanRUT.slice(0, -1)
const obtainedCheckDigit = getCheckDigit(withoutCheckDigitRUT)
return checkDigit.toLowerCase() === obtainedCheckDigit.toLowerCase()
}
/**
* @param {(string|number)} rut
* @returns {string}
*/
function getCheckDigit(rut) {
const cleanRUT = clearRUT(rut)
const reversedRUT = [...String(cleanRUT)].map(v => parseInt(v)).reverse()
let result = 0
for (let i = 0, j = 2; i < reversedRUT.length; i++, j < 7 ? j++ : j = 2) {
result += reversedRUT[i] * j;
}
const value = (11 - (result % 11)) % 11
return (value) <= 9 ? String(value) : 'K'
}
/**
* @param {string} rut
* @returns {string}
*/
function clearRUT(rut) {
return String(rut).replace(/[^0-9a-z]/gi, '');
}
/**
* @param {number} amount
* @param {boolean} dots
* @param {boolean} hyphen
* @returns {Array<string>}
*/
function generateRandomRUT(amount = 1, dots = false, hyphen = false) {
const generatedRUTs = [...Array(amount).keys()].map(() => {
const rut = Math.floor(1000000 + Math.random() * 30000000)
return `${dots ? rut.toLocaleString("es-CL") : rut}${hyphen ? '-' : ''}${getCheckDigit(rut)}`
})
return amount === 1 ? generatedRUTs[0] : generatedRUTs
}
/**
* @deprecated since 3.0.0
*/
const validarRUT = (rut) => {
if (typeof rut === 'string' || typeof rut === 'number') {
const rutSinFormato = limpiarRUT(rut);
const rutSinDv = rutSinFormato.slice(0, -1);
const rutDv = rutSinFormato.split('').pop().toLowerCase();
return calcularDv(rutSinDv) === rutDv;
}
else {
return false;
}
};
/**
* @deprecated since 3.0.0
*/
const calcularDv = rut => {
let suma = 0;
let rutReversa = limpiarRUT(rut).split('').reverse();
console.log(rutReversa)
for (let i = 0, j = 2; i < rutReversa.length; i++, j < 7 ? j++ : j = 2) {
suma += rutReversa[i] * j;
}
let resultado = 11 - (suma % 11)
if (resultado === 11) return '0';
if (resultado === 10) return 'k';
return String(resultado);
};
/**
* @deprecated since 3.0.0
*/
const limpiarRUT = rut => {
return String(rut).replace(/[^0-9a-z]/gi, '');
}
module.exports = {
clearRUT,
generateRandomRUT,
getCheckDigit,
validateRUT,
validarRUT,
calcularDv,
}