Last active
February 24, 2025 07:13
-
-
Save leon-domingo/631a98a13afe6e6163422fa85d52e9c9 to your computer and use it in GitHub Desktop.
Función para validar el CUPS (Código Unificado de Punto de Suministro)
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
/** | |
* Valida un CUPS (Código Unificado de Punto de Suministro) dado | |
* @param {string} CUPS Código CUPS que se quiere verificar. Más información aquí: https://es.wikipedia.org/wiki/C%C3%B3digo_Unificado_de_Punto_de_Suministro | |
* @returns Resultado de la verificación. `true` si el CUPS dado es correcto, o `false` en caso contrario. | |
*/ | |
function validarCUPS(CUPS) { | |
let ret = false; | |
const reCUPS = /^[A-Z]{2}(\d{4}\d{12})([A-Z]{2})(\d[FPCRXYZ])?$/i; | |
if (reCUPS.test(CUPS)) { | |
const mCUPS = CUPS.toUpperCase().match(reCUPS); | |
const [, cups16, control] = mCUPS; | |
const letters = [ | |
'T', 'R', 'W', 'A', 'G', 'M', | |
'Y', 'F', 'P', 'D', 'X', 'B', | |
'N', 'J', 'Z', 'S', 'Q', 'V', | |
'H', 'L', 'C', 'K', 'E', | |
]; | |
const cup16Mod = +cups16 % 529, | |
quotient = Math.floor(cup16Mod / letters.length), | |
remainder = cup16Mod % letters.length; | |
ret = (control === letters[quotient] + letters[remainder]); | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment